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.

  • \$\begingroup\$ This is a file, the file size is unknown, you cant find the length of the string in the file. \$\endgroup\$ – asmgx Jun 6 '17 at 23:11
  • \$\begingroup\$ I think you mean alphanumeric and spaces? \$\endgroup\$ – Jonathan Allan Jun 6 '17 at 23:36
  • \$\begingroup\$ This is tagged code challenge but no winning criteria is given \$\endgroup\$ – caird coinheringaahing Jul 1 '17 at 9:34

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.

| improve this answer | |
  • \$\begingroup\$ 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])) \$\endgroup\$ – NoOneIsHere Jun 6 '17 at 23:22
  • \$\begingroup\$ @NoOneIsHere I tried that before and it didn't work, probably just a typo. Thanks! \$\endgroup\$ – LyricLy Jun 6 '17 at 23:24
  • \$\begingroup\$ You can remove the " " from the split() call as it defaults to splitting on spaces. \$\endgroup\$ – totallyhuman Jun 6 '17 at 23:30
  • \$\begingroup\$ 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. \$\endgroup\$ – Jonathan Allan Jun 6 '17 at 23:39
  • \$\begingroup\$ @NoOneIsHere I don't think Reversal exists on PPCG. \$\endgroup\$ – LyricLy Oct 5 '17 at 7:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.