-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassessment.py
142 lines (99 loc) · 3.6 KB
/
assessment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""List Assessment
Edit the functions until all of the doctests pass when
you run this file.
"""
def all_odd(numbers):
"""Return a list of only the odd numbers in the input list.
For example::
>>> all_odd([1, 2, 7, -5])
[1, 7, -5]
>>> all_odd([2, -6, 8])
[]
"""
# Use list comprehension to loop through list given in parameters,
# and add number to new list if not divisible by 2
all_odd = [number for number in numbers if number % 2 != 0]
return all_odd
def print_indices(items):
"""Print index of each item in list, followed by item itself.
Do this without using a "counting variable" --- that is, don't
do something like this::
count = 0
for item in list:
print count
count = count + 1
Output should look like this::
>>> print_indices(["Toyota", "Jeep", "Volvo"])
0 Toyota
1 Jeep
2 Volvo
"""
for i, item in enumerate(items):
print i, item # Use enumerate method to print index before each item
def foods_in_common(foods1, foods2):
"""Find foods in common.
Given 2 lists of foods, return the items that are in common
between the two, sorted alphabetically.
**NOTE**: for this problem, you're welcome to use any of the
Python data structures you've been introduced to (not just
lists). Is there another that would be a good idea?
For example::
>>> foods_in_common(
... ["cheese", "bagel", "cake", "kale"],
... ["hummus", "beets", "bagel", "lentils", "kale"]
... )
['bagel', 'kale']
If there are no foods in common, return an empty list::
>>> foods_in_common(
... ["lamb", "chili", "cheese"],
... ["cake", "ice cream"]
... )
[]
"""
# Create sets out of food lists in order to use intersection to find common items
# Store the resulting intersection in a new list
common_foods = list(set(foods1) & set(foods2))
return common_foods
def every_other_item(items):
"""Return every other item in `items`, starting at first item.
For example::
>>> every_other_item([1, 2, 3, 4, 5, 6])
[1, 3, 5]
>>> every_other_item(
... ["you", "z", "are", "z", "good", "z", "at", "x", "code"]
... )
['you', 'are', 'good', 'at', 'code']
"""
# Start from beginning of list, increment by 2 until reaching the end of the list
every_other = items[::2]
return every_other
def largest_n_items(items, n):
"""Return the `n` largest integers in list, in ascending order.
You can assume that `n` will be less than the length of the list.
For example::
>>> largest_n_items([2, 6006, 700, 42, 6, 59], 3)
[59, 700, 6006]
It should work when `n` is 0::
>>> largest_n_items([3, 4, 5], 0)
[]
If there are duplicates in the list, they should be counted
separately::
>>> largest_n_items([3, 3, 3, 2, 1], 2)
[3, 3]
"""
largest_items = [] # Create empty list
# If the given parameter n is 0, the empty list will be returned
if n > 0:
items.sort() # Sort list given in parameter
# Take last n numbers from sorted list to enter into largest_items list
largest_items = items[-n:]
return largest_items
#####################################################################
# END OF ASSESSMENT: You can ignore everything below.
if __name__ == "__main__":
import doctest
print
result = doctest.testmod()
if not result.failed:
print "ALL TESTS PASSED. GOOD WORK!"
print