a better way to reverse input order | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

a better way to reverse input order

It is hard to describe this in title. I have a question that if i input "A B"(both integer and separate by a space), how can I output like "B A"?

24th Feb 2019, 3:07 PM
Zheng-Bin Weng
Zheng-Bin Weng - avatar
12 Answers
+ 5
Zheng-Bin Weng Java: Scanner scan = new Scanner (System.in); int x = scan.nextInt (); int y = scan.nextInt (); System.out.println (y + " " + x);
24th Feb 2019, 4:04 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
Why would my memory requirement grow tremendously when I read 1000 numbers? Handled as chars, 1000 numbers will take exactly twice the memory as 500 characters and 10,000 characters will take ten times as much. And it will still be a negligible amount of memory. A couple thousand characters is virtually nothing. A maximum size of 2^64 characters would be more than 700 billion times (!) more than you need to store a 25 million digit string. I don't think they'd implement a maxium of 2^64 characters when in real life you can't even use 0.0000000000001 % of it without running out of memory...
25th Feb 2019, 12:59 PM
Anna
Anna - avatar
+ 4
I am not sure if this is an improvement on memory usage, but my idea here is to consume the input string lazily, by using a generator function. So I tried to save the first string in a temp file, then print out the second string from the generator buffer, finally open the file and print the first value. Well it works with "hello world" but I am not sure about really large data... https://code.sololearn.com/cQ2Df16g95j6/#py Another possibility may be to take advantage of the fact that the input values are actually numbers. So maybe encoding them to hex or something, could save some memory if they are handled as numeric representations rather than strings.
25th Feb 2019, 2:49 PM
Tibor Santa
Tibor Santa - avatar
+ 3
What do you mean with reversed input? int x = 12 int y = 35 Output: 35 12? Or output 53 21?
24th Feb 2019, 3:27 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Python oneliner: print(' '.join(input().split()[::-1]))
24th Feb 2019, 4:52 PM
Tibor Santa
Tibor Santa - avatar
+ 3
This one could have been written much better, but it should be almost impossible to exceed the memory limit https://code.sololearn.com/cuU1ry8P07Qb/?ref=app
24th Feb 2019, 4:56 PM
Anna
Anna - avatar
+ 3
~ swim ~ The code handles the input as string. One char = 1 byte, 25 million chars = less than 24 MB memory
25th Feb 2019, 12:01 PM
Anna
Anna - avatar
+ 2
s = '123 456' i = s.index(' ') print(s[i+1:], s[:i])
24th Feb 2019, 6:22 PM
Anna
Anna - avatar
+ 2
import re s = '123 456' t = re.findall(r'\d+', s) print(*reversed(t))
24th Feb 2019, 6:26 PM
Anna
Anna - avatar
+ 1
ok, here's the example input: 123 567 output: 567 123 The problem I encountered is the input is too long that even using string might exceed the memory limit. p.s. the input may contain 25000000 digits per integer input.
24th Feb 2019, 3:58 PM
Zheng-Bin Weng
Zheng-Bin Weng - avatar
+ 1
I've tried all method you mentioned, and they seems not usable to my problem. They returned Memory Error on the online judge. For a error example: ----------------------------------------------------------------------------- Traceback (most recent call last): File "/4539645/code_4539645.py", line 1, in print(' '.join(input().split()[::-1])) File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] MemoryError ----------------------------------------------------------------------------- Actually, I'd like to know: Is there any method that I can skip first input temporarily to output second input, and then back to output first input. p.s. I thing I haven't mentioned: there is nothing (like '\n) in the end of the input. That really a challenge for me, thank you for helping me!
24th Feb 2019, 6:14 PM
Zheng-Bin Weng
Zheng-Bin Weng - avatar
24th Feb 2019, 3:28 PM
HonFu
HonFu - avatar