HELP Newbie solve this Problem - One-Liners

Dennis E. Hamilton infonuovo at email.com
Sun Mar 26 12:03:17 EST 2000


That's the spirit!!  Next we can have people send us their APL, Scheme, and
Haskell counter-solutions.

It was painful to play with this as a one-line file.  My old MS-DOS tools
held up much better than I expected.

1.  On Windows 98 I replaced open('data?') with open(input("Enter 'file
name': ")).   After getting the parentheses right and remembering to quote
the filename in responding to the prompt, it executed without any
exceptions.  I successfully ran NewbieWanKanobie2.py on the text of your
e-mail message, saved in NewbieWanKanobie.txt.

2.  Since I wasn't about to check whether the results were accurate, rather
than merely plausible, I made a single-line text file (sticking to the
spirit of this approach),

abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ

and obtained an incorrect result.  Case is not being folded properly.  I got
just the tally for the lower-case letters.

3.  My repair was to substitute

lambda letter:operator.setitem(ldict,string.lower(letter),
ldict[string.lower(letter)]+1)

as a brute-force remedy.  I didn't worry that the program is much longer
now, since my editor survived the experience.  I'll take the result and run
with it.

4.  Observations

The lovely thing about this puppy is that no one would dare turn it in as a
homework solution!  How would you like to be called to the front of the
class, as a beginner, and have to explain it?

Better yet, what if the instructor simply marks it wrong because the
instructor can't understand it as a solution.  How do you appeal?

I graded papers for Grace Hopper once.  She was teaching an introductory
course of some sort at the Wharton School and assigned some simple FORTRAN
problems (an interesting choice for her, in retrospect).  I helped one of my
colleagues, author of one of the Univac FORTRAN manuals, who had agreed to
do the grading but was a little overwhelmed.  I became chairman of the ANSI
FORTRAN committee at about that time.  One of the students turned in a
solution for finding primes that was amazing.  There were no divisions or
multiplications or use of built-in functions in the entire program.  It took
me quite a while to analyze the loops and arithmetic to verify that
divisibility was being worked out synthetically using additions and
subtractions.  I marked the paper correct (adjusted for a minor bug) and
attached a note suggesting that more-direct approaches were more likely to
be recognized as correct and acceptable in the future.

-- Dennis

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of David Fisher
Sent: Saturday, March 25, 2000 23:02
To: python-list at python.org
Subject: Re: HELP Newbie solve this Problem


[ ... ]

Well if it's about lines, here's a Python solution in one line (277 chars).
Of course it's slightly harder to read.

import string,operator;ldict={};map(lambda
letter:operator.setitem(ldict,letter,0),map(chr,range(256)));map(lambda
letter:operator.setitem(ldict,letter,ldict[letter]+1),open('data?').read());
print string.join(map(lambda letter:letter+'
'+`ldict[letter]`,string.lowercase),'\n')

This won't work from the Python command line, it has to be run from a
script.  And of course a filename can't have a '?' on a windows box, so it
won't work there either.

This is my first use of lambda (outside of scheme), my mom would be so
proud. :)

dnf






--
http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list