Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from lab04 import *
- ## Extra Lists, Dictionaries Questions ##
- #########
- # Lists #
- #########
- # Q12
- def merge(lst1, lst2):
- """Merges two sorted lists.
- >>> merge([1, 3, 5], [2, 4, 6])
- [1, 2, 3, 4, 5, 6]
- >>> merge([], [2, 4, 6])
- [2, 4, 6]
- >>> merge([1, 2, 3], [])
- [1, 2, 3]
- >>> merge([5, 7], [2, 4, 6])
- [2, 4, 5, 6, 7]
- """
- "*** YOUR CODE HERE ***"
- # Q13
- def mergesort(seq):
- """Mergesort algorithm.
- >>> mergesort([4, 2, 5, 2, 1])
- [1, 2, 2, 4, 5]
- >>> mergesort([]) # sorting an empty list
- []
- >>> mergesort([1]) # sorting a one-element list
- [1]
- """
- "*** YOUR CODE HERE ***"
- # Q14
- def coords(fn, seq, lower, upper):
- """
- >>> seq = [-4, -2, 0, 1, 3]
- >>> fn = lambda x: x**2
- >>> coords(fn, seq, 1, 9)
- [[-2, 4], [1, 1], [3, 9]]
- """
- "*** YOUR CODE HERE ***"
- return ______
- # Q15
- def deck(suits, numbers):
- """Creates a deck of cards (a list of 2-element lists) with the given
- suits and numbers. Each element in the returned list should be of the form
- [suit, number].
- >>> deck(['S', 'C'], [1, 2, 3])
- [['S', 1], ['S', 2], ['S', 3], ['C', 1], ['C', 2], ['C', 3]]
- >>> deck(['S', 'C'], [3, 2, 1])
- [['S', 3], ['S', 2], ['S', 1], ['C', 3], ['C', 2], ['C', 1]]
- >>> deck([], [3, 2, 1])
- []
- >>> deck(['S', 'C'], [])
- []
- """
- "*** YOUR CODE HERE ***"
- return ______
- ################
- # Dictionaries #
- ################
- # Q16
- def counter(message):
- """ Returns a dictionary of each word in message mapped
- to the number of times it appears in the input string.
- >>> x = counter('to be or not to be')
- >>> x['to']
- 2
- >>> x['be']
- 2
- >>> x['not']
- 1
- >>> y = counter('run forrest run')
- >>> y['run']
- 2
- >>> y['forrest']
- 1
- """
- word_list = message.split()
- "*** YOUR CODE HERE ***"
- # Q17
- def replace_all(d, x, y):
- """
- >>> d = {'foo': 2, 'bar': 3, 'garply': 3, 'xyzzy': 99}
- >>> replace_all(d, 3, 'poof')
- >>> d == {'foo': 2, 'bar': 'poof', 'garply': 'poof', 'xyzzy': 99}
- True
- """
- "*** YOUR CODE HERE ***"
Advertisement
Add Comment
Please, Sign In to add comment