Class 28 – October 27
Being a functionary can be a good thing
Riding together — Driving and navigating — Together as one
Look both ways
Paramount importance
- Code cannot be written unless both the driver and navigator are present
Agenda
- Functional chrestomathics with lists
Assignment 22 — module aid.py
Complete the implementation of four functions. Simple tester program abet.py is available to help you test your module aid
functions.
Function rotate( x )
Updates list x
by moving the last element of list x
(if any) to the beginning of list x
. This type of list transformation is often called a circular shift.
In carrying out its action, the function neither returns a new list nor prints anything — it only modify the contents of list x
in place (e.g., by list-modification methods like pop()
, append()
, and insert()
, or by directly modifying individual elements of list x
.
The following code segment from abet.py demonstrates it usage.
x1 = []
x2 = [ 3 ]
x3 = [ 3, 1, 4, 1, 5, 9 ]
x4 = [ 'p', 'e', 'c', 'u', 'l', 'a', 't', 'i', 'o', 'n', 's' ]
aid.rotate( x1 ) ; print( x1 )
aid.rotate( x2 ) ; print( x2 )
aid.rotate( x3 ) ; print( x3 )
aid.rotate( x4 ) ; print( x4 )
The segment produces as output:
[]
[3]
[9, 3, 1, 4, 1, 5]
['s', 'p', 'e', 'c', 'u', 'l', 'a', 't', 'i', 'o', 'n']
Function common( x, y )
Returns a new list whose elements are those elements in x
that are also in y
. The ordering of elements in the return list should reflect their ordering in x
. The following code segment from abet.py demonstrates it usage.
x1 = [] ; y1 = []
x2 = [ 3 ] ; y2 = []
x3 = [ 1 ] ; y3 = [ 3, 1, 4 ]
x4 = [ 2, 7, 1, 8, 2, 8, 1, 8 ] ; y4 = [ 2, 8, 4, 5, 9, ]
The segment produces as output:
[]
[]
[1]
[2, 8, 2, 8, 8]
Function rotate_k_times( x, k )
Updates list x
by performing k
circular shifts. In carrying out its action, the function neither returns a new list nor prints anything — it only modify the contents of the list x
in place. The following code segment from abet.py demonstrates it usage.
x1 = []
x2 = [ 3 ]
x3 = [ 3, 1, 4, 1, 5, 9 ]
x4 = [ 'p', 'e', 'c', 'u', 'l', 'a', 't', 'i', 'o', 'n', 's' ]
aid.rotate_k_times( x1, 2 ) ; print( x1 )
aid.rotate_k_times( x2, 3 ) ; print( x2 )
aid.rotate_k_times( x3, 4 ) ; print( x3 )
aid.rotate_k_times( x4 ,5 ) ; print( x4 )
The segment produces as output:
[]
[3]
[4, 1, 5, 9, 3, 1]
['t', 'i', 'o', 'n', 's', 'p', 'e', 'c', 'u', 'l', 'a']
To do
- Look over artifacts