Skip to content

Commit 2c7f2d9

Browse files
committed
Adds minimal problems.
1 parent 4fc95ca commit 2c7f2d9

File tree

5 files changed

+288
-0
lines changed

5 files changed

+288
-0
lines changed

Beginers/DictOfStateCapitals/StateCapitals.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
"""We have an existing dictionary that maps US states to their capitals.
22
3+
Print the state capital of Idaho
4+
Print all states.
5+
Print all capitals.
6+
Create a single string 'Alabama -> Montgomery, Alaska -> Juneau, ...'
7+
38
Now we want to add the reverse look up, given the name of a capital what state
49
is it in?
510
@@ -69,6 +74,25 @@
6974
'Wyoming' : 'Cheyenne',
7075
}
7176

77+
78+
def capital_of_Idaho():
79+
# Your code here
80+
pass
81+
82+
def all_states():
83+
# Your code here
84+
pass
85+
86+
def all_capitals():
87+
# Your code here
88+
pass
89+
90+
def states_capitals_string():
91+
# Your code here
92+
pass
93+
94+
95+
7296
def get_state(capital):
7397
pass
7498

Beginers/DictOfStateCapitals/solution/StateCapitals_bad.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
"""We have an existing dictionary that maps US states to their capitals.
22
3+
Print the state capital of Idaho
4+
Print all states.
5+
Print all capitals.
6+
Create a single string 'Alabama -> Montgomery, Alaska -> Juneau, ...'
7+
38
Now we want to add the reverse look up, given the name of a capital what state
49
is it in?
510
@@ -70,6 +75,21 @@
7075
'Wyoming' : 'Cheyenne',
7176
}
7277

78+
def capital_of_Idaho():
79+
return STATES_CAPITALS['Idaho']
80+
81+
def all_states():
82+
return STATES_CAPITALS.keys()
83+
84+
def all_capitals():
85+
return STATES_CAPITALS.values()
86+
87+
def states_capitals_string():
88+
l = []
89+
for k in sorted(STATES_CAPITALS.keys()):
90+
l.append('%s -> %s' % (k, STATES_CAPITALS[k]))
91+
return ', '.join(l)
92+
7393
def get_state(capital):
7494
"""A bad solution in that every time we search most of the dictionary.
7595
If the dictionary, instead of representing a few states, say, represented

Beginers/DictOfStateCapitals/solution/StateCapitals_better.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
"""We have an existing dictionary that maps US states to their capitals.
22
3+
Print the state capital of Idaho
4+
Print all states.
5+
Print all capitals.
6+
Create a single string 'Alabama -> Montgomery, Alaska -> Juneau, ...'
7+
38
Now we want to add the reverse look up, given the name of a capital what state
49
is it in?
510
@@ -69,6 +74,21 @@
6974
'Wyoming' : 'Cheyenne',
7075
}
7176

77+
def capital_of_Idaho():
78+
return STATES_CAPITALS['Idaho']
79+
80+
def all_states():
81+
return STATES_CAPITALS.keys()
82+
83+
def all_capitals():
84+
return STATES_CAPITALS.values()
85+
86+
def states_capitals_string():
87+
l = []
88+
for k in sorted(STATES_CAPITALS.keys()):
89+
l.append('%s -> %s' % (k, STATES_CAPITALS[k]))
90+
return ', '.join(l)
91+
7292
# Pre-compute the reverse lookup, this is done a module import time
7393
# What can go wrong here? - duplicates
7494
CAPITALS_STATES = {v : k for k, v in STATES_CAPITALS.items()}

Beginers/ListsTuples/problems.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
"""Some exercises involving lists.
22
3+
creating lists
4+
==============
5+
a='A'
6+
b='B'
7+
c='C'
8+
x = list()
9+
add a, b, c into x
10+
print the length of x
11+
12+
selecting from lists
13+
====================
14+
Given the list
15+
x = ['A', 'B', 'C']
16+
return the first item
17+
return the last item
18+
return the x reversed using indexing
19+
20+
selecting first items
21+
=======================
22+
Given the following:
23+
x = [('A','x'), ('B','y'), ('C','z')]
24+
return ['A','B','C']
25+
26+
add 5 to values
27+
=================
28+
Given the following:
29+
x = [1, 10, 20]
30+
return a list with 5 added to the numbers i.e. [6, 15, 25]
31+
32+
divisible by 5
33+
==================
34+
Given the following:
35+
x = [1, 10, 15, 3, 12, 15, 25, 50]
36+
return a list with only numbers divisible by 5 (% modulo operator)
37+
38+
merge_lists
39+
===================
40+
given the lists:
41+
x = ['A', 'B', 'C']
42+
y = ['x', 'y', 'z']
43+
create the following list:
44+
[('A','x'), ('B','y'), ('C','z')]
45+
346
transpose()
447
===========
548
Create a function that takes a list of lists and returns the transpose. So given:
@@ -55,6 +98,49 @@
5598
"""
5699
import pytest
57100

101+
def create_list():
102+
"""Create a list."""
103+
a ='A'
104+
b ='B'
105+
c ='C'
106+
# Your code goes here
107+
108+
def select_first_item():
109+
"""Return first item."""
110+
x = ['A', 'B', 'C']
111+
# Your code goes here
112+
113+
def select_last_item():
114+
"""Return first item."""
115+
x = ['A', 'B', 'C']
116+
# Your code goes here
117+
118+
def select_reversed():
119+
"""Return list reversed."""
120+
x = ['A', 'B', 'C']
121+
# Your code goes here
122+
123+
def select_first_items():
124+
"""Select first item on each list."""
125+
x = [('A','x'), ('B','y'), ('C','z')]
126+
# Your code goes here
127+
128+
def add_5_to_values():
129+
"""Return the list with 5 added to each value."""
130+
x = [1, 10, 20]
131+
# Your code goes here
132+
133+
def get_divisble_by_5():
134+
"""Return elements that are divisble by 5."""
135+
x = [1, 10, 15, 3, 12, 15, 25, 50]
136+
# Your code goes here
137+
138+
def merge_lists():
139+
"""Returns pairs from each list."""
140+
x = ['A', 'B', 'C']
141+
y = ['x', 'y', 'z']
142+
# Your code goes here
143+
58144
def transpose(list_of_lists):
59145
"""Transpose a list of lists."""
60146
# Your code goes here
@@ -73,6 +159,30 @@ def rotate_right(alist):
73159
pass
74160

75161
#=================== Tests ========================
162+
def test_create_list():
163+
assert create_list() == ['A', 'B', 'C']
164+
165+
def test_select_first_item():
166+
assert select_first_item() == 'A'
167+
168+
def test_select_last_item():
169+
assert select_last_item() == 'C'
170+
171+
def test_select_reversed():
172+
assert select_reversed() == ['C', 'B', 'A']
173+
174+
def test_select_first_items():
175+
assert select_first_items() == ['A','B','C']
176+
177+
def test_add_5_to_values():
178+
assert add_5_to_values() == [6, 15, 25]
179+
180+
def test_get_divisble_by_5():
181+
assert get_divisble_by_5() == [10, 15, 15, 25, 50]
182+
183+
def test_merge_lists():
184+
assert merge_lists() == [('A', 'x'), ('B', 'y'), ('C', 'z')]
185+
76186
def test_transpose():
77187
data = [
78188
[1, 2, 3],

Beginers/ListsTuples/solutions.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
"""Some exercises involving lists.
22
3+
creating lists
4+
==============
5+
a='A'
6+
b='B'
7+
c='C'
8+
x = list()
9+
add a, b, c into x
10+
print the length of x
11+
12+
selecting from lists
13+
====================
14+
Given the list
15+
x = ['A', 'B', 'C']
16+
return the first item
17+
return the last item
18+
return the x reversed using indexing
19+
20+
selecting first items
21+
=======================
22+
Given the following:
23+
x = [('A','x'), ('B','y'), ('C','z')]
24+
return ['A','B','C']
25+
26+
add 5 to values
27+
=================
28+
Given the following:
29+
x = [1, 10, 20]
30+
return a list with 5 added to the numbers i.e. [6, 15, 25]
31+
32+
divisible by 5
33+
==================
34+
Given the following:
35+
x = [1, 10, 15, 3, 12, 15, 25, 50]
36+
return a list with only numbers divisible by 5 (% modulo operator)
37+
38+
merge_lists
39+
===================
40+
given the lists:
41+
x = ['A', 'B', 'C']
42+
y = ['x', 'y', 'z']
43+
create the following list:
44+
[('A','x'), ('B','y'), ('C','z')]
45+
346
transpose()
447
===========
548
Create a function that takes a list of lists and returns the transpose. So given:
@@ -55,6 +98,53 @@
5598
"""
5699
import pytest
57100

101+
def create_list():
102+
"""Create a list."""
103+
a ='A'
104+
b ='B'
105+
c ='C'
106+
x = list()
107+
x.append(a)
108+
x.append(b)
109+
x.append(c)
110+
return x
111+
112+
def select_first_item():
113+
"""Return first item."""
114+
x = ['A', 'B', 'C']
115+
return x[0]
116+
117+
def select_last_item():
118+
"""Return first item."""
119+
x = ['A', 'B', 'C']
120+
return x[-1]
121+
122+
def select_reversed():
123+
"""Return list reversed."""
124+
x = ['A', 'B', 'C']
125+
return x[::-1]
126+
127+
def select_first_items():
128+
"""Select first item on each list."""
129+
x = [('A','x'), ('B','y'), ('C','z')]
130+
return [v[0] for v in x]
131+
132+
def add_5_to_values():
133+
"""Return the list with 5 added to each value."""
134+
x = [1, 10, 20]
135+
return [v + 5 for v in x]
136+
137+
def get_divisble_by_5():
138+
"""Return elements that are divisble by 5."""
139+
x = [1, 10, 15, 3, 12, 15, 25, 50]
140+
return [v for v in x if v % 5 == 0]
141+
142+
def merge_lists():
143+
"""Returns pairs from each list."""
144+
x = ['A', 'B', 'C']
145+
y = ['x', 'y', 'z']
146+
return zip(x, y)
147+
58148
def transpose_hard(list_of_lists):
59149
"""Transpose a list of lists, the hard way."""
60150
height = len(list_of_lists)
@@ -84,6 +174,30 @@ def rotate_right(alist):
84174
alist.insert(0, alist.pop())
85175

86176
#=================== Tests ========================
177+
def test_create_list():
178+
assert create_list() == ['A', 'B', 'C']
179+
180+
def test_select_first_item():
181+
assert select_first_item() == 'A'
182+
183+
def test_select_last_item():
184+
assert select_last_item() == 'C'
185+
186+
def test_select_reversed():
187+
assert select_reversed() == ['C', 'B', 'A']
188+
189+
def test_select_first_items():
190+
assert select_first_items() == ['A','B','C']
191+
192+
def test_add_5_to_values():
193+
assert add_5_to_values() == [6, 15, 25]
194+
195+
def test_get_divisble_by_5():
196+
assert get_divisble_by_5() == [10, 15, 15, 25, 50]
197+
198+
def test_merge_lists():
199+
assert merge_lists() == [('A', 'x'), ('B', 'y'), ('C', 'z')]
200+
87201
def test_transpose():
88202
data = [
89203
[1, 2, 3],

0 commit comments

Comments
 (0)