...
| Code Block |
|---|
def rshift(x): return x >> 1 |
Iterators
- Write an iterator class that, given a string via the constructor, returns a sequence of characters. It should return 1 character for each next() invocation. Tested out with a for loop that prints out the characters.
...
Assume the following data:
| Code Block |
|---|
L = [1143,3,5,7,998,4] names = ['parrt', 'ksb', 'tombu'] |
- Using a list comprehension, create an array containing the even numbers between 1 and 14 inclusive.
Generators
Higher-order functions
...
- words from names array whose length is greater than 3.
- Create an array containing all uppercase versions of the elements in names array.
- Create an array of tuples with the upper and lower case versions like [('PARRT','parrt'), ('KSB','ksb'), ...]
- Create a list of tuples containing the index of the name and the name from names like [(1, 'parrt'), ...]. You will need the enumerate(names) function that returns a sequence of index,value pairs.
Generators
- Create a generator function that recursively walks a list (it does not as we normally would iterate through it... you must use recursion) and yields the elements one by one. the code to do the recursive walk exists in the lecture notes but you must convert it to a generator function.
| Code Block |
|---|
def walk(a): # walk list a recursively
... |
Higher-order functions
- Write your own map(fun,list) function that takes a function and a list as arguments and then returns a list with fun applied to each element of list.
| Code Block |
|---|
def map(fun, list):
... |
Write a function const(x) that returns a (nested) function that always returns x.
| Code Block |
|---|
def const(x):
...
one = const(1)
print one() # 1
print one() # 1
two = const(2)
print two() # 2 |