home |
copyright ©2016, tim@menzies.us
overview |
syllabus |
src |
submit |
chat
1a. The following code has a bug. The countdown stops at 10 (no 9,8,7...). Why?
def countdown(n):
while n >= 0:
return n
n -= 1
print("We are go for launch")
for x in countdown(10):
print(x)
print("lift off!")
1b. Modify the following code such that the final out
list
only contains numbers over 20
def items(x, depth=-1):
if isinstance(x,(list,tuple)):
for y in x:
for z in items(y, depth+1):
yield z
else:
yield x
out = []
for x in items( [10,[ 20,30],
40,
[ ( 50,60,70),
[ 80,90,100],110]]):
out += [x]
print out
1c. Repeat the above, this time using list comprehensions.
1d. Using list comprehensions, write a function that returns only non-whitespace in a string. Hint:
import string
string.whitespace # <== contains all whitespace chars
1e. Using list comprehensions and the following code,
return all lines in a multi-line
strings that are (a) non-blanks and (b) longer than 20
characters in length. Hints: not str
returns True
for non empty strings.
def lines(string):
tmp=''
for ch in string:
if ch == "\n":
yield tmp
tmp = ''
else:
tmp += ch
if tmp:
yield tmp
1f. Whats the logical error in the code? I need a list of 20 random numbers
import random
x = []
for _ in range(20):
random.seed(1)
x.append(random.random())
print(x)
Correct it by moving 1 line.
1g. Write a small snippet for a random odd integer generator between 1 and 1000. (Hint: yield)
2a. What are the dunders in the following code? For each one, use them in a code snippet.
class o:
def __init__(i,**d) : i.__dict__.update(d)
def __setitem__(i,k,v) : i.__dict__[k] = v
def __getitem__(i,k) : return i.__dict__[k]
def __repr__(i) : return 'o'+str(i.__dict__)
2b. In the above, what is the magic dict variable?
2c. What would happen if the last line in the following __iadd__
method
was deleted?
r = random.random
rseed = random.seed
class Some:
def __init__(i, max=8):
i.n, i.any, i.max = 0,[],max
def __iadd__(i,x):
i.n += 1
now = len(i.any)
if now < i.max:
i.any += [x]
elif r() <= now/i.n:
i.any[ int(r() * now) ]= x
return i
2d. In English, explain what the above Some
class does. Use it in a loop
to keep Some
numbers in the series 0,1,2,...999.
You've seen the knightstour. Can you code up the 8 queens problem in python? (https://en.wikipedia.org/wiki/Eight_queens_puzzle)
What is a finite state machine? Can you describe an fsm in python for the following problem. You're in a bar.
- You start of sober.
- If you are sober you take a drink
- If you are drunk and take a drink then there is a 80% chance you get drunk and there is a 20% chance you pass out.
- If you are drunk and do not take a drink, there is a 50% chance you get sober.
- If you pass out you obviously stop drinking