Skip to content

Commit 3a3c02f

Browse files
authored
Merge pull request #167 from vinisalazar/ep04
Improving 'Volume of a cube' challenge
2 parents 0bde89b + fc784a8 commit 3a3c02f

File tree

1 file changed

+76
-14
lines changed

1 file changed

+76
-14
lines changed

_episodes/04-reusable.md

Lines changed: 76 additions & 14 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}
@@ -139,9 +201,9 @@ We need to go through a 2-step process before we can use them in our own program
139201
Step 1. use the `pip` command from the commandline. `pip` is installed as part of the Python install and is used to fetch the package from the Internet and install it in your Python configuration.
140202
141203
~~~
142-
> pip install <package name>
143-
{: .bash}
204+
$ pip install <package name>
144205
~~~
206+
{: .language-bash}
145207
146208
pip stands for Python install package and is a commandline function. Because we are using the Anaconda distribution of Python, all of the packages that we will be using in this lesson are already installed for us, so we can move straight on to step 2.
147209

0 commit comments

Comments
 (0)