Skip to content

Commit c1a7b67

Browse files
committed
added solutions
1 parent 99eb8d4 commit c1a7b67

30 files changed

+458
-91
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
# pydebug
2-
a small set of python sketches that do not run as expected and wait to be debugged.
2+
a small set of python sketches that do not run as expected and wait to be
3+
debugged.
34

45
used as exercises for
6+
57
![power.coders](powercoders-cropped.png "power.coders")
68

7-
good luck!
9+
the idea of these buggy sketches is to learn to read python's traceback. python
10+
will tell you very precisely why and also where it is unhappy with your code.
11+
one of the goals of these snippets is to learn to read the traceback and then
12+
fix the code.
13+
14+
15+
good luck!

src/01.py problems/01.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
'''
44
this code is buggy. make it run!
5+
6+
7+
it is supposed to print number from 0 up to (and without) 10 and then print
8+
if that number is odd or even.
59
'''
610

711
for i in range(10):
812
print(i)
9-
if i%2 == 0
13+
if i % 2 == 0
1014
print('even!')
1115
else
1216
print('odd')

src/02.py problems/02.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
'''
44
again, buggy code. can you guess what it should do and fix?
5+
6+
7+
first we want to print out the value of the key 'b'; then we want to print
8+
out all the values (1, 2, 3).
59
'''
610

711
dct = {'a': 1, 'b': 2, 'c': 3}

src/03.py problems/03.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env python3
22

33
'''
4-
bugs.
4+
bugs again.
5+
6+
this should print the user name given in the command line. but it does not.
7+
please fix!
58
'''
69

710
user = input('user name please: ')

src/04.py problems/04.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
'''
44
again, please fix!
5+
6+
first we want to print out the last element of lst.
7+
8+
then we want to print the last 5 elements.
59
'''
610

711
lst = list(range(10))

problems/05.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
do *not* run this! instead, try to find out what it does without running.
5+
6+
*then* run and see if you were correct.
7+
8+
there is not really a fix for this; just remove or comment out the offending
9+
line.
10+
'''
11+
12+
13+
def f():
14+
a = 5
15+
print(a)
16+
17+
f()
18+
print(a)

problems/06.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
we want to remove all integers other than 5 from a list.
5+
6+
run the code, be amazed and try to come up with a solution.
7+
8+
don't hurt yourself too much if you can't make this one work. go have a look
9+
at the solution once you have exhausted all your ideas.
10+
'''
11+
12+
lst = list(range(10))
13+
14+
for n in lst:
15+
if n != 5:
16+
lst.remove(n)
17+
18+
# this should print a list containing only the number 5
19+
print(lst)

src/07.py problems/07.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
'''
44
why is nothing happening? why is there no error?
5+
6+
we want to call function...
57
'''
68

79

problems/08.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
just fix please. take a *close* look at the traceback. you will see this a lot!
5+
'''
6+
7+
variable = 'variable'
8+
9+
print(varaible)

src/09.py problems/09.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22

33
'''
4-
again, look at the traceback and fix!
4+
again, look closely at the traceback and fix!
55
'''
66

77

src/10.py problems/10.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
trying to create a datetime. see the documentation:
55
https://docs.python.org/3/library/datetime.html
66
7-
why does this not work?!
7+
why does this not work?! please fix.
88
'''
99

1010
import datetime

problems/11.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
this will show you a python feature that is important to be aware of. we
5+
pretend to be ignorant of the feautre to show how it works. can you fix it for
6+
us?
7+
8+
the function f is supposed to get one argument and return the argument as
9+
only item in a list. (of course we could just have returned '[x]' or 'list(x)'
10+
but we want to use append for this one).
11+
12+
look at what is happening... can you fix that without changing the last 2 lines
13+
of f?
14+
'''
15+
16+
17+
def f(x, lst=[]):
18+
'''
19+
append x to lst.
20+
'''
21+
22+
lst.append(x) # do not modify this line
23+
return lst # do not modify this line
24+
25+
print(f(0)) # expecting [0]
26+
print(f('a')) # expecting ['a']
27+
print(f([])) # expecting [[]]

problems/12.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
now we are going to use the python feature we just learned about in the
5+
previous example to cache values of a function.
6+
7+
suppose long_running_f is a function that runs for quite some time (we simulate
8+
that here with time.sleep). assume this function is called many times but the
9+
number of different arguments is small.
10+
11+
we do not want to repeat the long calculation (or data retrival/...). so we
12+
store {argument: return_value} in a dictionary and just return the value if we
13+
have calculated it already. otherwise we calculate the value, add it to the
14+
cache and return it.
15+
16+
the code does not work as expected. please fix it!
17+
'''
18+
19+
20+
import time
21+
22+
23+
def long_running_f(x, _cache={}):
24+
"""
25+
computes the power of 2 for a number x.
26+
27+
Keeps results in memory because computing powers is expensive.
28+
"""
29+
# if the result for x is not yet in memory, we compute it
30+
if _cache[x] is None:
31+
print('- calculating value for {}'.format(x))
32+
time.sleep(2) # we preted this takes some time
33+
ret = 2 ** x
34+
_cache[x] = ret
35+
else:
36+
print('- can get value directly from cache for {}'.format(x))
37+
ret = _cache[x]
38+
39+
return ret
40+
41+
42+
for arg in (0, 1, -5, 3, 0, -3, 5, 3, 1, 2, 1, 4, 1, 4, -5, -3, 1, 5):
43+
44+
ret = long_running_f(arg)
45+
print('long_running_f({:2d}) = {}'.format(arg, ret))

solutions/01.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
the solution.
5+
6+
there was 1 space too many on the first print line.
7+
you also could have added a space before 'if' and 'else': this would have
8+
resulted in correct python code as well. but it is considered bad style to have
9+
indentations that are not multiples of 4 spaces.
10+
see: https://www.python.org/dev/peps/pep-0008/
11+
12+
then there were the ':' missing for the 'if/else' statement.
13+
'''
14+
15+
for i in range(10):
16+
print(i) # indentation was off here: 1 space too many
17+
if i % 2 == 0: # ':' was missing here
18+
print('even!')
19+
else: # ':' was missing here
20+
print('odd')

solutions/02.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
dictionaries consist of key/value pairs: {key: value, ...}.
5+
6+
value addess is done this way: dct[key] (and not dct[0] for e.g. the first
7+
element; dictionaries are unordered! the order in which you define the
8+
dictionary or the order you see when you are printing it has *no* meaning!)
9+
10+
there are 3 ways to iterate over dictionaries:
11+
12+
for key in dct: # iterate over keys
13+
print(key)
14+
15+
this is identical to:
16+
17+
for key in dct.keys(): # iterate over keys
18+
print(key)
19+
20+
21+
then you can iterate over the values:
22+
23+
for value in dct.values(): # iterate over values
24+
print(value)
25+
26+
and you can directly iterate over key/value pairs:
27+
28+
for key, value in dct.items(): # iterate over key/value pairs
29+
print(key, value)
30+
31+
details here:
32+
https://docs.python.org/3/tutorial/datastructures.html#dictionaries
33+
'''
34+
35+
dct = {'a': 1, 'b': 2, 'c': 3}
36+
37+
print("the value of key 'b' is:", dct['b'])
38+
39+
# print out the numbers:
40+
for value in dct.values():
41+
print(value)
42+
43+
# if you need them sorted, you have to state that:
44+
for value in sorted(dct.values()):
45+
print(value)

solutions/03.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
'user' is a str(ing) and not the name of a variable.
5+
'''
6+
7+
user = input('user name please: ')
8+
9+
print('got the username:', user) # removed the quotes.

solutions/04.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
lst = list(range(10)) means:
5+
lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6+
7+
list elements are accessed by their index in the list.
8+
9+
lst['10']
10+
11+
tries to access a list with a string. that will not work! and the index of the
12+
last element is not 10 but 9 (indices start at 0).
13+
14+
in general you should access the last element of a list with
15+
16+
lst[-1]
17+
18+
negative indices count from the end of the list.
19+
20+
21+
search for 'slicing' here (or find an explanation for 'slice' in python).
22+
https://docs.python.org/3/tutorial/introduction.html
23+
'''
24+
25+
lst = list(range(10))
26+
27+
print('the last element is', lst[-1])
28+
29+
# loop through last 5 elements only (should print numbers 5, 6, 7, 8, 9)
30+
for i in lst[5:]: # ':' was missing: we need a slice and not a single index.
31+
print(i)

solutions/05.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
'a' onlylives in the local namespace of 'f()' and is undefined in the global
5+
namespace.
6+
'''
7+
8+
9+
def f():
10+
a = 5
11+
print(a)
12+
13+
f()
14+
# print(a) # just comment this out. 'a' is undefined here.

solutions/06.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
take-home-message: *never* change a list (or anyghint you iterate over) while
5+
you are looping over it (unless you *really* know what you are doing). this
6+
will almost always end in tears!
7+
8+
filtering lists is most conveniently done using a list comprehension:
9+
https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions
10+
11+
'''
12+
13+
lst = list(range(10))
14+
lst = [value for value in lst if value == 5]
15+
16+
print(lst)

solutions/07.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
the function is never called. 'function' is just a reference to the function
5+
object of the function. that is valid python - the statement just does not
6+
do anything.
7+
8+
in order to call the function, you must add '()': function().
9+
'''
10+
11+
12+
def function():
13+
print('function called!')
14+
15+
function() # added '()' to *call* the function.

solutions/08.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
typo. misspelled 'variable'. did you notice how precisely python tells you
5+
what went wrong and where? have a look at the traceback again...
6+
'''
7+
8+
variable = 'variable'
9+
10+
print(variable)

0 commit comments

Comments
 (0)