Contents
- mystery.py
- search_compare.py
- twod_exercises.py
mystery.py 1/3
[top][prev][next]
# Practice with 2D lists
# by Sara Sprenkle
def main():
matrix = createMatrix()
print("Before:")
print(matrix)
mystery(matrix)
print("After:")
print(matrix)
def mystery(a):
""" "run" this on A, at right """
for row in range( len(a) ):
for col in range( len(a[0]) ):
if row == col:
a[row][col] = 42
else:
a[row][col] += 1
def createMatrix():
a0 = list(range(1,5))
a1 = list(range(5,9))
a2 = list(range(9,13))
a = []
a.append(a0)
a.append(a1)
a.append(a2)
return a
main()
search_compare.py 2/3
[top][prev][next]
# Compare the linear and binary searching techniques by the number of
# comparisons.
# by Sara Sprenkle
def main():
print("This program helps us to empirically compare search strategies.")
print("Metric: Number of comparisons required to find the key.")
print("\nDisclaimer: May not be a completely fair comparison but gives us an idea.")
print()
# The keys I want to search for in a list
#keys = [1, 4, 13, 19, 125, 126, 127]
keys = [1, 4, 13, 19, 125, 126, 127, 19997, 19998, 19999]
NUM_ENTRIES = 10000
# the list of integers that I'm searching (odd numbers)
integers = list(range(1,NUM_ENTRIES*2,2))
print("Creating list of size", NUM_ENTRIES, "starting at 1 and incrementing by 2 to", NUM_ENTRIES*2)
print()
print("KEY is the number we're looking for.\n")
print("%6s|%14s" % ("","# Comparisons"))
print("%6s| %6s %6s" % ("KEY", "LIN", "BIN"))
print("-"* 24)
total_lin_comp = 0.0
total_bin_comp = 0.0
for key in keys:
lin_comp = linearSearch(integers,key)
bin_comp = binarySearch(integers,key)
print("%6d| %6d %6d" % (key, lin_comp, bin_comp))
# update the total number of comparisons
total_lin_comp += lin_comp
total_bin_comp += bin_comp
print()
print("ON AVERAGE...")
print("-"*50)
print()
print("The average number of comparisons per search for")
print("\tlinear search was", total_lin_comp/len(keys))
print("\tbinary search was", total_bin_comp/len(keys))
# Return the number of comparisons required to find the key.
def linearSearch(searchlist, key):
numChecks = 0
for elem in searchlist:
numChecks += 1
if elem == key:
return numChecks
return numChecks
# Return the number of comparisons required to find the key
def binarySearch(searchlist, key):
low = 0
high = len(searchlist)-1
numChecks = 0
while low <= high:
mid = (low+high)//2
numChecks+= 2 # while comparison, next if comparison
if searchlist[mid] == key:
return numChecks
numChecks+=1
if searchlist[mid] < key:
low = mid+1
else:
high = mid-1
return numChecks
main()
twod_exercises.py 3/3
[top][prev][next]
# Practice with 2D Lists
# Sara Sprenkle
def main():
#rows = int(input("How many rows? "))
#columns = int(input("How many columns? "))
rows = 3
columns = 4
print()
print("Correct Matrix Creation:")
print('-'*30)
matrix = create2DList(rows, columns)
print(matrix)
print("\nAssigning matrix[1][2]=3")
matrix[1][2] = 3
print("Result: ")
print(matrix)
print()
print("*"*55)
print("Incorrect Matrix Creation:")
print('-'*30)
matrix = noCreate2DList(rows, columns)
print(matrix)
print("\nAssigning matrix[1][2]=3")
print("Result: ")
matrix[1][2] = 3
print(matrix)
def create2DList(rows, cols):
"""Returns a two-dimensional list filled with 0s that is 'rows' tall and
'cols' wide."""
twodlist = []
for rowPos in range(rows):
# creates a new list
row = []
for colPos in range(cols):
row.append(0)
twodlist.append(row)
return twodlist
def noCreate2DList(rows, cols):
"""Does not create a 2D list because each 'row' points to the same list in
memory."""
twodlist = []
# creates one list
onerow = []
for colPos in range(cols):
onerow.append(0)
for rowPos in range(rows):
twodlist.append(onerow)
return twodlist
main()
Generated by GNU Enscript 1.6.6.