Sample Python Exam Problem Solutions

From EECS 182
Sample Python Exam Problem Solutions
Jump to: navigation, search
 
Line 14: Line 14:
 
* mylist().append(4).isempty(): False
 
* mylist().append(4).isempty(): False
 
* mylist().append(4).pop().isempty(): True
 
* mylist().append(4).pop().isempty(): True
 +
 +
Here is where you can find an implementation of the above list (save both files in the same folder. Sample.py contains an example use. MYLIST.py contains the implementation
 +
above, except for the indexing function. You can do all the problems without indexing operation):
 +
* https://wiki.eecs.umich.edu/global/data/eecs182/images/8/8d/Sample.py
 +
* https://wiki.eecs.umich.edu/global/data/eecs182/images/a/a6/MYLIST.py
  
 
Given the above mylist type, implement the following functions only using the above interface to mylist.
 
Given the above mylist type, implement the following functions only using the above interface to mylist.

Latest revision as of 02:20, December 15, 2012

Problem: You are given a special type of List called mylist that has only a subset of functionality of Python lists. It provides the following methods:

Some examples:

Here is where you can find an implementation of the above list (save both files in the same folder. Sample.py contains an example use. MYLIST.py contains the implementation above, except for the indexing function. You can do all the problems without indexing operation):

Given the above mylist type, implement the following functions only using the above interface to mylist.

A. deletelasttwo(L): returns a mylist that contains all but the last two elements of L, in same sequence.

B. replacelast(L, item): returns a mylist that is same as L, except that the last value is replaced by item.

C. lookup(L, k): return the kth item in the mylist, where indexing starts at 0.

D. concat(L1, L2): returns a mylist that is a concatenation of L1 and L2.

E. insert_first(item, L): returns a mylist that contains item followed by items in L, in sequence.

F. len(L): returns the number of objects in mylist.

G; assign(L, index, val): Returns a new list identical to L, but in which the item at the index position in L is replaced by val.

All the methods are non-destructive, in the sense that the parameter L need not be modified.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
EECS @ UM
Tools