4

I am doing the 30 Days of Code in Kotlin on Hackerrank and I am stuck at Day 7.

How do you read multiple integers on a single line?

How is it added to an array and displayed in reverse?

I have solved it in Java but lack the syntax needed in Kotlin

Input:

4

1 4 3 2

My Code:

fun main(args: Array<String>) { val n = readLine()!!.toInt() var arr = Array(n) for(i in 0 until n) { arr[i] = readLine()!!.toInt() //Not Working? nor does readLine()!!.split(' ').toInt() } for(item in arr.size - 1 downTo 0) { print("${item} ") } } 

4 Answers 4

5

EDIT: question was updated from the original

The problem is the readLine() will read the entire line from stdin, so each time you call readLine() in the for loop it will result in a separate line being read each time.

One approach to this is to read the line, and then to split and map each value to an Int.

readLine()?.let { val numOfValues = it.toInt() println(numOfValues) readLine()?.let { line -> line.split(" ").map { it.toInt() }.reversed().forEach { println(it) } } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I should have clarified the first digit is the number of inputs in the array and the digits afterwards are in the array. Is there something akin to read() in Kotlin that only reads the next value?
@TadhgDeeney I've updated my answer with the additional parts of your question: the initial size line, and printing the reverse. It's not clear from the question why the initial line is needed for the size, is the Hackerrank challenge more specific about that?
Is there something akin to read() in Kotlin that only reads the next value? - you can use stdin.read(), which will read a single byte, but given the constraints provided in your question it would be more complicated than necessary.
3

If you want to store them in a list then you can follow this method

var items = readLine()!!.trim().split("\\s+".toRegex()).map (String::toInt) println(items) 

You can also store them in different variables like this way

var (a,b) = readLine()!!.trim().split("\\s+".toRegex()).map (String::toInt) println(a+b) 

Comments

2

You can also use the following code to item items splited and stored in array for a beginner approach

fun main(ags :Array<String>) { var item = readLine()!!.trim() println(item[0]) } 

Comments

0

Actually, you can refer to the official Kotlin tutorial: https://kotlinlang.org/docs/tutorials/competitive-programming.html

as mentioned in tutorial:

To make reading the input in competitive programming tasks like this more concise, you can have the following list of helper input-reading functions:

private fun readLn() = readLine()!! // string line private fun readInt() = readLn().toInt() // single int private fun readStrings() = readLn().split(" ") // list of strings private fun readInts() = readStrings().map { it.toInt() } // list of ints 

for your case, you can try use as below:

fun main() { val n = readInt() val x = readInts() for (j in x.reversed()) { print(j); print(" ") } println() } private fun readLn() = readLine()!! // string line private fun readInt() = readLn().toInt() // single int private fun readStrings() = readLn().split(" ") // list of strings private fun readInts() = readStrings().map { it.toInt() } // list of ints 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.