Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # SAMS 2018, Programming Sections A and B
- #########################################
- # Full name: Britney Beatey
- # Andrew ID: bbeatey
- #########################################
- # DUE DATE: Sunday July 29th, 5pm.
- # SUBMIT THIS FILE TO AUTOLAB. LATE SUBMISSIONS WILL NOT BE ACCEPTED.
- # For the functions below, erase the "return None" line and write the
- # appropriate piece of code instead.
- # IMPORTANT NOTE:
- # You are not allowed to import any modules or use recursion.
- # Takes a list, l, and rotates all the elements in l one position to the left,
- # placing the first element in the last position. For example, if the list
- # before the call to rotateLeft is 1, 4, 5, 6, it is 4, 5, 6, 1 after the call.
- def rotateLeft(n):
- if len(n) == 0:
- return None
- if len(n) == 1:
- return None
- first = n[0]
- for i in range(len(n)-1):
- n[i] = n[i+1]
- n[len(n)-1] = first
- return None
- # Takes a list, l, and rotates all the elements in l one position to the right,
- # placing the last element in position 0. For example, if the list before the
- # call to rotateRight is 1, 4, 5, 6, it is 6, 1, 4, 5 after the call.
- def rotateRight(n):
- if len(n) == 0:
- return None
- if len(n) == 1:
- return None
- last = n[-1]
- for i in range((len(n)-1),-1,-1):
- n[i] = n[i-1]
- n[0] = last
- return None
- # Takes a list, l and reverses the elements stored in the list in place. For example,
- # if the list before the call to reverse is 1, 4, 5, 6, it will be 6, 5, 4, 1 after
- # the call. Obviously, you are NOT allowed to use the built-in Python list.reverse()
- # method to solve this question!
- def reverse(n):
- if len(n) == 0:
- return None
- if len(n) == 1:
- return None
- first = 0
- last = (len(n)-1)
- while first < last:
- n[first],n[last] = n[last],n[first]
- first = first + 1
- last = last-1
- return None
- # Unlike the previous three functions which were destructive (i.e., they modified the
- # original list), this function takes a list and returns a new list with any duplicate
- # values removed (i.e., the new list will contain all the unique values in the original
- # list). The original list must not be modified in any way.
- def copyNoDups(n):
- return []
- # Takes a list, l, and returns the index of the first occurrence of target in l.
- # Returns -1 if target does not occur in l
- def firstIndex(n, target):
- return 42
- # Takes a list, l, and returns the index of the last occurrence of target in l.
- # Returns -1 if target does not occur in l
- def lastIndex(n, target):
- return 42
- # Takes a list of integers and counts the number of multiples of 2 and 3 that occur
- # in the list and returns whichever count is higher.
- def maxMultiples23(ints):
- return 42
- # Takes a list of integers and keeps track of the sum of the values as you traverse
- # from the first value to the last. But instead of returning the sum (how boring),
- # you will return the number of times the sum returns to zero after becoming non-zero.
- # For example, if [1,-1,0,-2,3,-3,2] were the parameter, the sum would start at 0,
- # and then take on the values 1, 0, 0, -2, 1, -2, 0; so the function would return 2
- def countCycles(ints):
- return 42
- ######################################################################
- # ignore_rest: The autograder will ignore all code below here
- ######################################################################
- def testRotateLeft():
- print("Testing rotateLeft...", end="")
- a = ['a']
- rotateLeft(a)
- assert(a == ['a'])
- a = ['a','b']
- rotateLeft(a)
- assert(a == ['b','a'])
- a = ['a','b','c']
- rotateLeft(a)
- assert(a == ['b','c','a'])
- a = [1, 4, 5, 6]
- rotateLeft(a)
- assert(a == [4, 5, 6, 1])
- print("Passed. (Add more tests to be more sure!)")
- def testRotateRight():
- print("Testing rotateRight...", end="")
- a = ['a']
- rotateRight(a)
- assert(a == ['a'])
- a = ['a','b']
- rotateRight(a)
- assert(a == ['b','a'])
- a = ['a','b','c']
- rotateRight(a)
- assert(a == ['c','a', 'b'])
- a = [1, 4, 5, 6]
- rotateRight(a)
- assert(a == [6, 1, 4, 5])
- print("Passed. (Add more tests to be more sure!)")
- def testReverse():
- print("Testing reverse..", end="")
- a = ['a']
- reverse(a)
- assert(a == ['a'])
- a = ['a','b']
- reverse(a)
- assert(a == ['b','a'])
- a = ['a','b','c']
- reverse(a)
- assert(a == ['c','b', 'a'])
- print("Passed. (Add more tests to be more sure!)")
- def testCopyNoDups():
- print("Testing copyNoDups...", end="")
- a = ['a']
- assert(copyNoDups(a) == ['a'] and a == ['a'])
- a = ['a','a','a']
- assert(copyNoDups(a) == ['a'] and a == ['a','a','a'])
- a = ['a','b','b']
- assert(copyNoDups(a) == ['a','b'] and a == ['a','b','b'])
- a = [1,2,3,4,2,2,1,2,5]
- assert(copyNoDups(a) == [1,2,3,4,5] and a == [1,2,3,4,2,2,1,2,5])
- print("Passed. (Add more tests to be more sure!)")
- def testFirstIndex():
- print("Testing firstIndex...", end="")
- assert(firstIndex([1,2,3],2) == 1)
- assert(firstIndex([1,2,2],2) == 1)
- assert(firstIndex([1,2,8,9,2],2) == 1)
- assert(firstIndex([1,2,3],4) == -1)
- print("Passed. (Add more tests to be more sure!)")
- def testLastIndex():
- print("Testing lastIndex...", end="")
- assert(lastIndex([1,2,3],2) == 1)
- assert(lastIndex([1,2,2],2) == 2)
- assert(lastIndex([1,2,8,9,2],2) == 4)
- assert(lastIndex([1,2,3],4) == -1)
- print("Passed. (Add more tests to be more sure!)")
- def testMaxMultiples23():
- print("Testing maxMultiples23..", end="")
- assert(maxMultiples23([2,4,3,6,8]) == 4)
- assert(maxMultiples23([2,4,3,9]) == 2)
- assert(maxMultiples23([1,7,9,11,17,19]) == 1)
- assert(maxMultiples23([-1,-3]) == 1)
- assert(maxMultiples23([]) == 0)
- print("Passed. (Add more tests to be more sure!)")
- def testCountCycles():
- print("Testing countCycles..", end="")
- assert(countCycles([1,-1,0,-2,3,-3,2]) == 2)
- assert(countCycles([1,-1,-2,3,-3,2]) == 2)
- assert(countCycles([]) == 0)
- assert(countCycles(list(range(10))) == 0)
- assert(countCycles(list(range(5)) + list(range(-4,1))) == 1)
- assert(countCycles([1,-1,0,5,-3,-2,0,2]) == 2)
- assert(countCycles([1,-1,-2,2,3,-3]) == 3)
- print("Passed. (Add more tests to be more sure!)")
- def testAll():
- testRotateLeft()
- testRotateRight()
- testReverse()
- testCopyNoDups()
- testFirstIndex()
- testLastIndex()
- testMaxMultiples23()
- testCountCycles()
- testAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement