Activity #3: Making Diamonds¶
Tip
For best results, complete the Functions chapter before starting this activity.
One good way to solve large problems is to split them into smaller tasks. With code, these smaller pieces are solved by writing functions. Combining the results from each function allows us to complete the bigger task.
To master functions, you need to write a lot of them.
In this activity, you will write lots of small functions and combine their output. By the end, you will create “shape strings”, like this nifty diamond:
#
###
#####
#######
#########
#########
#######
#####
###
#
Part 1: Warm Up (Lines, Rectangles, and Squares)¶
Review the make_line()
, make_rectangle()
, and make_square()
functions you wrote for the chapter 10 exercises. These
provide a good reminder for how to return a value from a function, how to use
the accumulator pattern, and how to call one function from inside another.
Recall that the functions do NOT print anything. Instead, each one returns a complete string, which can be assigned to a variable.
Each function definition includes a default character for making the shape.
If a function call leaves out the string argument, the returned value uses the
#
character.
Function Call |
Returned Value |
Printed Output |
---|---|---|
|
|
aaaa
|
|
|
#####
#####
|
|
|
$$$
$$$
$$$
|
Note how the returned strings make use of the newline character (\n
).
Part 2: Making Triangles¶
Your next step is to build some more complicated shapes.
Write a function called
make_space_line()
that returns a line with a given number of spaces, followed by a given number of characters, followed by another set of spaces.The examples below use dots to represent the spaces. Do NOT do this in your code. Use actual whitespace instead.
The function call may or may not include a character. Be sure to assign a default value when you define the function.
¶ Function Call
Returned Value
make_space_line(5, 3)
".....###....."
make_space_line(3, 2, 'C')
"...CC..."
Code a
make_triangle()
function that callsmake_space_line()
and returns a triangle of a given height.¶ Function Call
Returned Value
Printed Output
make_triangle(3)
"#\n###\n#####"
# ### #####
make_triangle(4, '*')
"*\n***\n*****\n*******"
* *** ***** *******
The trickiest part of the
make_triangle()
function is relating the height to the number of spaces/characters in each line. Here are some hints:Call the top line of the triangle row 0, the next row 1, etc.
The number of characters (like
#
) in each line is2*row + 1
.Each row has the same TOTAL length (spaces + characters).
Look at the bottom row. What is the relationship between the height of the triangle and the total line length? (Your teacher has the answer).
Knowing the total length and the number of characters for a row, how many spaces belong on each end of the row?
Part 3: Make a Diamond¶
Your final step is to write a function called make_diamond()
that behaves
like this:
Function Call |
Returned Value |
Printed Output |
---|---|---|
|
|
#
###
#####
#####
###
#
|
|
|
$
$$$
$$$$$
$$$$$$$
$$$$$$$
$$$$$
$$$
$
|
Note that the integer in the function call determines the height for the top half of the shape.
Hints:
Don’t repeat yourself! Remember that functions can call other functions. The diamond shape is just one triangle stacked on top of another.
Do you remember coding the reverse_string() function?