Instructions for diamonds

(produced at 21:45 UTC on 2024-01-30)

This task is part of project02 which is due at 23:00 EST on 2024-02-06.

You have the option to work with a partner on this task if you wish. Working with a partner requires more work to coordinate schedules, but if you work together and make sure that you are both understanding the code you write, you will make progress faster and learn more.

You can download the starter code for this task using this link.

You can submit this task using this link.

Put all of your work for this task into the file diamonds.py
(which is provided among the starter files)

This task will help you think about how functions can be used to store and replay code, and how parameters can make that process even more flexible. With the right approach, the entire printed pattern can be produced by a program that only includes a single line of code which uses the print function.

Your goal is straightforward: In the provided file, define a zero-parameter function named diamondPattern. When diamondPattern is invoked, it should print the pattern of digits shown in this example.

Notice that the starter code contains the following variable definitions:

# pre-defined strings
zero  = '     '
one   = '  1  '
two   = ' 2 2 '
three = '3 3 3'
empty = ''

By combining only these strings and no others, diamondPattern must print out the pattern shown in the example. You must also have no more than 10 places in your program where the print function is called. Of course, because those lines of code may be run several times if they are in a function which is called more than once, your code will end up calling print exactly 40 times, once for each line of the output pattern.

You will need to define some of your own functions besides just diamondPattern to solve this problem, and it is up to you how to define those functions. As a minimum requirement, your diamondPattern function must itself contain at least two function calls to other custom functions you have defined (this could be two calls to a single other function, or two calls to two different functions, as long as there are two different function calls). In addition, your diamondPattern function may not call print directly (i.e., calls to print may appear in helper functions called by diamondPattern, but not within diamondPattern itself).

Notes

  • Remember that every function you write must include a documentation string (docstring) that describes what it does.

  • There are possible solutions where one of the provided strings may not be necessary.

  • You may concatenate the provided strings to form larger strings, but you are not allowed to create any new strings delimited by quotation marks. You may not use the newline character '\n'.

  • In this problem, you are not allowed to use features of Python that we have not studied yet, even if you know them. In particular, you are not allowed to use conditionals (i.e., if statements) or loops (i.e., for loops or while loops).

  • You may not define any functions within other functions. This isn't a useful thing to do in any case. Each of your functions should be defined one-by-one without any indentation before the 'def'.

  • For a perfect score, you must also make no more than 8 function calls of any kind within the diamondPattern function itself (though the functions that it calls may make more).

  • As an extra goal, you may call print in no more than 5 places throughout your file, rather than 10.

  • This problem has many possible solutions. There is a particularly elegant solution that calls print in only one place. While we don't require you to do this, you might want to challenge yourselves.

  • To check whether your pattern is correct, you can call it in the shell by typing diamondPattern() and hitting enter. We have also provided a test case in the test_diamonds.py file. When you run that file, you should see the following text appear in red:

    ✓ test_diamonds.py:24
    

    If your pattern is not correct, you will see an explanation of where the first difference was found (or what the error was if there was an error).

Hints

The goal of this problem is two-fold: to recognize patterns, and to write functions that can generate these patterns and combine them together to produce the complete pattern. We strongly recommend that you spend some time thinking about how to break the drawing into different patterns that can be captured by simple functions. If you are having difficulty recognizing patterns, we have a diagram to help you with this process. Access the diagram only after pondering on the pattern on your own.

Show the diagram Description: The same pattern shown in the example, including extra lines that divide it into symmetric parts. The first two parts can be combined to form the third, and that combined with itself forms the fourth part. The whole pattern can be thought of as two copies of the last part.
  1         1         1         1  
 2 2       2 2       2 2       2 2 
3 3 3     3 3 3     3 3 3     3 3 3
 2 2       2 2       2 2       2 2 
  1         1         1         1  

       1         1         1         1  
      2 2       2 2       2 2       2 2 
     3 3 3     3 3 3     3 3 3     3 3 3
      2 2       2 2       2 2       2 2 
       1         1         1         1  

  1         1         1         1  
 2 2       2 2       2 2       2 2 
3 3 3     3 3 3     3 3 3     3 3 3
 2 2       2 2       2 2       2 2 
  1         1         1         1  
       1         1         1         1  
      2 2       2 2       2 2       2 2 
     3 3 3     3 3 3     3 3 3     3 3 3
      2 2       2 2       2 2       2 2 
       1         1         1         1  

  1         1         1         1  
 2 2       2 2       2 2       2 2 
3 3 3     3 3 3     3 3 3     3 3 3
 2 2       2 2       2 2       2 2 
  1         1         1         1  
       1         1         1         1  
      2 2       2 2       2 2       2 2 
     3 3 3     3 3 3     3 3 3     3 3 3
      2 2       2 2       2 2       2 2 
       1         1         1         1  
  1         1         1         1  
 2 2       2 2       2 2       2 2 
3 3 3     3 3 3     3 3 3     3 3 3
 2 2       2 2       2 2       2 2 
  1         1         1         1  
       1         1         1         1  
      2 2       2 2       2 2       2 2 
     3 3 3     3 3 3     3 3 3     3 3 3
      2 2       2 2       2 2       2 2 
       1         1         1         1  

Examples

The diamond pattern

This example shows what the output pattern should look like. Note that there is a tiny amount of flexibility: extra spaces are allowed at the end of a line.

In []:
diamondPattern()
Prints
1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1

Rubric

Group goals:
 
unknown All functions are documented
Each function you define must include a non-empty documentation string as the very first thing in the function.
 
unknown Do not define functions inside of other functions
None of your function definitions may be placed inside of other function definitions.
 
unknown Do not use quotes to create any new strings.
Other than the provided 5 strings stored in variables, do not use single, double, or triple quotes to create any string values except the docstrings for your functions. You will need to use operations on the provided string values to construct other strings and store them in variables, but you should not define any new strings using quotation marks.
 
unknown diamondPattern must print the correct output
The output printed when your diamondPattern function is run must match the solution output.
 
unknown Call print
Call print in at most 10 places.
 
unknown Define diamondPattern with 0 parameters
Use def to define diamondPattern with 0 parameters
 
unknown Do not call print
Within the definition of diamondPattern with 0 parameters, do not call print.
 
unknown At least two calls to locally-defined function(s)
Within the definition of diamondPattern with 0 parameters, call at least one custom function at least twice, or at least two different custom functions at least once each. (You are welcome to call more functions or call them more than twice.)
 
unknown Define diamondPattern with 0 parameters
Use def to define diamondPattern with 0 parameters
 
unknown No more than 8 calls to locally-defined function(s)
Within the definition of diamondPattern with 0 parameters, don't make more than 8 calls to locally-defined functions. Note that you may indirectly make as many calls as you like, only calls within diamondPattern itself are limited.
 
unknown Do not use if statements
We haven't covered these yet, but even if you know how to use if statements, you are not allowed to use them for this task.
 
unknown Do not use loops
We haven't covered these yet, but even if you know how to use loops, you are not allowed to use them for this task.
 
unknown Call print
Call print in at most 5 places.