This project must be submitted to Gradescope by 6:00pm CST on Friday, February 26, 2021.
This project must be completed individually. Seeking or giving aid on this assignment is prohibited; doing so constitutes academic misconduct which can have serious consequences. The only resources you are allowed to consult are the ones listed below. It is very important to follow these rules. If you are unsure about whether something is allowed, ask. The course syllabus contains more information about the course and university policies regarding academic honesty.
Contact the instructor or TA by email, or visit office hours.
Contact the instructor or TA by email, or visit office hours.
This is a summary of how the 25 points on the project will be divided.
Points | Item |
---|---|
4.0 | manual code review |
4.0 | autograder basic tests (files present, syntax, etc) |
7.5 | autograder tests of is_egg |
3.5 | autograder tests of is_superegg_recursive |
3.5 | autograder tests of is_superegg_iterative |
2.5 | autograder tests of is_hyperegg |
25.0 | total |
This project focuses on recursion, both as a programming tool and as a concept that is used in mathematical definitions. This project is also more theoretical than Project 1. Instead of providing long and highly detailed instructions for many classes and methods, this project asks you to write just a few functions that do more complicated things.
The project is based on some definitions of types of strings, which were made up for this project. You'll need to review the definitions below carefully to complete the project.
For the purposes of this assignment, we'll say that a string is an egg if its length is 3, and if the last two characters are equal. Thus:
"egg"
is an egg"fee"
is an egg"755"
is an egg"888"
is an egg (no requirement that the first character is different!)"wit"
is not an egg (last two characters not equal)"stratospheric"
is not an egg (length not equal to 3)"id"
is not an egg (length not equal to 3)"x"
is not an egg (length not equal to 3)Note that another way to describe an egg is to say that it can be written as A+B+B
where A
and B
are one-character strings.
(The reason for the name is that "egg" is a common noun in English that is an example of this class of strings.)
We say that a string is a superegg if it is possible to write the string as a concatenation A + B + B
where A
and B
are nonempty strings satisfying:
A
is either a single character or is itself a superegg, andB
is a single characterNotice that this definition is recursive (supereggs are defined in terms of other supereggs). Here are some examples.
A
and B
to be single characters"abbcc"
is a superegg, because you can write it as A+B+B
with A="abb"
(which is an egg, and hence a superegg) and `B="C""noooooo"
is a superegg; it splits as A+B+B
with A="noooo"
and B="o"
, so we just need to show that "noooo"
is a superegg:"noooo"
is a superegg because it splits as A+B+B
with A="noo"
and B="o"
, and this A
is an egg, hence a superegg."7225533"
is a superegg (as you can check)"waaa"
is not a superegg. If it was, then it would need to split as A+B+B
with A="wa"
, but that is neither a superegg nor a single character."reconsidering"
is not a superegg, because it doesn't end with two copies of the same character (whereas any superegg must, as B
is a single character)"foofee"
is not a superegg, because it would need to split as A+B+B
with A="foof"
, and "foof"
is neither a single character nor a superegg (because it doesn't end with two copies of the same character).We say that a string is a hyperegg if it is possible to write the string as a concatenation A + B + B
where A
and B
are nonempty strings satisfying:
A
is either a single character or is itself a hyperegg, andB
is either a single character or is itself a hypereggExamples:
B
to be a single character"fooss"
is a hyperegg, taking A="foo"
(which is a hyperegg) and B="s"
"alooloo"
is a hyperegg, taking A="a"
and B="loo"
(which is a hyperegg)"feefoofoo"
is a hyperegg, taking A="fee"
(which is a hyperegg) and B="foo"
(which is a hyperegg)"aseeseetoomiimiitoomiimii"
is a hyperegg (you should check this!)"gullability"
is not a hyperegg, because there is no way to express it as A+B+B
with B
a nonempty string"deedotdot"
is not a hyperegg, because the only way to express it as A+B+B
with B
a nonempty string is to take B="dot"
, and "dot"
is not a hyperegg; here's why:"dot"
were a hyperegg, then it would need to be expressed as A+B+B
with A
and B
each being a single character, for otherwise the concatenation would have more than 3 characters. But then it would follow that the last two characters are equal, which is not the case for "dot"
.Create a module eggs
(in a file eggs.py
) that contains the four functions documented below. Submit eggs.py
to gradescope.
is_egg(s)
True
if s
is an egg, otherwise returns False
.is_superegg_iterative(s)
True
if s
is a superegg, otherwise returns False
.is_superegg_recursive(s)
True
if s
is a superegg, otherwise returns False
.is_hyperegg(s)
True
if s
is a hyperegg, otherwise returns False
.For all of these functions, you can assume that the only argument, s
, is a string. You do not need to do any checking to detect an argument of another type.
Also, for functions that are required to use recursion, loops are permitted in the function body. However, a purely iterative solution it not acceptable.
To receive full credit, your code must also follow the additional requirements in the next section.
This section contains rules your code must follow, in addition to it needing to do everything documented in the previous section.
Like everything you submit for credit in MCS 275, your code must follow the rules in the course coding standards document.
eggs.py
¶The file eggs.py
should define the necessary functions, and when imported as a module, it should not do anything else. That is, running the Python REPL and typing
>>> import eggs
should succeed (no exceptions), should make the functions eggs.is_hyperegg
etc. availabe, and should not print or do anything else.
It is permissible to put some test code in eggs.py
outside of all the function bodies as long as you put it inside a block that looks like this:
if __name__=="__main__":
# TEST CODE HERE
(The line containing if
should not be indented at all.) This allows you to create tests that will run when the file is executed as a script, e.g. with
python eggs.py
but which are not run when it is imported as a module.
Your functions should allow anything that Python considers to be a character to appear in the string. You must not impose extra assumptions (e.g. assuming all of the characters will be in the set A-Z
, a-z
, or 0-9
would be an error).
Code you submit for this project is not allowed to import the re
module. (That's a module that contains functions which perform pattern matching on strings, potentially allowing you to circumvent the difficult parts of this assignment.)
I don't think you will need to import any modules at all in your solution, but the following modules are permitted if you find them useful for any reason (perhaps for testing):
sys
os
math
random
If you want to use a module that is not listed above, contact the instructor to request approval. If approval is given, the project description will be updated accordingly. Submissions that import modules not listed on the project description will be penalized.
It is strongly recommended that you test the functions in your eggs
module against the following sample data. Each is a text file containing one string per line. You'll need to write your own Python script to read these files line by line and check whether your functions behave as expected on these inputs.
Remember that when you read a line of text from a text file, it usually has a newline character at the end (which needs to be removed before you check whether it is an egg/superegg/hyperegg). The recommended way to deal with this is to check for a newline character at the end of a line and remove it if present, e.g. using code like
# Suppose you've already read one line of text from the file
# and stored it in a variable called `line`
if line and line[-1] == "\n": # (`line` nonempty) and (`line` ends with "\n")
# remove the trailing newline
line = line[:-1]
# Now use `line`
You'll want to download (save) these files, rather than just view them in your browser.
In response to a student question about Project 2, I created the following diagram showing that the string on line 643 of hypereggs.txt is in fact a hyperegg:
Each line above is a statement that it is a hyperegg, given what is known from the lines above it. The colors highlight A, B, and B.
The autograder is available in Gradescope as of 2021-02-20.