Photo credit: DuBoix from morguefile.com
Whaddup, Python people. My first homework assignment for MIT’s Introduction to Computer Science and Programming Using Python was due today, testing our knowledge of simple algorithms (“for” and “while” loops). Spoiler: it was hard, and I cried. Also, Python has a funny way of ranking the English alphabet.
The first two homework questions were similar to problems I have encountered before:
1. Given a string of letters, count the vowels (assume all are lowercase). Here’s one answer:
Number_of_vowels = 0 for letter in phrase: if letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u": Number_of_vowels += 1 print "Number of vowels: " + str(Number_of_vowels)
CHECK. Yeah, vowels. What this is saying is “go through the sentence letter by letter and if the letter is a vowel, add it to our vowel count. Then print that.” Easy.
2. Given a string of letters, count the “bob”s. For example, if phrase = ‘azcbobobegghakl’, then bob occurs twice.
Ok, this one had me hating people named Bob for a good long while. So while I know how to tell the computer to go through letter by letter and check for a match, I couldn’t figure out how to tell it “ok check the letter, if it’s a b, check the next letter, and then hold on!! if that’s an o, then check the next letter…. it’s right there, right next to this one… computer? are you still with me?” (No, the computer has already run your program and has moved on and is now updating QuickTime for the umpteenth time.)
But I figured it out, with some help from the internet, and splicing.
Pattern = "bob" Number_of_bobs = 0 iteration = 0 for letter in phrase: if letter == "b": if Pattern in phrase[iteration:iteration+3]: Number_of_bobs += 1 iteration += 1 print "Number of bob: " + str(Number_of_bobs)
So that iteration:+3 thingy is a splice, and it is basically doing what I described above. It’s saying, ok, let’s keep track of how many times we go through this loop, and if you see the pattern we want in the letters immediately following this iteration, that’s a bob! Stick him with the other bobs, and call it a day.
This is so useful, right, I know. Stick with me. OK. Question #3, the infamous alphabetical substrings question.
3. Write a program that prints the longest substring in which letters occur in alphabetical order. For example, if phrase = 'azcbobobegghakl'
, then your program should print “Longest substring in alphabetical order is: beggh”
Ok, so this is more complicated for several reasons, which I will attempt to explain thusly: it’s one thing to tell the program “look and see if ‘bob’ exists”; it’s another to ask it to determine “does o follow b in the alphabet? ok then what about the next letter?” I was trying to assign letters to numbers and going down a path all kinds of over-complicated, and then I discovered this:
"a" > "b"
Out[3]: False
"a" < "b"
Out[4]: True
Python already knows the alphabet, and has assigned a hierarchy where each letter is greater than the one that comes before.
Here I am trying to teach #Python about string “alphabet” and it’s like “yeah, we’ve read it — the ending is SO GOOD” #mitx6001x
— Mary Dickson (@marythought) January 19, 2015
The poet in me takes all sorts of issues with this logic.
Even knowing that I could use a < b, I still had to cheat a bit to get a right answer. But that’s ok, because after I studied the answer I deleted the whole thing and made myself do it from scratch. And then again, an hour later. And tomorrow I’ll do that again, and I’m sure it will be Greek. Because this may very well be the first class I’ve taken as an adult where it’s not about the grade or the pass/fail, but learning the content in a meaningful way. Progress! Slow, but progress!
Leave a Reply