Back to General discussions forum
I'm confused about how to handle input because I'm used to working with array-like syntax. But here, the input comes as plain text. Should I manually convert it into arrays, or is there a better way to handle this?
I tried using input() but couldn’t figure out how to read all the values. For example, from the input below, when I try to print the 0th index, I only get 12, and there’s nothing at index 1, 2, etc.
I also tried putting the input into a file and reading from it, but that approach didn’t feel right.
Ideally, I want to write functions for problems and pass inputs in the form of arrays and variables. But for that, I need to manually convert the text input into arrays.
What should I do now? Please share some possible solutions.
Example input: 12 488540310 -53524 -1896247503 -20985 735766001 113746 -1251968135 -124234 176926741 28514
You might find it helpful to look at other peoples' solutions to see how they go about it. If you have solved a problem, you can see solutions by others for that problem. To do so, go to the problem, and click "Who solved this?", and then you can make it show only Javascript solutions. Some others may have hardcoded the test data, or put the test data into a file, so you might have to check several solutions.
input() will read one line of text from the test data, and return a string. This function only works on the CodeAbbey site, it
isn't a standard function in Javascript (but you could implement your own input function to do what you want).
Using [] on a string gives you one character from that string at the index you give it. For example if your code
has foo = "Hello", and you do foo[0], that will give you "H".
You said when you tried to print the 0th index, you only get 12, and nothing at index 1, 2, etc. I think you misunderstood what
happened. If you did foo = input() and outputted foo[0],foo[1],foo[2] then those actually end up being "1", "2", and " " for
your example input, so it looks as if you only got "12" from foo[0], but actually it came from both foo[0] and foo[1].
Anyways, you can just do values = input().split(' '); to read in a line of input, and turn it into an array of strings.
Alternatively you can also do foo = input(); and then values = foo.split(' ');.
What split does is it gets all the substrings in the string you're splitting, separated by a delimiter (fancy word for separator).
Doing foo.split(' ') gets you an array of all the substrings in foo separated by spaces. Similarly foo.split(',')
would get you all the substrings in foo separated by commas.
One more remark: it's okay to put your input into a file and read from that. Some of the seasoned users do that as well. If you're working on a solution in an IDE, you'll probably either have to periodically copy the code from the IDE into the CodeAbbey code box to test it, or run the program in your IDE and copy paste the test data into that every execution, or you can copy the test data one time into a file and do all your testing in your IDE until you're ready to try submitting. So, if you are working on your solution in an IDE and you end up having to test your code many times, I think putting the input into a file would let you do less repetitive copy-pasting work.