In this exercise you will continue practicing control flow, boolean operators and working with strings.
Assignment Outline
- Tar Heel Arithmetic (Level: Novice) – 30 Points Autograded
- Find Duplicates (Level: Advanced) – 35 Points Autograded
- Happy Trees (On Your Own) – 15 Points Autograded
- Style, Linting, Typing – 20 Points Autograded
0. Pull the skeleton code
You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:
- Be sure you are in your course workspace. Open the file explorer and you should see your work for the course. If you do not, open your course workspace through File > Open Recent.
- Open the Source Control View by clicking the 3-node (circles) graph (connected by lines) icon in your sidebar or opening the command palatte and searching for Source Control.
- Click the Ellipses in the Source Control pane and select “Pull” from the drop-down menu. This will begin the pulling process from the course repository. It should silently succeed.
- Return to the File Explorer pane and open the
exercises
directory. You should see it now contains another directory namedex03
. If you expand that directory, you should see the starter files for the three Python programs in this exercise. - If you do not see the
ex03
directory, try once more but selecting"Pull From"
and selectupstream
in step 2.
1. tar_heels.py – 30 Points
The first program involves a small logical and arithmetic puzzle. The starter code is found in tar_heels.py
. You should prompt
the user for an integer and respond with the following logic:
- When evenly divisible by 2, print “TAR”.
- When evenly divisible by 7, print “HEELS”.
- When evenly divisible by both 2 and 7, print “TAR HEELS” instead of just “TAR” or “HEELS”
- When none of the above conditions are met, print “CAROLINA”
Consider the following demo:
$ python -m exercises.ex03.tar_heels
Enter an int: 5
CAROLINA
$ python -m exercises.ex03.tar_heels
Enter an int: 8
TAR
$ python -m exercises.ex03.tar_heels
Enter an int: 21
HEELS
$ python -m exercises.ex03.tar_heels
Enter an int: 28
TAR HEELS
The command to run your program is: python -m exercises.ex03.tar_heels
Hints:
- Consider how making use of the arithmetic remainder operator and the equality operator can allow you to write a boolean expression to determine whether an integer is evenly divisible by some number or not.
- You will need to think carefully about the ordering of your logic, boolean operators, and the rules of
if/else
statements in order to successfully implement the required logic.
2. find_duplicates.py – 35 Points
In this second part of the exercise, you will be identifying duplicate characters within a string. Your program should prompt the user for a word, and then print a message confirming or denying the presence of any duplicates.
Consider the following demo:
$ python -m exercises.ex03.find_duplicates
Enter a word: hello
Found duplicate: True
$ python -m exercises.ex03.find_duplicates
Enter a word: Kaki
Found duplicate: False
$ python -m exercises.ex03.find_duplicates
Enter a word: kaki
Found duplicate: True
$ python -m exercises.ex03.find_duplicates
Enter a word: sandwich
Found duplicate: False
The command to run your program is: python -m exercises.ex03.find_duplicates
Hints:
- Your program should be case-sensitive. This means that the same letter appearing uppercase and lowercase in the inputted word does not count as a duplicate. See the second example run of the program above.
- You may find it useful to use two loops.
A video that will provide some futher guidance on this portion and working with nested loops can be found here.
3. happy_trees.py ON YOUR OWN – 15 Points
In the final part of the exercise, you will be building some lush, beautiful forests of pine trees with code!
We are able to represent emojis in code using special strings specifing unicode characters. You can read a bit more about it below if you are curious. The main takeaway is that you can represent emojis in your code using a regular str
variable. At the top of your starter code file, you should have a named constant TREE
holding the unicode sequence for representing a tree emoji. Try printing it out and running your program. You should see a little tree appear in your terminal!
The following demo demonstrates the expected behavior of your finished program:
$ python -m exercises.ex03.happy_trees
Depth: 1
🌲
$ python -m exercises.ex03.happy_trees
Depth: 2
🌲
🌲🌲
$ python -m exercises.ex03.happy_trees
Depth: 4
🌲
🌲🌲
🌲🌲🌲
🌲🌲🌲🌲
$ python -m exercises.ex03.happy_trees
Depth: 0
You should prompt the user for an integer depth, and produce a forest of trees in this form. A depth of 1, corresponds to 1 row of 1 tree. A depth of 2 should result in 2 rows, where the second row has an additional tree. A depth of 3 produces a third row with three trees, and so on.
Additionally, if the user inputs something less than or equal to 0, no trees are printed.
Hints:
- You should make use of at least one loop in your code.
- You can refer back to the
repeat_beat
exercise for a refresher on how to build up strings within a loop.
As a reminder, the command to run your program is: python -m exercises.ex03.happy_trees
Some notes using emojis in our code!
When ASCII was decided in the 60s, it was a technical achievement to include both lower and uppercase letters in the standard. Emoji, and much more importantly large alphabet languages such as Chinese, were not possible until later. As additional characters were added to international standards, the set of total characters possible expanded well beyond ASCII’s initial 127 character specification.
For example, try the following in the REPL:
>>> chr(129312)
Hold on to your saddles, because we’re about to go on a little adventure. This is a bit outside of the scope of your concerns in COMP110, but to make use of Emoji in our programs (which is of utmost importance) there’s just a little more to the story to reveal.
Putting a hex
on large integers
The decimal system is base-10, meaning we have 10 digits ranging from 0 through 9. Notice you grew comfortable with a 0-indexing numbering scheme in elementary school! Consider the base-10 value 90
. It can be interpretted in a binary, a base-2 numeral system as 01011010
. Binary is base 2 and has only 2 digits: 0 and 1. It can also be represented in a hexademical, a base-16 numeral system, with 5A
. Hexadecimal is base 16 and has 16 digits, 0-9 followed by A-F which correspond to the decimal values of 10-15. Computer scientists love hexadecimal because each single digit corresponds to four binary digits. Notice that in the example: 01011010
, which is 8 binary digits, is equivalent to 5A
.
Python has a built-in hex
function for converting to its representation. The 0x
in front of the hexadecimal notation can be ignored and is case insensitive.
>>> hex(90)
0x5a
When looking up the codes for emoji or characters in other languages, they will tend to be presented to you in a hex format, such as on this web site. You will notice in the code column, there is a format of U+1F920
. The U+
tells you this is Unicode, a more modern international character encoding standard than ASCII. The 1F920
is a hexadecimal representation of the code for the cowboy emoji. In Python, you can use such a Unicode character in your strings as follows:
>>> print("The \U0001F920 rides a \U0001F40E!")
The 🤠 rides a 🐎!
The leading backslash begins an escape sequence, which will be discussed in depth shortly. The U
is an indication that what will follow is an 8-digit hex representation of a unicode character. Then, to encode 1F920
, we must add three leading 0
s for padding because 8 hex digits are expected.
It is worth taking a moment to appreciate that Python is doing a proper job of treating those emoji each as an individual item in our sequence of characters.
>>> emoji: str = "\U0001F920\U0001F40E"
>>> print(emoji)
🤠🐎
>>> len(emoji)
2
>>> emoji[0]
🤠
4. Make a Backup Checkpoint “Commit”
As you make progress on this exercise, making backups is encouraged.
- Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
- Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
- Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
- If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
- In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Progress on Exercise 3” will suffice.
- Press the Check icon to make a Commit (a version) of your work.
- Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.
5. Submit to Gradescope for Grading
Login to Gradescope and select the assignment named “EX03 - More Control Flow.”. You’ll see an area to upload a zip file. To produce a zip file for autograding, return back to Visual Studio Code.
If you do not see a Terminal at the bottom of your screen, open the Command Palette and search for “View: Toggle Integrated Terminal”.
Type the following command (all on a single line):
python -m tools.submission exercises/ex03
In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex03.zip”. The “mm”, “dd”, and so on, are timestamps with the current month, day, hour, minute. If you right click on this file and select “Reveal in File Explorer” on Windows or “Reveal in Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.
Autograding will take a few moments to complete. If there are issues reported, you are encouraged to try and resolve them and resubmit. If for any reason you aren’t receiving full credit and aren’t sure what to try next, come give us a visit in office hours!