Copy below code to https://repl.it/languages/kotlin and modify the code to complete the assesment tasks as below. Once you completed click on share to share a link to the assesment.
import kotlinx.coroutines.*
val words = arrayOf(
"start", "citizen", "flour", "circle", "petty", "neck", "seem", "lake", "page",
"color", "ceiling", "angle", "agent", "mild", "touch", "bite", "cause", "finance",
"greet", "eat", "minor", "echo", "aviation", "baby", "role", "surround", "incapable",
"refuse", "reliable", "imperial", "outer", "liability", "struggle", "harsh", "coerce",
"front", "strike", "rage", "casualty", "artist", "ex", "transaction", "parking", "plug",
"formulate", "press", "kettle", "export", "hiccup", "stem", "exception", "report", "central",
"cancer", "volunteer", "professional", "teacher", "relax", "trip", "fountain", "effect",
"news", "mark", "romantic", "policy", "contemporary", "conglomerate", "cotton", "happen",
"contempt", "joystick", "champagne", "vegetation", "bat", "cylinder", "classify", "even",
"surgeon", "slip", "private", "fox", "gravity", "aspect", "hypnothize", "generate",
"miserable", "breakin", "love", "chest", "split", "coach", "pound", "sharp", "battery",
"cheap", "corpse", "hobby", "mature", "attractive", "rock"
)
fun main(args: Array<String>) {
println(getRandomWordSync())
}
fun getRandomWordSync() : String {
val index = (0..99).random()
return words[index]
}
suspend fun getRandomWord(slow: Boolean = false): String {
val index = (0..199).random()
val word = if (index < words.size) {
words[index]
} else {
"Fatal: Index out of bounds"
}
if (slow)
delay(500)
return word
}
Complete these tasks in order. If you can't complete a task, just proceed with the next one.
- Print numbers from 1 to 100 to the console, but for each number also print a random word using the function
getRandomWordSync
. E.g.
1: four
2: firm
3: shape
4: choice
5: coach
6: purple
...
100: buffalo
-
Modify your code to be a "Fizz Buzz" program. That is, print the numbers as in the previous step, but for multiples of three, print "Fizz" (instead of the random word), for multiples of five, print "Buzz" and for numbers which are both multiples of three and five, print "FizzBuzz".
-
Create a version of steps 1 and 2 using the asynchronous function,
getRandomWord
. This function returns a Promise, which resolves to a random word string. -
Add error handling to both the synchronous and asynchronous solutions (calling
getRandomWord()
will throw an error instead of returning a random word). When an error is caught, the programm should print "It shouldn't break anything!" instead of the random word, "Fizz", "Buzz" or "FizzBuzz" -
Instead of printing the console. Write the information to a file in the root of this project.
-
send your result to an HTTP endpoint (since there is no running endpoint, this part of your solution does not need to actually run)
Bonus:
- The numbers should be printed in ascending order.
- Imagine
getRandomWord
's implementation is slow and takes 500ms to complete (callgetRandomWord({ slow: true })
to emulate this). Can you make your solution run in less than 1000ms with theslow
option turned on?