Exercises: Functions

To solve problems with code, you need to be able to break large problems into small ones. Usually, these smaller problems will take the form of functions that are used to solve the larger problem. Therefore, to solve problems with code, you need to be skilled at writing functions. And to master functions, you need to write a lot of them.

These exercises ask you to write many relatively small functions, which combine to form larger, more complicated ones.

At the end, you will be able to create strings of shapes, like this nifty diamond:

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

Getting Started

You will find the starter code in data-analysis-projects/functions/exercises/functions-exercises.py.

Part 1: Rectangles

  1. Write a function make_line(size) that returns a line with exactly size hashes.

    print(make_line(5))

    Console Output

       #####
    
    1
    2
    3
    4
    5
    6
    7
    
    def make_line(size):
       line = ""
       for i in range(size):
          line += "#"
       return line
    
    print(make_line(5))
  2. Write a function called make_square(size) that returns a size by size string of hashes. The function should NOT print each row of the square. Instead, it must return a single string that contains the entire shape.

    Tip
    1. Call your make_line function to create each row of the square.
    2. The newline character, \n, will be helpful to you.
    3. Do NOT include a newline character at the end of your string.
    print(make_square(5))

    Console Output

       #####
       #####
       #####
       #####
       #####
    
    Warning

    For each of the shape exercises, do not include a newline character at the very end of your string. While the final \n might not be noticeable for the simpler shapes, including it will make life harder for you toward the end of the exercises.

  3. Write a function make_rectangle(width, height) that returns a rectangle with the given width and height. Use your make_line function to do this.

    print(make_rectangle(5, 3))

    Console Output

       #####
       #####
       #####
    
    1
    2
    3
    4
    5
    6
    7
    
    def make_rectangle(width, height):
       rectangle = ""
       for i in range(height):
          rectangle += (make_line(width) + "\n")
       return rectangle
    
    print(make_rectangle(5, 3))
  4. Now, go back and rewrite make_square to use make_rectangle.

Part 2: Triangles

  1. Write a function make_downward_stairs(height) that prints the staircase pattern shown below, with the given height. Use your make_line function to do this.

    print(make_downward_stairs(5))

    Console Output

       #
       ##
       ###
       ####
       #####
    
    1
    2
    3
    4
    5
    6
    7
    
    def make_downward_stairs(height):
       stairs = ""
       for i in range(height):
          stairs += (make_line(i+1) + "\n")
       return stairs
    
    print(make_downward_stairs(5))
  2. Write a function make_space_line(numSpaces, numChars) that returns a line with exactly the specified number of spaces, followed by the specified number of hashes, followed again by num_spaces more spaces.

    print(make_space_line(3, 5));

    Console Output

    ___#####___
    
    Note

    We have inserted underscores to represent spaces, so they are visible in the output. Don’t do this in your code.

  3. Write a function make_isosceles_triangle(height) that returns a triangle of the given height.

    print(make_isosceles_triangle(5))

    Console Output

           #
          ###
         #####
        #######
       #########
    
    Tip

    Consider the top line of the triangle to be level 0, the next to be line 1, and so on. Then line i is a space-line with height - i - 1 spaces and 2 * i + 1 hashes.

    1
    2
    3
    4
    5
    6
    7
    
    def make_isosceles_triangle(height):
       triangle = ""
       for i in range(height):
          triangle += (make_space_line(height - i - 1, 2 * i + 1) + "\n")
       return triangle
    
    print(make_isosceles_triangle(5))

Part 3: Diamonds

  1. Write a function make_diamond(height) that returns a diamond where the triangle formed by the top portion has the given height.

    print(make_diamond(5))

    Console Output

           #
          ###
         #####
        #######
       #########
       #########
        #######
         #####
          ###
           #
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    def make_diamond(height):
       diamond = ""
       triangle = make_isosceles_triangle(height)
       diamond += triangle[:-1]
       for i in range(len(triangle)-1, -1, -1):
          diamond += triangle[i]
       return diamond
    
    print(make_diamond(5))

Submitting Your Work

When finished, copy the URL to your GitHub repository and paste it in the submission box on the assignment page in Canvas.