Contents
- ./descendSort.py
- ./fibs2.py
- ./swap.py
- ./test.py
./descendSort.py 1/4
[top][prev][next]
# Demonstrate passing lists to functions
# One function modifies the list parameter,
# one function is a "pure function" and does not modify the parameter
# CSCI111
import test
def main():
# test descendSort3Nums
aList = [1,2,3]
descendSort3Nums(aList)
print(aList)
aList = [0, 5, -3]
descendSort3Nums(aList)
print(aList)
aList = [7,4,1]
descendSort3Nums(aList)
print(aList)
aList = [-1, -1, -3]
descendSort3Nums(aList)
print(aList)
aList = [-1, -5, -3]
descendSort3Nums(aList)
print(aList)
def descendSort3Nums(list3):
"""
Parameter: list3: a list containing three numbers
Sorts the list in descending order
Note: does not return anything, no output
"""
if list3[1] > list3[0]:
# swap 'em
tmp = list3[0]
list3[0] = list3[1]
list3[1] = tmp
if list3[2] > list3[1]:
# swap 'em
tmp = list3[1]
list3[1] = list3[2]
list3[2] = tmp
if list3[1] > list3[0]:
# swap 'em
tmp = list3[0]
list3[0] = list3[1]
list3[1] = tmp
def createDescendingList(list3):
"""
Parameter: list3: a list containing three numbers
Sorts the list in descending order and returns that list
(Example of "pure function")
"""
copyOfList3 = list3 + []
if copyOfList3[1] > copyOfList3[0]:
# swap 'em
tmp = copyOfList3[0]
copyOfList3[0] = copyOfList3[1]
copyOfList3[1] = tmp
if copyOfList3[2] > copyOfList3[1]:
# swap 'em
tmp = copyOfList3[1]
copyOfList3[1] = copyOfList3[2]
copyOfList3[2] = tmp
if copyOfList3[1] > copyOfList3[0]:
# swap 'em
tmp = copyOfList3[0]
copyOfList3[0] = copyOfList3[1]
copyOfList3[1] = tmp
return copyOfList3
def testDescendSort3Nums():
origList = [1, 2, 3]
descendSort3Nums(origList)
# test that the list sorted is in the other order
test.testEqual( origList, [3, 2, 1])
aList = [-1, -5, -3]
descendSort3Nums(aList)
test.testEqual( aList, [-1, -3, -5])
def testCreateDescendingSort3Nums():
origList = [1, 2, 3]
test.testEqual( createDescendingList(origList), [3, 2, 1])
# verify that the original list didn't change.
test.testEqual( origList, [1, 2, 3] )
aList = [-1, -5, -3]
test.testEqual( createDescendingList(aList), [-1, -3, -5])
test.testEqual( aList, [-1, -5, -3])
testDescendSort3Nums()
testCreateDescendingSort3Nums()
#main()
./fibs2.py 2/4
[top][prev][next]
# Example of creating a list of the appropriate size
# Computes the first SIZE Fibonacci numbers
# Tradeoff of using more space (the list, rather than a few variables)
# for easier implementation
# Sara Sprenkle
SIZE = 20
print("This program generates the first", SIZE, "Fibonacci numbers")
# creates a list of size SIZE, containing all 0s
fibs = [0]*SIZE
#fibs[0] = 0
fibs[1] = 1
for x in range(2, len(fibs)):
newfib = fibs[x-1]+fibs[x-2]
fibs[x] = newfib
# print that list out, as a list
print(fibs)
# To print out the list, individually (instead of as a list)
print("Printing elements of list: ")
for num in fibs:
print(num, end=" ")
print()
./swap.py 3/4
[top][prev][next]
# Attempt at swapping two variables in a function
# FAIL! Swapping within a function won't work. :(
# Why? With immutable data types (like integers), the functions are
# passed *copies* of the parameters, not the original variables
def main():
x = 5
y = 7
print("In main:")
print("x =", x)
print("y =", y)
swap(x, y)
print("In main after call to swap:")
print("x =", x)
print("y =", y)
def swap(a, b):
"""
Swaps the values stored in a and b
"""
tmp = a
a = b
b = tmp
print("In swap function: ", a, b)
main()
./test.py 4/4
[top][prev][next]
# From How to Think Like a Computer Scientist textbook
def testEqual(actual, expected):
if type(expected) == type(1):
# they're integers, so check if exactly the same
if actual == expected:
print('Pass')
return True
elif type(expected) == type(1.11):
# a float is expected, so just check if it's very close, to allow for
# rounding errors
if abs(actual-expected) < 0.00001:
print('Pass')
return True
else:
# check if they are equal
if actual == expected:
print('Pass')
return True
print('Test Failed: expected ' + str(expected) + ' but got ' + str(actual))
return False
Generated by GNU Enscript 1.6.5.90.