This task is part of project02 which is due at 23:00 EDT on 2024-09-17.
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).
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).
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.
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
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 []:PrintsdiamondPattern()
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
diamondPattern
must print the correct output
diamondPattern
function is run must match the solution output.print
print
in at most 10 places.diamondPattern
with 0 parameters
def
to define diamondPattern
with 0 parametersprint
diamondPattern
with 0 parameters, do not call print
.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.)diamondPattern
with 0 parameters
def
to define diamondPattern
with 0 parametersdiamondPattern
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.if
statements
if
statements, you are not allowed to use them for this task.print
print
in at most 5 places.