Last active
March 9, 2021 17:11
-
-
Save ernestkamara/573efbf7c3fe4f5e2794ad5ff27f7597 to your computer and use it in GitHub Desktop.
Solution to Salone Hackathon 2019 Entry Questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###1. FIND THE LONGEST WORD | |
["hello", "world", "hi", "bye"] | |
###Solution: | |
Given that there is only one longest word in the Array or the first longest word is a valid | |
```kotlin | |
fun findLongestWord(words: List<String>): String { | |
var longestWord = "" | |
words.onEach { word -> if(word.length > longestWord.length) longestWord = word } | |
return longestWord | |
} | |
``` | |
Test: | |
```kotlin | |
fun main() { | |
val words = listOf("hello", "world", "hi", "bye") | |
println("Longest word: ${findLongestWord(words)}") | |
} | |
``` | |
###2. RETURN ALL ODDS | |
[1, 2, 3, 4, 5, 7] | |
```kotlin | |
fun filterOdd(numbers: List<Int>): List<Int> { | |
return numbers.filter { num -> num % 2 != 0} | |
} | |
``` | |
###3. SORT DICTIONARY/MAP BASED ON KEY | |
{"key": "value", "ab": "hi there", "we": "say what"} | |
```kotlin | |
fun filterKeys(values: Map<String, String>): Map<String, String> { | |
return values.filterKeys { key -> key.isNotEmpty() } | |
} | |
``` | |
###4. READING A FILE | |
read a file called helloworld.txt and return line #10 | |
//TODO: | |
###5. READ A CSV FILE | |
you have a csv file called helloworld.txt with the following data in it | |
hi,bye,there,3 | |
go,there,hi,3 | |
hi,bye,there,4 | |
Assuming the CVS file as been parsed to a line of String.. | |
```kotlin | |
fun MutableMap<String, Int>.addOrUpdateMap(line: String){ | |
val list = line.split(",") | |
var value = list[list.size - 1].toInt() | |
val key = list.joinToString(limit = 3, separator = ",") | |
if (this.containsKey(key)) { | |
value += this.get(key) ?: 0 | |
} | |
this.put(key, value) | |
println("Map $this") | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment