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.

Sample Results

Function Call

Returned Value

Printed Output

make_line(4, 'a')

"aaaa"

aaaa

make_rectangle(5, 2)

"#####\n#####"

#####
#####

make_square(3, '$')

"$$$\n$$$\n$$$"

$$$
$$$
$$$

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.

  1. 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.

    1. The examples below use dots to represent the spaces. Do NOT do this in your code. Use actual whitespace instead.

    2. The function call may or may not include a character. Be sure to assign a default value when you define the function.

    Sample Results

    Function Call

    Returned Value

    make_space_line(5, 3)

    ".....###....."

    make_space_line(3, 2, 'C')

    "...CC..."

  2. Code a make_triangle() function that calls make_space_line() and returns a triangle of a given height.

    Sample Results

    Function Call

    Returned Value

    Printed Output

    make_triangle(3)

    "#\n###\n#####"

      #
     ###
    #####
    

    make_triangle(4, '*')

    "*\n***\n*****\n*******"

       *
      ***
     *****
    *******
    
  3. 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:

    1. Call the top line of the triangle row 0, the next row 1, etc.

    2. The number of characters (like #) in each line is 2*row + 1.

    3. Each row has the same TOTAL length (spaces + characters).

    4. 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).

    5. 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:

Sample Results

Function Call

Returned Value

Printed Output

make_diamond(3)

"#\n###\n#####\n#####\n###\n#"

  #
 ###
#####
#####
 ###
  #

make_diamond(4, '$')

"$\n$$$\n$$$$$\n$$$$$$$\n$$$$$$$\n$$$$$\n$$$\n$"

   $
  $$$
 $$$$$
$$$$$$$
$$$$$$$
 $$$$$
  $$$
   $

Note that the integer in the function call determines the height for the top half of the shape.

Hints:

  1. Don’t repeat yourself! Remember that functions can call other functions. The diamond shape is just one triangle stacked on top of another.

  2. Do you remember coding the reverse_string() function?