Instructions for diamonds

(produced at 22:01 UTC on 2021-09-28)

This task is part of ps03 which is due at 23:59 EDT on 2021-09-28.

This is an individual task.

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. We recommend you write these descriptions before writing the code for the function.

  • 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'.

  • This task would be easy (but tedious) if you could invoke print 40 times, one for each of the 40 lines in the pattern. But we require that print may appear at most 10 times in your program (and at most 5 times for full credit). This is possible if you define helper functions to abstract over common patterns. Your goal is to make your program as simple and elegant as possible.

  • 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'.

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

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

  • In the description so far we have been referring to the function by its name only, but when you invoke it, you'll need to include the parentheses.

  • To check whether your pattern is correct, we have provided a test case and an associated expectation using the optimism module.

  • You are free to define your own test cases for your custom functions if you wish, but this is not required. Strings created within expectOutputContains function calls are an exception to the rule forbidding the creation of new strings. Just be careful, because it can be difficult to ensure that a multi-line string you enter in Python is actually the same as what gets printed, since spaces a newlines have to match exactly.

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. We recommend selecting the text below to help you see where exactly the spaces are.

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

 
unknown Style Requirements
How your code is written.
 
unknown Core goals
Complete all core goals for core credit. Get partial credit for completing at least half, and more partial credit for completing at least 90%.
 
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 Procedure Requirements
What code you use to solve the problem.
 
unknown Core goals
Complete all core goals for core credit. Get partial credit for completing at least half, and more partial credit for completing at least 90%.
 
unknown Call print
Call print in at most 10 places.
 
unknown Define diamondPattern with 0 arguments
Use def to define diamondPattern with 0 arguments
 
unknown Do not call print
Within the definition of diamondPattern with 0 arguments, do not call print.
 
unknown At least two calls to locally-defined function(s)
Within the definition of diamondPattern with 0 arguments, 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 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 Extra goals
Complete all extra goals in addition to the core goals for a perfect score.
 
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, and the expectations for any test cases you define (which must be created inside a call to expectOutputContains). 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 Define diamondPattern with 0 arguments
Use def to define diamondPattern with 0 arguments
 
unknown No more than 8 calls to locally-defined function(s)
Within the definition of diamondPattern with 0 arguments, 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 Call print
Call print in at most 5 places.
 
unknown Behavior Requirements
What your code does from the user's perspective.
 
unknown Core goals
Complete all core goals for core credit. Get partial credit for completing at least half, and more partial credit for completing at least 90%.
 
unknown diamondPattern prints the correct output
The output printed when your diamondPattern function is run must match the solution output.