-
Notifications
You must be signed in to change notification settings - Fork 298
/
exercise3.py
256 lines (213 loc) · 8.25 KB
/
exercise3.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: UTF-8 -*-
"""Modify each function until the tests pass."""
def is_odd(a_number):
"""Return True if a_number is odd, and False if a_number is even.
Look into modulo division using the '%' operator as one way of doing this.
e.g. 4 % 2 = 0
13 %12 = 1
3 % 2 = 1
So if a_number modulo two is zero, then it's even.
"""
return None
def fix_it(moves=True, should_move=True):
"""Decide what to do.
Using the engineering flowchart (in week2 folder of the CODE1161-2019
repo engineeringFlowchart.png) for the rules, return the apropriate
response to the input parameters.
Use conditional statements: if, else, elif etc.
This function should return either:
"WD-40"
"Duct Tape"
"No Problem"
Most people write this function with 4 return statements.
As an extra challenge, see if you can get that down to three.
"""
return None
def loops_preview():
"""Make 8 poops.
Using a for loop
return a list of 8 items, each one a string with exacly one 💩 in it.
E.g.: ['💩', '💩', '💩', '💩', '💩', '💩', '💩', '💩']
"""
choc_list = []
for i in range(8):
choc_list.append("💩")
return choc_list
def loops_1a():
"""Make 10 stars.
Using a for loop
return a list of 10 items, each one a string with exacly one star in it.
E.g.: ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*']
"""
return None
def loops_1c(number_of_items=5, symbol="#"):
"""Respond to variables.
Return a list of number_of_items items, each one a
string with exacly one symbol in it.
E.g.: ['#', '#', '#', '#', '#']
Remember that you're being passed arguments here. Don't hard code the number
or the symbol, let it be whatever it wants to be.
"""
return None
def loops_2():
"""Make a big square starfield.
return a list of 10 items, each one a list of 10 items,
each one of those, a string with exacly one star in it.
E.g.: [
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
]
"""
return None
def loops_3():
"""Make a rising block of numbers.
Return this:
[
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
['2', '2', '2', '2', '2', '2', '2', '2', '2', '2'],
['3', '3', '3', '3', '3', '3', '3', '3', '3', '3'],
['4', '4', '4', '4', '4', '4', '4', '4', '4', '4'],
['5', '5', '5', '5', '5', '5', '5', '5', '5', '5'],
['6', '6', '6', '6', '6', '6', '6', '6', '6', '6'],
['7', '7', '7', '7', '7', '7', '7', '7', '7', '7'],
['8', '8', '8', '8', '8', '8', '8', '8', '8', '8'],
['9', '9', '9', '9', '9', '9', '9', '9', '9', '9']
]
remember that range(10) produces a list of numbers from 0...9
So for every step produced by `for i in range(10):` i is a different number
TIP: notice that this needs to to return strings of numbers,
so call str(number) to cast.
"""
return None
def loops_4():
"""Make a block of numbers that rises left to right.
Return this:
[
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
]
"""
return None
def loops_5():
"""Make the coordinates of the block.
Return this:
[
['(i0, j0)', '(i0, j1)', '(i0, j2)', '(i0, j3)', '(i0, j4)'],
['(i1, j0)', '(i1, j1)', '(i1, j2)', '(i1, j3)', '(i1, j4)'],
['(i2, j0)', '(i2, j1)', '(i2, j2)', '(i2, j3)', '(i2, j4)'],
['(i3, j0)', '(i3, j1)', '(i3, j2)', '(i3, j3)', '(i3, j4)'],
['(i4, j0)', '(i4, j1)', '(i4, j2)', '(i4, j3)', '(i4, j4)'],
['(i5, j0)', '(i5, j1)', '(i5, j2)', '(i5, j3)', '(i5, j4)'],
['(i6, j0)', '(i6, j1)', '(i6, j2)', '(i6, j3)', '(i6, j4)'],
['(i7, j0)', '(i7, j1)', '(i7, j2)', '(i7, j3)', '(i7, j4)'],
['(i8, j0)', '(i8, j1)', '(i8, j2)', '(i8, j3)', '(i8, j4)'],
['(i9, j0)', '(i9, j1)', '(i9, j2)', '(i9, j3)', '(i9, j4)']
]
TIP:
If you've got num_bottles, e.g. num_bottles = 8
You can construct strings either by concatinating them:
"There are " + str(num_bottles) + " green bottles"
or by using format:
"There are {} green bottles".format(num_bottles)
or, my favourite, f-strings:
f"There are {num_bottles} green bottles"
you'll come to see the pros and cons of each over time.
"""
return None
def loops_6():
"""Make a wedge of numbers.
Return this:
[
['0'],
['0', '1'],
['0', '1', '2'],
['0', '1', '2', '3'],
['0', '1', '2', '3', '4'],
['0', '1', '2', '3', '4', '5'],
['0', '1', '2', '3', '4', '5', '6'],
['0', '1', '2', '3', '4', '5', '6', '7'],
['0', '1', '2', '3', '4', '5', '6', '7', '8'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
]
You don't have to use a literal number in the range function.
You can use a variable.
TIP: look out for the starting condition.
"""
return None
def loops_7():
"""Make a pyramid.
Return this:
[
[' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', '*', '*', '*', ' ', ' ', ' '],
[' ', ' ', '*', '*', '*', '*', '*', ' ', ' '],
[' ', '*', '*', '*', '*', '*', '*', '*', ' '],
['*', '*', '*', '*', '*', '*', '*', '*', '*']
]
or in more simple terms:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
(this is what will print when you test from inside this file)
This is a hard problem. Use lots of experimentation and draw
lots of diagrams!
"""
return None
def little_printer(some_kind_of_list, exercise_name):
"""Help to see what's going on.
This is a helper function that prints your
results to check that they are tidy.
Note: You don't have to do anything with it.
"""
if some_kind_of_list is not None:
print("\n" + exercise_name)
if type(some_kind_of_list[0]) is list:
for row in some_kind_of_list:
for column in row:
print(column, end="")
print()
else:
for column in some_kind_of_list:
print(column, end="")
print()
else:
print(exercise_name, "maybe you haven't got to this one yet?")
if __name__ == "__main__":
# this section does a quick test on your results and prints them nicely.
# It's NOT the official tests, they are in tests.py as usual.
# Add to these tests, give them arguments etc. to make sure that your
# code is robust to the situations that you'll see in action.
print(is_odd(1), "is_odd odd")
print(is_odd(4), "is_odd even")
print(fix_it(True, True), "fix_it")
print(fix_it(True, False), "fix_it")
print(fix_it(False, True), "fix_it")
print(fix_it(False, False), "fix_it")
little_printer(loops_preview(), "loops_preview")
little_printer(loops_1a(), "loops_1a")
little_printer(loops_1c(4, "×°×"), "loops_1c")
little_printer(loops_2(), "loops_2")
little_printer(loops_3(), "loops_3")
little_printer(loops_4(), "loops_4")
little_printer(loops_5(), "loops_5")
little_printer(loops_6(), "loops_6")
little_printer(loops_7(), "loops_7")