Reverse the file

-5

0

I remember this was exam question long long long time ago.

There is a file say MyFile.txt, it can be any size and contains aphanumeric only

your task is to revese the words example : "Go beyond the traditional laptop with Surface Laptop. It's backed by the best of Microsoft, including Windows and Office"

It should be "Office and Windows including Microsoft, of best the by backed It's Laptop. Surface with laptop traditional the beyond Go"

The size of the text in the file is unknown.

asmgx

Posted 2017-06-06T23:06:14.893

Reputation: 629

Question was closed 2017-06-06T23:23:23.197

This is a file, the file size is unknown, you cant find the length of the string in the file. – asmgx – 2017-06-06T23:11:34.873

I think you mean alphanumeric and spaces? – Jonathan Allan – 2017-06-06T23:36:03.270

This is tagged code challenge but no winning criteria is given – caird coinheringaahing – 2017-07-01T09:34:56.240

Answers

5

Python 3, 77 71 47 43 bytes

print(" ".join(open("f").read().split()[::-1]))

EDIT: Saved 6 bytes thanks to NoOneIsHere.
EDIT: Saved another 24 thanks to Jonathan Allan.

LyricLy

Posted 2017-06-06T23:06:14.893

Reputation: 3 313

Why do you need f = f.read() if you only use f once? Why not with open("MyFile.txt") as f:fprint(" ".join(f.read().split(" ")[::-1])) – NoOneIsHere – 2017-06-06T23:22:22.393

@NoOneIsHere I tried that before and it didn't work, probably just a typo. Thanks! – LyricLy – 2017-06-06T23:24:59.020

You can remove the " " from the split() call as it defaults to splitting on spaces. – totallyhuman – 2017-06-06T23:30:45.380

print(" ".join(open("MyFile.txt").read().split()[::-1])) saves 12 bytes. Also the question only gives "Myfile.txt" as an example, you could assume the name is just "f" and save another 9. – Jonathan Allan – 2017-06-06T23:39:01.243

@NoOneIsHere I don't think Reversal exists on PPCG. – LyricLy – 2017-10-05T07:45:33.410

@LyricLy https://codegolf.stackexchange.com/help/badges/50/reversal

– NoOneIsHere – 2017-10-05T17:34:27.340