[Courses] [python] Lesson 3: Fun with Strings and Lists
Monique Y. Mudama
monique at bounceswoosh.org
Wed Jul 6 21:00:24 UTC 2011
On Fri, Jul 1 at 21:57, Akkana Peck penned:
> [Trying re-sending this -- sorry if you get two copies, but the
> first one doesn't seem to have shown up.]
It showed up several hours later in my inbox, so I guess the mail
server was probably just taking a nap ...
> ================== Homework ======================
>
> 1. How would you count the number of words in a single string?
> Assume words are separated by spaces ... don't worry about
> things like newlines, commas or hyphens.
foo = "I have four words"
len(foo.split(" "))
> 2. What does an index of [-1], or another negative number mean in a
> list or string? Take a guess, then try it and see if you were right.
I actually had to use this for some regression tests I'm rewriting in
python (way better than batch files, which were my original proof of
concept implementation - don't ask), so I know the answer. It returns
the last item in the list (or, I suppose, last character in the string.)
More generally, it counts from the end of the list/string, starting at
-1 and decrementing by 1, rather than from the start.
> 3. Who is Guido van Rossum and why am I using him as an example?
*googles* The answer to both questions is the same - he's the creator
of Python.
> 4. Rewrite the exercise from lesson 2, the one where
> you printed "one", "two", "three", "four", "five",
> using a list instead of a series of if-elif.
> (A few people already posted solutions that worked that way
> in their lesson 2 answers. If you already did this for lesson 2, no
> need to do it again. If you read other people's, it's still worth
> writing it yourself now without going back and looking.)
numbers = ["one", "two", "three", "four", "five"]
for n in numbers:
print n
> 5. This one's a little harder, but give it a try if you have time.
> Plot a histogram graph from a list of numbers, with each number in
> the list on its own line.
>
> For instance, if you start with numbers like this:
> vals = [ 0, 2, 4, 8, 16, 18, 17, 14, 9, 7, 4, 2, 1 ]
> you might plot something like this:
>
> **
> ****
> ********
> ****************
> ******************
> *****************
> **************
> *********
> *******
> ****
> **
> *
> where the first line has no stars, the second has two, then 4, etc.
> Hint: you'll need two loops, one inside the other.
You mentioned that python is pretty smart about strings, so I tried
this, and it worked:
for v in vals:
print v*"*"
But since you mentioned nested loops, I went back and did it again.
It's a good thing, too, because apparently in the entire *week* since
the last lesson, I'd totally forgotten about range! But I still wasn't
able to mimic your solution without resorting to outside research,
because even a print with a comma after it still prints a space. So I
googled and used stdout instead, which allowed me to print without
spaces between the asterisks.
import sys
vals = [ 0, 2, 4, 8, 16, 18, 17, 14, 9, 7, 4, 2, 1 ]
for v in vals:
for n in range(0,v):
sys.stdout.write("*")
sys.stdout.write("\n")
... and then I re-read your lesson one more time, and saw the +=,
and the lightbulb finally turned on.
for v in vals:
bar = ""
for n in range(0,v):
bar += "*"
print bar
Thank you, again, for doing this class! With that histogram exercise,
especially, I can see how you are giving us homework that encourages us
to use previous lessons in the current homework, without hitting us over
the head with it. That's a delicate balance =)
--
monique
More information about the Courses
mailing list