Assignment 27 — list manipulating functions
Due Wednesday, November 4
Module aid.py
- Code will be available at the start of class.
- Complete the implementation of three functions. Simple tester program abet.py is available to help you test your module
aid
functions.
- None of your functions should get input or print output.
- Examine and think about algorithms for the problems. However, do not write any code before class.
Function rotate( x )
- Updates list
x
by moving the last element of listx
(if any) to the beginning of listx
. This type of list transformation is often called a circular shift.
- In carrying out its action, the function modifies the contents of the list
x
in place (e.g., by list-modification methods likepop()
,append()
, andinsert()
, or by directly modifying individual elements of listx
.
- The function returns
None
.
- 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 =", x1 )
aid.rotate( x2 ) ; print( "x2 =", x2 )
aid.rotate( x3 ) ; print( "x3 =", x3 )
aid.rotate( x4 ) ; print( "x4 =", x4 )
- The segment produces as output:
x1 = []
x2 = [3]
x3 = [9, 3, 1, 4, 1, 5]
x4 = ['s', 'p', 'e', 'c', 'u', 'l', 'a', 't', 'i', 'o', 'n']
Function rotate_k_times( x, k )
- Updates list
x
by performingk
circular shifts.
- In carrying out its action, the function modifies the contents of the list
x
in place.
- The function returns
None
- 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 =", x1 )
aid.rotate_k_times( x2, 3 ) ; print( "x2 =", x2 )
aid.rotate_k_times( x3, 4 ) ; print( "x3 =", x3 )
aid.rotate_k_times( x4 ,5 ) ; print( "x4 =", x4 )
- The segment produces as output:
x1 = []
x2 = [3]
x3 = [4, 1, 5, 9, 3, 1]
x4 = ['t', 'i', 'o', 'n', 's', 'p', 'e', 'c', 'u', 'l', 'a']
Function common( x, y )
- Returns a new list whose elements are those elements in
x
that are also iny
. The ordering of elements in the return list should reflect their ordering inx
.
- The function does not modify parameters
x
andy
in any way.
- 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, ]
z1 = aid.common( x1, y1 ) ; print( "z1 =", z1 )
z2 = aid.common( x2, y2 ) ; print( "z2 =", z2 )
z3 = aid.common( x3, y3 ) ; print( "z3 =", z3 )
z4 = aid.common( x4, y4 ) ; print( "z4 =", z4 )
- The segment produces as output:
z1 = []
z2 = []
z3 = [1]
z4 = [2, 8, 2, 8, 8]
© 2020 Jim Cohoon | Resources from previous semesters are available. |