-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-07-02.qmd
252 lines (195 loc) · 5.01 KB
/
slides-07-02.qmd
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
---
title: "for loops with range (slides)"
format: revealjs
slide-number: true
df-print: kable
---
# CSc 110 - for loops with range
## `while` vs. `for` loops
In addition to `while`, we can use `for` to create loops
```{python}
#| eval: true
#| echo: true
index = 0
while index < 3:
print(index)
index += 1
```
```{python}
#| eval: true
#| echo: true
for index in range(3):
print(index)
```
## `while` vs. `for` loops
In addition to `while`, we can use `for` to create loops
```{python}
#| eval: true
#| echo: true
values = [70, 20, 30]
index = 0
while index < len(values):
print(values[index])
index += 1
```
```{python}
#| eval: true
#| echo: true
values = [70, 20, 30]
for index in range(len(values)):
print(values[index])
```
## Write a function
1. Its name is `make_all_even`
2. It takes one argument, a list of `integers`
3. It iterates over the list, changing odd numbers to even number (even up)
4. Use a `for` loop
```{python}
#| eval: false
#| echo: true
test_integers = [1, 2, 3, 4]
assert make_all_even(test_integers) == [2, 2, 4, 4]
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def make_all_even(integers):
# for each index in list
for index in range(len(integers)):
integers[index] += integers[index] % 2 # add zero if even, one if odd
return integers
def main():
test_integers = [1, 2, 3, 4]
assert make_all_even(test_integers) == [2, 2, 4, 4]
print(test_integers) # we print the list we created before function call
main()
```
## Write a function
Its name is `indices_of_vowels` that takes a single `string` as its parameter.
It returns a list of integers that represent the indices of the vowels in the original list.
Name your file `indices_of_vowels.py` and submit to Gradescope. Use a `for` loop.
Test cases:
```{python}
#| echo: true
#| eval: false
assert indices_of_vowels("hello") == [1, 4]
assert indices_of_vowels("") == []
assert indices_of_vowels("aeiou") == [0, 1, 2, 3, 4]
```
## Write a function -- solution
```{python}
#| echo: true
#| eval: true
def indices_of_vowels(string):
result = [] # initialize empty list to hold indices
# for every index in list
for index in range(len(string)):
if string[index] in "aeiou": # check if character is vowel
result.append(index) # append index to result
return result
def main():
assert indices_of_vowels("hello") == [1, 4]
assert indices_of_vowels("") == []
assert indices_of_vowels("aeiou") == [0, 1, 2, 3, 4]
main()
```
## More on how to use `range()`
Syntax: `range(start, stop, step)`
- `start` Optional. An integer number specifying at which position to start. Default is 0
- `stop` Required. An integer number specifying at which position to stop (not included).
- `step` Optional. An integer number specifying the incrementation. Default is 1
## More on how to use `range()`
```{python}
#| eval: true
#| echo: true
for n in range(5):
print(n)
```
```{python}
#| eval: true
#| echo: true
for n in range(0, 5, 1):
print(n)
```
```{python}
#| eval: true
#| echo: true
for n in range(0, 5, 2):
print(n)
```
## Write a function
1. Its name is `every_two_together`
2. It takes one argument, a list of `characters`
3. It creates a new string with items at even indices in `characters` concatenated together
4. It returns the string created
5. Use a `for` loop
Test cases:
```{python}
#| eval: false
#| echo: true
characters = ["a", "e", "p", "o", "p", "w", "l", "i", "e", "f"]
assert every_two_together(characters) == "apple"
```
## Write a function -- solution
What other test cases should we run?
```{python}
#| eval: true
#| echo: true
def every_two_together(chars):
new_string = ""
for i in range(0, len(chars), 2):
new_string += chars[i]
return new_string
def main():
characters = ["a", "e", "p", "o", "p", "w", "l", "i", "e", "f"]
assert every_two_together(characters) == "apple"
main()
```
## Write a function
Test cases:
```{python}
#| eval: false
#| echo: true
my_numbers = [0, 1, 0, 3, 0]
assert add_every_two(my_numbers) == 0
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def add_every_two(numbers):
result = 0
for i in range(0, len(numbers), 2):
result += numbers[i]
return result
def main():
my_numbers = [0, 1, 0, 3, 0]
assert add_every_two(my_numbers) == 0
main()
```
## Write a function
Its name is `every_two`. It takes one argument, a list of `characters`. It creates a **new** list with items at even indices in `characters` and return the list.
Use a `for` loop.
Test cases:
```{python}
#| eval: false
#| echo: true
characters = ["a", "e", "p", "o", "p", "w", "l", "i", "e", "f"]
assert every_two(characters) == ["a", "p", "p", "l", "e"]
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def every_two(chars):
result = []
for i in range(0, len(chars), 2):
result.append(chars[i])
return result
def main():
characters = ["a", "e", "p", "o", "p", "w", "l", "i", "e", "f"]
assert every_two(characters) == ["a", "p", "p", "l", "e"]
print("pass the test case")
main()
```