Skip to content

Commit fc784a8

Browse files
committed
Rename and improve challenge
- Rename and improve 'Volume of a cube' challenge. - Improve and extend answer. - Use lowercase letters for function arguments.
1 parent 45f2905 commit fc784a8

File tree

1 file changed

+74
-12
lines changed

1 file changed

+74
-12
lines changed

_episodes/04-reusable.md

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,90 @@ print(get_item_count(items_str = items_owned, sep=';'))
9898
~~~
9999
{: .output}
100100

101-
> ## Exercise
101+
> ## Volume of a cube
102102
>
103-
> 1. Write a function definition to calculate the volume of a cuboid. The function will use three parameters `H`, `W` and `L` and return the volume.
103+
> 1. Write a function definition to calculate the volume of a cuboid. The function will use three parameters `h`, `w`
104+
> and `l` and return the volume.
104105
>
105106
> 2. Supposing that in addition to the volume I also wanted to calculate the surface area and the sum of all of the edges. Would I (or should I) have three separate functions or could I write a single function to provide all three values together?
106107
>
107108
> > ## Solution
108-
> >
109+
> > - A function to calculate the volume of a cuboid could be:
110+
> >
111+
> > ~~~
112+
> > def calculate_vol_cuboid(h, w, l):
113+
> > """
114+
> > Calculates the volume of a cuboid.
115+
> > Takes in h, w, l, that represent height, width, and length of the cube.
116+
> > Returns the volume.
117+
> > """
118+
> > volume = h * w * l
119+
> > return volume
109120
> > ~~~
110-
> > def cuboid(H,W,L):
111-
> > vol = H*W*L
112-
> > sa = 2*(H*W + H*L + L*W)
113-
> > edges = 4*(H + W + L)
114-
> > return vol, sa, edges
121+
> > {: .language-python}
122+
> > - It depends. As a rule-of-thumb, we want our function to __do one thing and one thing only, and to do it well.__
123+
> > If we always have to calculate these three pieces of information, the 'one thing' could be
124+
> > 'calculate the volume, surface area, and sum of all edges of a cube'. Our function would look like this:
115125
> >
116-
> > volume, surface_area, length_of_edges = cuboid(2,3,4)
117-
> > print("Volume = ", volume, " Surface_area = ", surface_area, " Sum of edges = ", length_of_edges)
126+
> > ~~~
127+
> > # Method 1 - single function
128+
> > def calculate_cuboid(h, w, l):
129+
> > """
130+
> > Calculates information about a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
131+
> >
132+
> > Returns the volume, surface area, and sum of edges of the cuboid.
133+
> > """
134+
> > volume = h * w * l
135+
> > surface_area = 2 * (h * w + h * l + l * w)
136+
> > edges = 4 * (h + w + l)
137+
> > return volume, surface_area, edges
118138
> > ~~~
119139
> > {: .language-python}
120140
> >
121-
> > The cuboid function above returns all three values from a single call. This means that you need three variables in which to place the 3 returned values. If you do not provide three variables, you will get an error.
122-
> > Unless you are always going to use the 3 values, it would probably be better in this case to use distinct functions for these three cases.
141+
> > It may be better, however, to break down our function into separate ones - one for each piece of information we are
142+
> > calculating. Our functions would look like this:
143+
> > ~~~
144+
> > # Method 2 - separate functions
145+
> > def calc_volume_of_cuboid(h, w, l):
146+
> > """
147+
> > Calculates the volume of a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
148+
> > """
149+
> > volume = h * w * l
150+
> > return volume
151+
> >
152+
> >
153+
> > def calc_surface_area_of_cuboid(h, w, l):
154+
> > """
155+
> > Calculates the surface area of a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
156+
> > """
157+
> > surface_area = 2 * (h * w + h * l + l * w)
158+
> > return surface_area
159+
> >
160+
> >
161+
> > def calc_sum_of_edges_of_cuboid(h, w, l):
162+
> > """
163+
> > Calculates the sum of edges of a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
164+
> > """
165+
> > sum_of_edges = 4 * (h + w + l)
166+
> > return sum_of_edges
167+
> > ~~~
168+
> > {: .language-python}
169+
> >
170+
> > We could then rewrite our first solution:
171+
> > ~~~
172+
> > def calculate_cuboid(h, w, l):
173+
> > """
174+
> > Calculates information about a cuboid defined by the dimensions h(eight), w(idth), and l(ength).
175+
> >
176+
> > Returns the volume, surface area, and sum of edges of the cuboid.
177+
> > """
178+
> > volume = calc_volume_of_cuboid(h, w, l)
179+
> > surface_area = calc_surface_area_of_cuboid(h, w, l)
180+
> > edges = calc_sum_of_edges_of_cuboid(h, w, l)
181+
> >
182+
> > return volume, surface_area, edges
183+
> > ~~~
184+
> > {: .language-python}
123185
> >
124186
> {: .solution}
125187
{: .challenge}

0 commit comments

Comments
 (0)