Skip to content

Commit 994b01c

Browse files
Add Linear Search in Kotlin (#4482)
* initial commit * initial draft, all tests passed * refined code and comments
1 parent af98312 commit 994b01c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

archive/k/kotlin/LinearSearch.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fun main(args: Array<String>)
2+
{
3+
// store usage message in variable
4+
val message = "Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")"
5+
6+
// validate input array is correct size and does not contain empty Strings
7+
if(args.size !=2 || args[0].isBlank() || args[1].isBlank())
8+
{
9+
println(message)
10+
return
11+
}
12+
13+
// convert input number String into a List of integers, invalid characters are converted to null
14+
val intList = args[0].split(",").map { it.trim().toIntOrNull() }
15+
16+
// convert input key String into an int, invalid characters are converted to null
17+
val key = args[1].toIntOrNull()
18+
19+
// check if the List or the key contains null (invalid) elements
20+
if(null in intList || key == null)
21+
{
22+
println(message)
23+
return
24+
}
25+
26+
// check if key is in the List and print returned boolean
27+
println(key in intList)
28+
}

0 commit comments

Comments
 (0)