From 6c32ba9a5e8e3d4680aefc6a1efa7ab9ce9c2e15 Mon Sep 17 00:00:00 2001 From: Matthew Larsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 12 Feb 2025 06:37:50 +0000 Subject: [PATCH 1/3] initial commit --- archive/k/kotlin/LinearSearch.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 archive/k/kotlin/LinearSearch.kt diff --git a/archive/k/kotlin/LinearSearch.kt b/archive/k/kotlin/LinearSearch.kt new file mode 100644 index 000000000..e54179666 --- /dev/null +++ b/archive/k/kotlin/LinearSearch.kt @@ -0,0 +1,12 @@ +/* + +Linear search is quite intuitive, it is basically searching an element in an array by traversing the array from the beginning to the end and comparing each item in the array with the key. If a particular array entry matches with the key the position is recorded and the loop is stopped. The algorithm for this is: + +Define a flag (set it's value to 0) for checking if key is present in array or notation. +Iterate through every element in array. +In each iteration compare the key and the current element. +If they match set the flag to 1, position to the current iteration and break from the loop. +If entire loop is traversed and the element is not found the value of flag will be 0 and user can notified that key is not in array. + +*/ + From f294f2115ca0ba64de547d04e0983706fddc951f Mon Sep 17 00:00:00 2001 From: Matthew Larsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 12 Feb 2025 09:06:55 +0000 Subject: [PATCH 2/3] initial draft, all tests passed --- archive/k/kotlin/LinearSearch.kt | 41 +++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/archive/k/kotlin/LinearSearch.kt b/archive/k/kotlin/LinearSearch.kt index e54179666..ed9b29d2c 100644 --- a/archive/k/kotlin/LinearSearch.kt +++ b/archive/k/kotlin/LinearSearch.kt @@ -1,6 +1,9 @@ /* -Linear search is quite intuitive, it is basically searching an element in an array by traversing the array from the beginning to the end and comparing each item in the array with the key. If a particular array entry matches with the key the position is recorded and the loop is stopped. The algorithm for this is: +Linear search is quite intuitive, it is basically searching an element in an array by traversing +the array from the beginning to the end and comparing each item in the array with the key. If a +particular array entry matches with the key the position is recorded and the loop is stopped. +The algorithm for this is: Define a flag (set it's value to 0) for checking if key is present in array or notation. Iterate through every element in array. @@ -10,3 +13,39 @@ If entire loop is traversed and the element is not found the value of flag will */ + +fun main(args: Array) +{ + // store usage message in variable + val message = "Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")" + + // validate input array is correct size and does not contain empty Strings + if(args.size !=2 || args[0].isBlank() || args[1].isBlank()) + { + println(message) + return + } + + // convert list of numbers string input into an int array, invalid entries converted to null + val intArray = args[0].split(",").map { it.trim().toIntOrNull() } + + // check if array contains null (invalid) elements + if(null in intArray) + { + println(message) + return + } + + // convert key string input into an int, invalid entries converted to null + val key = args[1].toIntOrNull() + + // check if key is null (invalid) + if(key == null) + { + println(message) + return + } + + // check if key is in the array and print returned boolean + println(key in intArray) +} \ No newline at end of file From be8b2481c750ed51f8bac1182fb4db2261c0f740 Mon Sep 17 00:00:00 2001 From: Matthew Larsen <174657206+mlarsen-source@users.noreply.github.com> Date: Thu, 13 Feb 2025 20:46:24 +0000 Subject: [PATCH 3/3] refined code and comments --- archive/k/kotlin/LinearSearch.kt | 39 +++++++------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/archive/k/kotlin/LinearSearch.kt b/archive/k/kotlin/LinearSearch.kt index ed9b29d2c..80a53c482 100644 --- a/archive/k/kotlin/LinearSearch.kt +++ b/archive/k/kotlin/LinearSearch.kt @@ -1,19 +1,3 @@ -/* - -Linear search is quite intuitive, it is basically searching an element in an array by traversing -the array from the beginning to the end and comparing each item in the array with the key. If a -particular array entry matches with the key the position is recorded and the loop is stopped. -The algorithm for this is: - -Define a flag (set it's value to 0) for checking if key is present in array or notation. -Iterate through every element in array. -In each iteration compare the key and the current element. -If they match set the flag to 1, position to the current iteration and break from the loop. -If entire loop is traversed and the element is not found the value of flag will be 0 and user can notified that key is not in array. - -*/ - - fun main(args: Array) { // store usage message in variable @@ -26,26 +10,19 @@ fun main(args: Array) return } - // convert list of numbers string input into an int array, invalid entries converted to null - val intArray = args[0].split(",").map { it.trim().toIntOrNull() } - - // check if array contains null (invalid) elements - if(null in intArray) - { - println(message) - return - } - - // convert key string input into an int, invalid entries converted to null + // convert input number String into a List of integers, invalid characters are converted to null + val intList = args[0].split(",").map { it.trim().toIntOrNull() } + + // convert input key String into an int, invalid characters are converted to null val key = args[1].toIntOrNull() - // check if key is null (invalid) - if(key == null) + // check if the List or the key contains null (invalid) elements + if(null in intList || key == null) { println(message) return } - // check if key is in the array and print returned boolean - println(key in intArray) + // check if key is in the List and print returned boolean + println(key in intList) } \ No newline at end of file