-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathunit2_lesson_01_understanding_controlflow.py
More file actions
203 lines (166 loc) · 4.27 KB
/
Copy pathunit2_lesson_01_understanding_controlflow.py
File metadata and controls
203 lines (166 loc) · 4.27 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
__author__ = 'Kalyan'
from placeholders import *
notes = '''
python has support for standard control flow statements similar to other languages.
iteration over sequences like list, string etc. is built into the language itself (c# has
similar features) and the loops support an else clause which is not common elsewhere.
'''
def test_if():
value = 1
if True:
value = 2
assert __ == value
if not True:
value = 3
assert __ == value
def test_if_else():
value = 1
if not True:
value = 2
else:
value = 3
assert __ == value
def test_if_elif_else():
value = 3
str = "str"
if value < 0:
str = "negative"
elif value == 0:
str = "zero"
else:
str = "positive"
assert __ == str
def test_for_loop_range():
"""
for loops are used to iterate over arbitrary sequences
"""
nums =[]
for x in range(1,5):
nums.append(x)
assert [__] == nums
def test_for_loop_string():
chars = []
for x in "engine":
chars.append(x)
assert [__] == chars
def test_for_loop_list():
result = ""
for fruit in ["orange", "banana", "apple"]:
result += fruit
assert __ == result
def test_for_loop_list_with_enumerate():
words = ["one", "two", "three"]
result = []
for p in enumerate(words):
result.append(p)
assert [__] == result
mapping = dict(result)
assert {__} == mapping
def test_for_loop_dict():
num_to_word = {1 : "one", 2 : "two", 3 : "three"}
result = []
for item in num_to_word:
result.append(item)
assert [__] == result
def test_while_loop():
result = []
while len(result) < 3:
result.append(10)
assert [__] == result
def test_for_loop_break():
result = []
for x in range(1,10):
if x % 5 == 0:
break
result.append(x)
assert [__] == result
def test_for_loop_continue():
result = []
for x in range (1, 10):
if x % 3 == 0:
continue
result.append(x)
assert [__] == result
def test_nested_loop_break():
result = []
for x in range(2):
for y in range(1,5):
if y%3 == 0:
break
result.append(x)
assert [__] == result
def test_nested_loop_continue():
result = []
for x in range(2):
for y in range(1,5):
if y%3 == 0:
continue
result.append(x)
assert [__] == result
def test_nested_loop_break_continue():
result = []
for x in range(3):
for y in range(1,5):
if y%3 == 0:
continue
if x%2 == 1:
break
result.append(x)
assert [__] == result
# else on loops is not available in other common languages
def test_for_loop_else_plain():
result = []
for x in range(5):
result.append(x)
print("x in loop", x)
else:
result.append(10)
assert [__] == result
def test_for_loop_else_break():
result = []
for x in range(1,5):
if x %3 == 0:
break
result.append(x)
else:
result.append(10)
assert [__] == result
def test_for_loop_else_continue():
result = []
for x in range(5):
if x %3 == 0:
continue
result.append(x)
else:
result.append(10)
assert [__] == result
def test_while_loop_else():
result = []
x = 1
while x in range(5):
result.append(x)
x = x+1
if x%4 == 0:
break
else:
result.append(10)
assert [__] == result
# Apply what you have learnt. Implement the method below using while, "for with else",
# if, etc.
# Don't treat this as a optimization problem, write plain brute force code :)
def get_primes(count):
"""
Return the 1st count prime integers.
"""
pass
def test_primes():
assert [] == get_primes(0)
assert [] == get_primes(-1)
assert [2] == get_primes(1)
assert [2,3,5,7,11] == get_primes(5)
assert [2,3,5,7,11,13,17,19,23,29] == get_primes(10)
three_things_i_learnt = """
-
-
-
"""