[Tutor] Re: [Python-Help] practice problems

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Sat, 19 Aug 2000 13:02:35 -0700 (PDT)

On Sat, 19 Aug 2000, JamesBill wrote:

> Is there any way that, you could send me some example problems or
> practice excercises.  So that, I can try to solve them?

Hello!  You might want to ask the tutor@python.org list too about this
one too.  I'll forward the message there too, so others might give some
input on these ones.

For some of these problems, it'll be useful to know that you can turn a
string into a list of characters:

>>> list("this is a list")
['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'l', 'i', 's', 't']

and you can turn it back by using string.join().

As a disclaimer, these example problems are not my own; I just remember
hearing them, and found them cute, so here goes:

1. Let's say I have a list of words, and I want to find all the "Hebrew"
words.  We'll define a "Hebrew" word to be one without any vowels.  For
example: "xyzzy" could be considered a Hebrew word, if we don't count 'y'
as a vowel.  Can you write a program to tell the user if a word has the
Hebrew nature or not?

2. Have you heard of ROT13?  It's a very funny encryption scheme that
scrambles a word in a Caesar-cypher style.  Each letter of a word will be
"rotated" by 13, that is:

a -> n
b -> o
m -> z
n -> a
o -> b

For example:

>> rot13("foobar")
's b b o n e'

>>> rot13("alphatransparency")
'n y c u n g e n a f c n e r a p l'

(Hmm... I forgot to tell string.join() not to use spaces in joining.  

Can you write a program that will give back the ROT13 of a word?  What's
also neat about this problem is that if you ROT13 something twice, you'll
get back your original word.

>>> rot13("sbbone")
'f o o b a r'

Also, ROT13 is used in USENET newsgroups a lot, when sensitive material is
being expressed.  The ord() and chr() functions might be useful in solving
this problem.

3.  This one is challenging: There's a classical CS problem called the
anagram solver.  Let me see if I can paraphrase the question properly.

First, I'd better say what an anagram is, just for clarity: an anagram is
a rearrangement of the letter of a word, so that it too is a word.  I have
examples of anagrams below.

Let's say that you're given a list of dictionary words, one per line.  
Also, let's say that you'd like to find all of the anagrams of a single
word.  Your program should lookup the words in the dictionary, and show
which words are anagrams.  For example, here are a few anagrams:

  liens, lines
  defrost, frosted
  earls, lares, laser, reals
  pares, parse, pears, rapes, reaps, spare, spear
  drawer, redraw, rewar, warder, warred
  alger, glare, lager, large, regal

I don't know, I've always found this program fun, because it has nothing
to do with math, and once you figure out how to do it, it's not hard.  
There is a direct way to solve this, but you'll find that it will go too
slow if the dictionary is huge.

There's a very nice way of solving this problem, and it involves using
sort().  If you need a hint, then email us again.

Good luck!