On Thu, May 2, 2013 at 11:42 PM, Andy Bradford <[email protected]> wrote: > Thus said Michael Torrie on Thu, 02 May 2013 22:13:33 -0600: > >> Python version, coded in 1 minutes, well an extra minute to run the >> REPL to make sure I had the slice syntax correct: > > Except it doesn't produce the same output: > > $ time ./strrev words > words.strrev > 0m0.11s real 0m0.11s user 0m0.00s system > $ time python mt.py < words > words.mt.py > 0m0.61s real 0m0.59s user 0m0.00s system > $ md5 words.mt.py words.strrev > MD5 (words.mt.py) = 25aa34ae2d9a16f3556233ca4e7593be > MD5 (words.strrev) = 9c75f40e4cdd3dfa9f9946eeb2c6010b > > To correct this you need something like: > > import sys > > for x in map(str.strip,sys.stdin): > print x[::-1] > > $ time python mta.py < words > words.mta.py > 0m0.81s real 0m0.77s user 0m0.04s system > $ md5 words.mta.py words.strrev words.mt.py > MD5 (words.mta.py) = 9c75f40e4cdd3dfa9f9946eeb2c6010b > MD5 (words.strrev) = 9c75f40e4cdd3dfa9f9946eeb2c6010b > MD5 (words.mt.py) = 25aa34ae2d9a16f3556233ca4e7593be >
You couldn't make exact output a requirement, since Sasha says you can optionally truncate long lines. This is really an extremely pointless exercise, though. Your solution does bring to mind an interesting thing that Haskell can do, though. There was an avenue of research that got a bit of buzz in the Haskell community a few years ago regarding identifying and classifying patterns of recursion. They'd find the patterns, give them funny names (catamorphism, anamorphism, paramorphism, etc.) and give equational properties that they have which allow you to re-write them to fuse multiple recursive operations together. Some of these rewrite rules got implemented in Haskell, so a function that filtered a list and then folded it could be transformed automatically such that the list only had to be traversed a single time. The rewrite rules are actually implemented at the library level rather than the compiler level. /* PLUG: http://plug.org, #utah on irc.freenode.net Unsubscribe: http://plug.org/mailman/options/plug Don't fear the penguin. */
