ex25.py:
1:
2:
3:
4: # if it weren't for emacs' tab auto-indent I would have forgotten the colon
5: # on every one of these defs
6:
7: def line2words(line):
8: words = line.split(' ')
9: return words
10:
11: # sorted() takes an array and sorts
12:
13: def print_head(words):
14: print words.pop(0)
15:
16: def print_arse(words):
17: print words.pop(-1)
18:
19: def sort_line(line):
20: return sorted(line2words(line))
21:
22: def print_first_n_last(line):
23: ws = line2words(line)
24: print_head(ws)
25: print_arse(ws)
26:
27: def print_top_n_bot(line):
28: ws = sort_line(line)
29: print_head(ws)
30: print_arse(ws)
31:
Testing ex25 with the Python interpreter:
1: slithgow@holly:~/python$python
2: Python 2.6.4 (r264:75706, Mar 24 2010, 10:35:01)
3: [GCC 4.0.2] on sunos5
4: Type "help", "copyright", "credits" or "license" for more information.
5: >>> import ex25
6: >>> l1 = "All good things come to those who wait."
7: >>> words = ex25.line2words(sentence)
8: Traceback (most recent call last):
9: File "<stdin>", line 1, in <module>
10: NameError: name 'sentence' is not defined
11: >>> words = ex25.line2words(l1)
12: >>> ex25.print_head(words)
13: All
14: >>> ex25.print_top_n_bot(l1)
15: All
16: who
17: >>> ex25.print_first_n_last(l1)
18: All
19: wait.
20: >>>
Oooh.... ex26 is a debugging task/test.... excellent. This really is a very good tutorial. Possibly approaching LYAHFGG (http://learnyouahaskell.com/) in quality. Which is pretty much the highest compliment I can give to any other tutorial.
The best way to do the test is to just copy-paste the file (http://learnpythonthehardway.org/exercise26.txt) and try to run it... we'll get errors - errors that have been deliberately selected as ones we're likely to make (even once we know what we're doing).
1:File "ex26.py", line 18
2: print word
3: ^
4: SyntaxError: invalid syntax
Note that not only does it give you the line - it also tells you where the error is. If the python interpreter is like most programming languages then it is extremely valuable to learn when it does this accurately and when it does not (hint: this is one of those times). Becoming familiar with a compiler/interpreter is key to debugging. And debugging is almost certainly what we'll be spending most of our time on.First pass through the file, you'll get all the syntax errors. It can't even parse the file if for example, brackets don't match up. Then it will try to run the file, and there should be more errors - the kind it runs into when its running aka runtime errors. The first load will be ones that make little sense - they were basically typos that didnt break it enough to stop it from parsring. Then finally, you should encounter logic errors. These are the ones that you can't blame on a tiny laptop keyboard. I've never met anyone who doesn't make them sometimes (and I've met some freaking geniuses) so chill about them but they do tend to indicate a lack of understanding of what you want to be doing. Each time you get an error, you could search through the code for other instances of this (via the editor's search feature, never manually); sometimes this is wise (like a typo in a variable name that you think you might have made a few times), but usually its a good idea to fix one error, then try to recompile.
This is what I ended up with (let me know if I missed any!):
1: def break_words(stuff):
2: """This function will break up words for us."""
3: words = stuff.split(' ')
4: return words
5:
6: def sort_words(words):
7: """Sorts the words."""
8: return sorted(words)
9:
10: def print_first_word(words):
11: """Prints the first word after popping it off."""
12: word = words.pop(0)
13: print word
14:
15: def print_last_word(words):
16: """Prints the last word after popping it off."""
17: word = words.pop(-1)
18: print word
19:
20: def sort_sentence(sentence):
21: """Takes in a full sentence and returns the sorted words."""
22: words = break_words(sentence)
23: return sort_words(words)
24:
25: def print_first_and_last(sentence):
26: """Prints the first and last words of the sentence."""
27: words = break_words(sentence)
28: print_first_word(words)
29: print_last_word(words)
30:
31: def print_first_and_last_sorted(sentence):
32: """Sorts the words then prints the first and last one."""
33: words = sort_sentence(sentence)
34: print_first_word(words)
35: print_last_word(words)
36:
37:
38: print "Let's practice everything."
39: print 'You\'d need to know \'bout \\s that do \n \\ns and \t \\ts.'
40:
41: poem = """
42: \tThe lovely world
43: with logic so firmly planted
44: cannot discern \n the needs of love
45: nor comprehend passion from intuition
46: and requires an explantion
47: \n\t\twhere there is none.
48: """
49:
50:
51: print "--------------"
52: print poem
53: print "--------------"
54:
55: five = 10 - 2 + 3 - 5 - 1
56: print "This should be five: %s" % five
57:
58: def secret_formula(started):
59: jelly_beans = started * 500
60: jars = jelly_beans / 1000
61: crates = jars / 100
62: return jelly_beans, jars, crates
63:
64:
65: start_point = 10000
66: beans, jars, crates = secret_formula(start_point)
67:
68: print "With a starting point of: %d" % start_point
69: print "We'd have %d jellybeans, %d jars, & %d crates." % (beans, jars, crates)
70:
71: start_point = start_point / 10
72:
73: print "We can also do that this way:"
74: print "We'd have %d beans, %d jars, & %d crates" % secret_formula(start_point)
75:
76: sentence = "All good\tthings come to those who wait."
77: # finding typos in strings is sometimes important to change, sometimes not.
78: # I cbf checking the poem for example, everywhere else I'll check
79:
80: words = break_words(sentence)
81: sorted_words = sort_words(words)
82:
83: print_first_word(words)
84: print_last_word(words)
85: print_first_word(sorted_words)
86: print_last_word(sorted_words)
87: sorted_words = sort_sentence(sentence)
88: print "%s" % ' '.join(sorted_words) # <- this arguably isnt an error.
89: # usually though, keep internal representations
90: # away from end users, they'll typically only hurt themselves.
91:
92: print_first_and_last(sentence)
93: print_first_and_last_sorted(sentence)
94: