List & tuple exercises
Copy/Paste the code below into 2 files and call it list1.py & list2.py
Do the exercises and run it. When the tests all pass, you have solved the exercise.
All exercises should be done with List Comprehensions whenever possible.
List1.py
1# Copyright 2010 Google Inc.
2# Licensed under the Apache License, Version 2.0
3# http://www.apache.org/licenses/LICENSE-2.0
4
5# Basic list & tuples exercises
6# Fill in the code for the functions below. main() is already set up
7# to call the functions with a few different inputs,
8# printing 'OK' when each function is correct.
9# The starter code for each function includes a 'return'
10# which is just a placeholder for your code.
11# It's ok if you do not complete all the functions, and there
12# are some additional functions to try in list2.py.
13
14# A. match_ends
15# Given a list of strings, return the count of the number of
16# strings where the string length is 2 or more and the first
17# and last chars of the string are the same.
18# Note: python does not have a ++ operator, but += works.
19def match_ends(words):
20 # +++your code here+++
21 return
22
23
24# B. front_x
25# Given a list of strings, return a list with the strings
26# in sorted order, except group all the strings that begin with 'x' first.
27# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
28# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
29# Hint: this can be done by making 2 lists and sorting each of them
30# before combining them.
31def front_x(words):
32 # +++your code here+++
33 return
34
35
36
37# C. sort_last
38# Given a list of non-empty tuples, return a list sorted in increasing
39# order by the last element in each tuple.
40# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
41# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
42# Hint: use a custom key= function to extract the last element form each tuple.
43
44
45
46def sort_last(tuples):
47 # +++your code here+++
48 return
49
50
51# Simple provided test() function used in main() to print
52# what each function returns vs. what it's supposed to return.
53def test(got, expected):
54 if got == expected:
55 prefix = ' OK '
56 else:
57 prefix = ' X '
58 print (f'{prefix} got: {got} expected: {expected}')
59
60
61# Calls the above functions with interesting inputs.
62def main():
63 print ('match_ends')
64 test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
65 test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
66 test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
67
68 print()
69 print ('front_x')
70 test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
71 ['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
72 test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
73 ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
74 test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
75 ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
76
77
78 print()
79 print ('sort_last')
80 test(sort_last([(1, 3), (3, 2), (2, 1)]),
81 [(2, 1), (3, 2), (1, 3)])
82 test(sort_last([(2, 3), (1, 2), (3, 1)]),
83 [(3, 1), (1, 2), (2, 3)])
84 test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
85 [(2, 2), (1, 3), (3, 4, 5), (1, 7)])
86
87
88main()
89
List2.py
1
2# Copyright 2010 Google Inc.
3# Licensed under the Apache License, Version 2.0
4# http://www.apache.org/licenses/LICENSE-2.0
5
6# Additional basic list exercises
7
8# D. Given a list of numbers, return a tuple where
9# all adjacent == elements have been reduced to a single element,
10# so [1, 2, 2, 3] returns (1, 2, 3). You may create a new list or
11# modify the passed in list.
12
13
14def remove_adjacent(nums):
15 # +++your code here+++
16 return
17
18
19# E. Given two lists sorted in increasing order, create and return a merged
20# list of all the elements in sorted order. You may modify the passed in lists.
21# Ideally, the solution should work in "linear" time, making a single
22# pass of both lists.
23def linear_merge(list1, list2):
24 # +++your code here+++
25 return
26
27# Note: the solution above is kind of cute, but unforunately list.pop(0)
28# is not constant time with the standard python list implementation, so
29# the above is not strictly linear time.
30# An alternate approach uses pop(-1) to remove the endmost elements
31# from each list, building a solution list which is backwards.
32# Then use reversed() to put the result back in the correct order. That
33# solution works in linear time, but is more ugly.
34
35
36# Simple provided test() function used in main() to print
37# what each function returns vs. what it's supposed to return.
38def test(got, expected):
39 if got == expected:
40 prefix = ' OK '
41 else:
42 prefix = ' X '
43 print (f'{prefix} got: {got} expected: {expected}')
44
45
46# Calls the above functions with interesting inputs.
47def main():
48 print()
49 print('remove_adjacent')
50 test(remove_adjacent([1, 2, 2, 3]), (1, 2, 3))
51 test(remove_adjacent([2, 2, 3, 3, 3]), (2, 3))
52 test(remove_adjacent([]), ())
53
54 print()
55 print('linear_merge')
56 test(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']),
57 ['aa', 'bb', 'cc', 'xx', 'zz'])
58 test(linear_merge(['aa', 'xx'], ['bb', 'cc', 'zz']),
59 ['aa', 'bb', 'cc', 'xx', 'zz'])
60 test(linear_merge(['aa', 'aa'], ['aa', 'bb', 'bb']),
61 ['aa', 'aa', 'aa', 'bb', 'bb'])
62
63
64main()