# lab07.py
# YOUR NAME(S) AND NETID(S) HERE
# Initial skeleton by W. White (WMW2)
# October 7, 2014
"""Functions for Lab 7"""
# Part 1: Lists
def unique(thelist):
"""Returns: The number of unique elements in the list.
Example: unique([5, 9, 5, 7]) evaluates to 3
Example: unique([5, 5, 1, 'a', 5, 'a']) evaluates to 3
Precondition: thelist is a list."""
pass # Implement me
def clamp(thelist,min,max):
"""Modifies the list so that every element is between min and max.
Any number in the list less than min is replaced with min. Any number
in the list greater than max is replaced with max. Any number between
min and max is left unchanged.
This is a PROCEDURE. It modified thelist, but does not return a new list.
Example: if thelist is [-1, 1, 3, 5], then clamp(thelist,0,4) changes
thelist to have [0,1,3,4] as its contents.
Precondition: thelist is a list of numbers. min and max are numbers
with min <= max"""
pass # Implement me
# Part 2: Dictionaries
def invert(thelist):
"""Returns: a dictionary that maps each element of thelist to its position
Example: invert(['a', 'b', 'c']) returns {'a':0, 'b':1, 'c':2}
Example: invert([]) returns {}
Precondition: thelist is a list of primitive values (e.g. not objects or lists)"""
pass # Implement me
def listify(thedict):
"""Return: a copy of thedict where each numeric value is replaced by a list of that
number and the number plus 1.
Example: listify({'a':3, 'b':5', c:1}) returns {'a':[3,4], 'b':[5,6], 'c':[1,2]}
Precondition: thedict is a dictionary whose values are integers (the keys are
all unspecified)."""
pass # Implement me
# OPTIONAL: 2D Lists
# This function is optional and is only mentioned in the online PDF
# It is not mentioned in the printed handout
def reshape(thelist, rows, cols):
"""Returns: A rows*cols 2D list with the contents of thelist
Hint: In a 2D list, the element at row x and col y is the
x*cols+y element listed.
Example: reshape([1,2,3,4],2,2) returns [[1,2], [3,4]]
Example: reshape([1,2,3,4,5,6],3,2) returns [[1,2], [3,4], [5,6]]
Example: reshape([1,2,3,4,5,6],2,3) returns [[1,2,3] ,[4,5,6]]
Precondition: thelist is a list of numbers of size rows*cols. rows
and cols are positive integers."""
pass # Implement me