hw7.py.
def bisect(lst, ele):lst where the element could be
inserted without violating the sorted ordering.>>> bisect([], 12) 0 >>> bisect([1,3,5,7], 0) 0 >>> bisect([1,3,5,7], 1) 1 >>> bisect([1,3,5,7], 2) 1 >>> bisect([1,3,5,7], 3) 2 >>> bisect([1,3,5,7], 4) 2 >>> bisect([1,3,5,7], 5) 3 >>> bisect([1,3,5,7], 6) 3 >>> bisect([1,3,5,7], 7) 4 >>> bisect([1,3,5,7], 8) 4
insert(lst, ele) that takes two
arguments: a list and an element (just like bisect()).
You should assume that lst is sorted. The function will
not return anything; instead, it will add ele
to lst while keeping lst sorted. You may call
bisect(lst, ele) as part of your solution if you find it helpful,
but you are not required to do so.
>>> lst = [1,3,5,7] >>> insert(lst, 0) >>> lst [0, 1, 3, 5, 7] >>> insert(lst, 2) >>> lst [0, 1, 2, 3, 5, 7] >>> insert(lst, 6) >>> lst [0, 1, 2, 3, 5, 6, 7] >>> insert(lst, 8) >>> lst [0, 1, 2, 3, 5, 6, 7, 8] >>> insert(lst, 4) >>> lst [0, 1, 2, 3, 4, 5, 6, 7, 8] >>> insert(lst, 5) [0, 1, 2, 3, 4, 5, 5, 6, 7, 8]
insertionSort(lst) that takes an
arbitrary list as an argument and returns a new list that contains all the
elements of the original list in sorted order. The parameter lst
will not be modified. It is strongly
recommended that you call insert() as part of your solution.
>>> lst = [5, 3, 8, 2, 1, 0, 5, 6] >>> insertionSort(lst) [0, 1, 2, 3, 5, 5, 6, 8] >>> lst [5, 3, 8, 2, 1, 0, 5, 6]
stumble2() to implement this program.[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0] [0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0] [0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0] [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0] [0, 0, 1, 2, 1, 1, 0, 0, 0, 0, 0] [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]
def randomElement(lst):lst, randomElement(lst) in lst should
always evaluate to True.
for loop, while loop, or an if statement, it is incorrect!
>>> randomElement([12,34,56,78,90]) 34 >>> randomElement([12,34,56,78,90]) 90 >>> randomElement([12,34,56,78,90]) 12 >>> randomElement(['hello', 'goodbye', 'greetings', 'salutations']) 'greetings'
def polygonArea(poly):poly.>>> polygonArea([(0, 0), (0, 4), (4, 4), (4, 0)]) 16.0 >>> polygonArea([(0, 0), (2, 2), (4, 0)]) 4.0 >>> polygonArea([(0, 0), (0, 4), (2, 6), (4, 4), (4, 0)]) 20.0 >>> polygonArea([(2, 0), (4, 1), (3, 2), (1, 3), (-1, 2), (-2, 1), (0, 0)]) 11.0
squares
smaller
multBy
lowerCase
stripped
onlyWith
strings2points
splitUp
>>> comment_lines('test.txt')
['# This is a test file for our file I/O.\n', '# This line is another comment line.\n']
>>> sorted_lines('test.txt')
['# This is a test file for our file I/O.\n', '# This line is another comment line.\n', 'A fifth line might clarify issues further.\n', 'Another line is given here to help test sorting.\n', 'This is the second line of the aforementioned file.\n']
>>> counter('test.txt')
(221, 42, 5)
>>> only_with('test.txt', 'This')
['# This is a test file for our file I/O.\n', 'This is the second line of the aforementioned file.\n', '# This line is another comment line.\n']