-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-05-03.qmd
237 lines (178 loc) · 4.75 KB
/
slides-05-03.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
---
title: "while loop aggregation (slides)"
format: revealjs
slide-number: true
---
# CSc 110 - while loop aggregation
## `while` loops
- Using an `index` variable:
- Defined before the loop
- Used in the **condition** of the loop
- Changed within the loop
- Using a `temporary variable` for aggregation:
- Defined before the loop
- Changed within the loop
- **Returned** outside the loop
## `while` loops with aggregation
Adding numbers from 1 to 5:
```{python}
#| eval: true
#| echo: true
total = 0
index = 1
while index <= 5:
print('adding ' + str(index))
total = total + index
index = index + 1
print(total)
```
## `while` loops table
Start with `total = 0`, `index = 1`:
| `index <= 5` | total = total + index | index = index + 1 |
| :-------: |:-------: |:-------: |
| True | total = 0 + 1 | index = 1 + 1 |
| True | total = 1 + 2 | index = 2 + 1 |
| True | total = 3 + 3 | index = 3 + 1 |
| True | total = 6 + 4 | index = 4 + 1 |
| True | total = 10 + 5 | index = 5 + 1 |
| False | - | - |
## Write a function
Its name is `sum_all` and takes two numeric arguments: `low` and `high`.
It runs a loop that iterates through the values `low` and `high` summing all values.
It returns the sum of all values between `low` and `high`.
HINT: Create a variable that will aggregate the sum. Use `while` (set the index as the `low` before the loop, use index in the `while` condition, update the index inside the loop).
```{python}
#| eval: false
#| echo: true
assert sum_all(1, 3) == 6
assert sum_all(0, 5) == 15
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def sum_all(low, high):
total = 0
index = low
while index <= high:
total += index
index += 1
return total
def main():
assert sum_all(1, 3) == 6
assert sum_all(0, 5) == 15
main()
```
## Write a function
Write a function called `count_vowels` that takes a string as argument, and returns
an integer with the number of vowels in the string.
HINT: use `in` to determine if a character is a vowel. The vowels are `"a"`,`"e"`,`"i"`,`"o"`, and `"u"`.
```{python}
#| eval: false
#| echo: true
assert count_vowels("banana") == 3
assert count_vowels("fly") == 0
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def count_vowels(string):
index = 0
count = 0
while index < len(string):
if string[index] in "aeiou":
count += 1
index += 1
return count
def main():
assert count_vowels("banana") == 3
assert count_vowels("fly") == 0
print("End of tests.")
main()
```
## Write a function
Its name is `factorial` and takes a numeric argument `number`.
It returns the factorial of `number`.
The factorial of a number is the product of that number and every number that comes before it, down to one.
For example, factorial of 4: `1 * 2 * 3 * 4 = 24`.
Name your file `factorial.py` and submit to Gradescope.
```{python}
#| eval: false
#| echo: true
assert factorial(4) == 24
assert factorial(5) == 120
assert factorial(0) == 1
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def factorial(number):
result = 1
index = 1
while index <= number:
result *= index
index += 1
return result
def main():
assert factorial(4) == 24
assert factorial(5) == 120
assert factorial(0) == 1
main()
```
## Write a function
1. Its name is `power`
2. It takes two numeric arguments: `base` and `exp`
3. It returns the `base` to the power of `exp`
4. Don't use the `**` operator, use a `while` loop (define an `index` before the loop, use `index` in the `while` condition, change `index` inside the loop)
```{python}
#| eval: false
#| echo: true
assert power(2, 4) == 16
assert power(3, 2) == 9
```
## Write a function -- solution
Write more test cases for this function.
```{python}
#| eval: true
#| echo: true
def power(base, exp):
result = 1
index = 1
while index <= exp:
result *= base
index += 1
return result
def main():
assert power(2, 4) == 16
assert power(3, 2) == 9
main()
```
## Write a function
1. Its name is `vowels_only`
2. It takes a `string` argument
3. It builds a new string containing only the vowels in the `string` argument
4. It returns new string with vowels only (define an `index` before the loop, use index in the `while` condition, change `index` inside the loop)
```{python}
#| eval: false
#| echo: true
print( vowels_only("banana") ) # "aaa"
print( vowels_only("fly") ) # ""
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def vowels_only(string):
new_string = ""
index = 0
while index < len(string):
if string[index] in "aeiou":
new_string += string[index]
index += 1
return new_string
def main():
print( vowels_only("banana") ) # "aaa"
main()
```