I suppose most Pythonistas would rather prefer making rtoa a class variable and using roman_to_dec as a method. TIMTOWTDI. ;-)
Ha ha! Yes, despite the Zen of Python
aiming for "There should be one -- and preferably only one—obvious way to do it" I could
not help but notice when playing code golf in multiple languages that Python sometimes
out-TMTOWTDI-ed them all!
Also, without TMTOWTDI code golf is no fun at all and, surprisingly, code golf is way more popular in Python than Perl nowadays.
The TMTOWTDI examples below were taken from:
Consider how to create a "Dear John" string in each of the
four languages:
"Dear $name" # Perl and PHP
"Dear %s" % expr # Python and Ruby % printf-like operator
"Dear {0}".format(expr) # Python format string method
"Dear "+`expr` # Python backticks (TMTOWTDI)
"Dear #{expr}" # Ruby string interpolation
"Dear @{[expr]}" # Perl "Baby Cart" string interpolation
Curiously, Python is the only member of the gang of four languages to
allow you to reverse the order of the two string multiply operands:
5 * "X" also produces "XXXXX" in Python! TMTOWTDI! :)
5 * "X" ... but not in Ruby (won't compile: type error)
5 x "X" ... or Perl (produces empty string)
That is, the string multiply operator is commutative in Python,
but not in Perl or Ruby.
This language idiosyncrasy makes string multiply based solutions
most attractive in Python. To illustrate, note these code snippets
from my string multiply based solutions to this game:
$"x(318%$_/9) Perl
" "*(318%i/9) Ruby
318%i/9*" " Python
This is a very rare example of Python out-golfing both Perl and Ruby.
From my early 195-stroke function-based Python solution:
n=99
z=lambda:`n or 99`+" bottle"+"s of beer on the wall"[n==1:]
while n:y=z();n-=1;print"%s, %s.\n"*2%(y,y[:-12],n and"Take one down a
+nd pass it around"or"Go to the store and buy some more",z())
notice the expression:
n and"Take one down and pass it around"or"Go to the store and buy some
+ more"
Shortening the two strings above to "Take" and "Go to"
for clarity, let's consider the many and varied ways
of doing this in Python (TMTOWTDI):
"Take"if n else"Go to"
(n>0)*"Take"or"Go to"
["Go to","Take"][n>0]
("Go to","Take")[n>0]
n and"Take"or"Go to"
"GToa kteo"[n>0::2] # "Slice and Dice" wins this golf!
As you can see, I missed the winning Python "Slice and Dice"
tactical trick in this game.
|