Académique Documents
Professionnel Documents
Culture Documents
Built-in Function Problem """Return the difference between the largest and
smallest absolute values in a list."""
1. Create the sequence [0, 3, 6, 9, ... N]. What is the x = [4, 3, -9, 21, 0]
problem if N is very large? # Your code here
Is there a better way if N is very large?
2. Find the difference between the biggest and def list_to_sequence_dict():
smallest values in the list """Create a dictionary where the key is the ordinal
[4, 3, -9, 21, 0] of the object in the list
3. The same as 2. but use the absolute values in the and the value is the object itself. For example: {0 :
list. 'Zero', 1 : 'One', ...}"""
4.Convert a list: x = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
'Seven', 'Eight', 'Nine'] d = {}
To a dictionary: # Your code here
{ return d
0 : 'Zero',
1 : 'One', def is_same(a, b):
2 : 'Two', """Return True is the two items are the same."""
3 : 'Three', # Your code here
4 : 'Four', pass
5 : 'Five',
6 : 'Six', #=========== Tests ===================
7 : 'Seven', def test_create_sequence():
8 : 'Eight', assert create_sequence(12) == [0, 3, 6, 9, 12]
9 : 'Nine',
}
def test_range_of_list():
5. I have two list a and b. Is there a way that I can tell
assert range_of_list() == 30
if they are the same
list? For example in the following case they are the
def test_range_of_list_abs():
same list:
assert range_of_list_abs() == 21
a = [1, 2, 3]
b=a
def test_list_to_sequence_dict():
And any change to b be will be 'seen' by a.
expected = {
However in this case a and b are not the same list in
0 : 'Zero',
the sense that any change
1 : 'One',
to b be will NOT be 'seen' by a.
2 : 'Two',
a = [1, 2, 3]
3 : 'Three',
b = [1, 2, 3]
4 : 'Four',
5 : 'Five',
def create_sequence(N):
6 : 'Six',
"""Create the 3x table up to and including N."""
7 : 'Seven',
# Your code here
8 : 'Eight',
pass
9 : 'Nine',
}
def range_of_list():
assert list_to_sequence_dict() == expected
"""Return the difference between the largest and
smallest values in a list."""
def test_is_same():
x = [4, 3, -9, 21, 0]
a = [1, 2, 3]
# Your code here
b=a
assert is_same(a, b)
b = [1, 2, 3] def test_range_of_list():
assert not is_same(a, b) assert range_of_list() == 30
def list_to_sequence_dict():
"""Create a dictionary where the key is the ordinal
of the object in the list
and the value is the object itself. For example: {0 :
'Zero', 1 : 'One', ...}"""
x = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine']
d = {}
for index, value in enumerate(x):
d[index] = value
return d
def transpose_easy(list_of_lists):
"""Transpose a list of lists."""
# Gotcha: zip returns a list of tuples, we want a list
of lists
return [list(v) for v in zip(*list_of_lists)]
transpose = transpose_hard
def peak_to_peak(alist):
"""Return the peak to peak value of a list."""
return max(alist) - min(alist)
def rotate_left(alist):
"""Rotates a list to the left so that the first item
appears at the end."""
if len(alist):
alist.append(alist.pop(0))
def rotate_right(alist):
"""Rotates a list to the right so that the last item
appears at the beginning."""
if len(alist):
alist.insert(0, alist.pop())
return [a_string[i:i+4] for i in
String Problem range(0,len(a_string),4)]
"""With this string:
'monty pythons flying circus'
Create a function that returns a sorted string with no def test_no_duplicates():
duplicate characters s = 'monty pythons flying circus'
(keep any whitespace): assert no_duplicates(s) == ' cfghilmnoprstuy'
Example: ' cfghilmnoprstuy'
Create a function that returns the words in reverse
order: def test_reversed_words():
Example: ['circus', 'flying', 'pythons', 'monty'] s = 'monty pythons flying circus'
Create a function that returns a list of 4 character assert reversed_words(s) == ['circus', 'flying',
strings: 'pythons', 'monty']
Example: ['mont', 'y py', 'thon', 's fl', 'ying', ' cir',
'cus']
def test_four_char_strings():
String Sol’n s = 'monty pythons flying circus'
assert four_char_strings(s) == ['mont', 'y py',
def no_duplicates(a_string): 'thon', 's fl', 'ying', ' cir', 'cus']
# set(a_string) will remove duplicates
# sorted(sequence) will create a sorted list of
sequence
# ''.join(sequence) will create a single string out of
a sequence of strings
# This can all be done in one line
return ''.join(sorted(set(a_string)))
def reversed_words(a_string):
# a_string.split() will create a list of words
# reversed(sequence) will create a reversed
sequence iterator
# list(iterator) will iterate across the sequence and
create a list of those objects
# This can all be done in one line
return list(reversed(a_string.split()))
def four_char_strings(a_string):
# The key to this puzzle is to build it up in stages
# Note that:
# range(0,len(a_string),4)
# Gives: [0, 4, 8, 12, 16, 20, 24]
# And a_string[0:4]
# Gives 'mont'
# And a_string[4:8]
# Gives 'y py'
# And so on so a_string[i:i+4] seems useful
# This can all be done in one line