-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-03-03.qmd
156 lines (105 loc) · 3.58 KB
/
slides-03-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
---
title: "more on decomposition (slides)"
format: revealjs
slide-number: true
---
# CSC 110 more on decomposition
## Class activity one
Read the code, discuss and answer the question in 1-2 sentences using natural language:
- What problem does the code solve?
## Rubik’s Cubes
```{python}
#| echo: true
#| eval: false
cube_len = int(input("Cube length? "))
box_w = int(input("Box width? "))
box_h = int(input("Box height? "))
box_l = int(input("Box length? "))
fit_w = box_w // cube_len
fit_h = box_h // cube_len
fit_l = box_l // cube_len
print(str(fit_w) + " Rubik's cubes will fit width-wise.")
print(str(fit_h) + " Rubik's cubes will fit height-wise.")
print(str(fit_l) + " Rubik's cubes will fit length-wise.")
res = fit_w * fit_h * fit_l
print(str(res)+" Rubik's cubes will fit in that container.")
```
## Activity two
Read the code and write the questions:
- which lines of code are similar?
- which lines of code are unique?
- which lines can be grouped together into a function? what does the function do?
## Rubik’s Cubes
```{python}
#| echo: true
#| eval: false
cube_len = int(input("Cube length? "))
box_w = int(input("Box width? "))
box_h = int(input("Box height? "))
box_l = int(input("Box length? "))
fit_w = box_w // cube_len
fit_h = box_h // cube_len
fit_l = box_l // cube_len
print(str(fit_w) + " Rubik's cubes will fit width-wise.")
print(str(fit_h) + " Rubik's cubes will fit height-wise.")
print(str(fit_l) + " Rubik's cubes will fit length-wise.")
res = fit_w * fit_h * fit_l
print(str(res)+" Rubik's cubes will fit in that container.")
```
## Activity two -- answers
Read the code and write the questions:
- which lines of code are similar?
- line 2-4, line 6-8, line 10-12
- which lines of code are unique?
- line 1, line 14-15
## Activity two -- continued
Which lines can be grouped together into a function?
- line 2, 6, 10:
```{python}
#| echo: true
#| eval: false
box_w = int(input("Box width? "))
fit_w = box_w // cube_len
print(str(fit_w) + " Rubik's cubes will fit width-wise.")
```
What are the parameters of this function? In other words, what information must be known beforehand?
## Activity two -- answers continued
Given the length of a cube and the name of a box side:
- It prompts you to the enter the length of the side
- It calculates the number of cube that can fit in the side
- It print the message and return the number.
## Write a function
Write function `get_fit` that takes the length of a `cube` (integer) and the name of a `side` (string)
- It prompts you to the enter the length of the `side`
- It calculates the number of `cube` that can fit in the `side`
- It print the message and return the number.
Name your file as `cubes.py` and submit to Gradescope.
```{python}
#| echo: true
#| eval: false
def main():
cube_len = int(input("Cube length? "))
fit_w = get_fit(cube_len, 'width')
fit_h = get_fit(cube_len, 'height')
fit_l = get_fit(cube_len, 'length')
result = fit_w * fit_h * fit_l
print(str(result) + " Rubik's cubes will fit in that container.")
```
## Write a function -- answer
```{python}
#| echo: true
#| eval: false
def get_fit(cube, side):
large = int(input("Box {}? ".format(side)))
fit = large // cube
print("{} Rubik's cubes will fit {}-wise.".format(fit, side))
return fit
def main():
cube_len = int(input("Cube length? "))
fit_w = get_fit(cube_len, 'width')
fit_h = get_fit(cube_len, 'height')
fit_l = get_fit(cube_len, 'length')
result = fit_w * fit_h * fit_l
print(str(result) + " Rubik's cubes will fit in that container.")
main()
```