[Tutor] Sys.argv read parameters

Danny Yoo dyoo at hashcollision.org
Wed Apr 17 22:49:07 CEST 2013


Wait.  If the solution that we're stopping at to use a hashtable here,
that's not quite right. A good solution to this should be _much_
shorter, on the order of a one-liner.  Hashtables are great, but
they're not the answer to everything.


If we're doing something like:

    a -> "This is A"
    b -> "This is B"
    ...
    z -> "This is Z"

a good solution to this isn't to make a hashtable with 26 entries, and
do a lookup: the approach is more something like this:


###############################
def getMessage(letter):
    return "This is " + letter.upper()
###############################

For example:

#################
>>> getMessage('a')
'This is A'
>>> getMessage('b')
'This is B'
>>> getMessage('z')
'This is Z'
#################


The size of your program should be proportional to how "differently"
it has to act on input.  In this case, the program is pretty much the
same on all inputs, modulo the very last chunk of the message.  That's
why thinking about this in terms of functions is crucial: if you
don't, the code ends up being larger than it deserves.


More information about the Tutor mailing list