COMP 208
Winter Semester 2025
INSTRUCTORS: DR. Isabeau Premont-Schwarz and DR. Chad Zammar
Assignment 2: Exploring Inputs, Outputs, and Loops.
Due date: 21 February 2025, 11:59 PM.
Before you start:
• Use your IDE (Thonny) to write and debug your solution. Only use Ed Lessons to
submit the code.
• Use only Python material covered in class. No advanced topics are allowed.
• If you get stuck, please see a T.A. or an instructor during office hours. When you come
to office hours, please do not ask something like “Here is my program. What’s wrong
with it?” Instead, try first to narrow down where the error is occurring and be ready
to explain roughly what the problem is. We strongly encourage you to try to use the
debugger.
• Policy on Academic Integrity: The assignments are to be done individually. While we
encourage you to discuss the assignment with each other verbally, you should not tell
each other how to solve the problem, and under no circumstances should you share
your code. See the Course Outline Sec. 6 for the policies. You are expected to read
this document. If you have any questions, then please ask.
• Late policy 10k % points out of 100 will be deducted if you submit k days of delay. So
one day late means 10 points, two days late means 20 points, etc, up to 4 days late
which is 40 points. Assignments will not be accepted after 4 days late.
• Note that submitting one minute late is equivalent to submitting 23 hours and 59
minutes late, etc. So, make sure you are nowhere near that threshold when you
submit.
Assignment 2 learning objectives
• Looping techniques
• Helper functions
• Doctest
Question 1 - Text Analyser
Write a Python function called analyze_text() that takes a string of text as input and
performs the following two analyses:
1. Word Count: Determine the total number of words in the text.
2. Most Frequent Letter: Identify the letter that appears the most frequently in the
text.
To accomplish these tasks, your analyze_text() function should call two separate helper
functions:
1. count_words(text): This function takes a string input text and returns the total
number of words.
2. find_most_frequent_letter(text): This function takes a string input text and returns
the letter that appears most frequently within the text. In the case of a tie, where
multiple letters have the same highest frequency, the function will return the letter
that comes first alphabetically. The function should ignore case sensitivity and only
consider alphabetic characters (i.e., it should disregard numbers, punctuation, and
whitespace).
Your analyze_text() function should then return both the word count and the most
frequent letter as a tuple, like this:
(word_count, most_frequent_letter)
Assumptions:
• The input text will only contain alphabetic characters (no special characters or
numbers).
For example, if you call analyze_text("The quick brown fox jumps over the lazy dog."), it
should return:
(9, 'o')
Provide doctest example covering the previous scenario.
Question 2 - Perfect Three
The number Three (3) is important in Mathematics. Do you know that sometimes it is
possible to obtain Three (3) different positive integers a, b, c (greater than 1) that satisfy the
“perfect Three (3)” equation d3 = a3 + b3 + c3 for a given integer number d?
For example, the equations 63 = 33 + 43 + 53, and 123 = 63 + 83 + 103 are just two examples
that satisfy the equation. Your task is to write a function called perfect_three that gets one
integer as a parameter (i.e., the integer represents the variable d in the “perfect Three”
equation) and it returns True if there exist three different integers (i.e., a, b, and c) that satisfy
the equation. Suppose it is not possible to find those different integers. In that case, your
program must return False (e.g., a call for perfect_three must return False given that no three
distinct positive integers satisfy the equation).
Provide doctest examples covering the following scenarios:
Examples (as executed in Thonny):
>>>perfect_three(6)
True
>>>perfect_three(12)
True
>>>perfect_three(1)
False
Question 3 - Collatz Conjecture
The Collatz Conjecture, also known as the 3n+1 problem, is a mathematical problem that has
been studied for decades. The conjecture states that for any positive integer n, if we
repeatedly apply the following operation:
• If n is even, divide it by 2.
• If n is odd, multiply it by 3 and add 1.
Then, the sequence will eventually reach the value 1, regardless of the starting value of n.
For example, if we start with n = 6, the sequence would be:
6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
Your task is to write a function collatz_sequence(n, j) that computes the first j terms of the
Collatz sequence for the given value of n. If the sequence reaches 1 before j terms, the
function should return the number of terms it took to reach 1. If the sequence does not reach
1 within j terms, the function should return -1.
Outside the function, prompt the user to enter a value for n and j. If n <= 0 or j <= 0, print
"Invalid input." and end the program. Otherwise, call collatz_sequence(n, j) with the user's
inputs and print the result to the screen.
Provide doctest examples covering the following scenarios:
Example 1:
Enter a positive integer n: 6
Enter a positive integer j: 8
The sequence reached 1 after 8 terms.
Example 2:
Enter a positive integer n: 0
Enter a positive integer j: 5
Invalid input.
Example 3:
Enter a positive integer n: 27
Enter a positive integer j: 10
The sequence did not reach 1 within 10 terms.
Question 4 - SETI Communication
The SETI program to communicate with extraterrestrial life has successfully established
contact with alien lifeforms. We do not share a common language with aliens, but numbers
are universal. The aliens use polymethacrylate containing pendent azobenzene-naphthalene
to store their data (for more info see this link), and those can have 3-states instead of the
usual 2 we humans typically use. As such, aliens use ternary coding (base 3) instead of binary
as we do. They use the following symbols:
0: ߷ (Unicode 07F7)
1: ༚ (Unicode 0F1A)
2: ༔ (Unicode 0F14)
As the information-theory expert of the SETI program, you must write code to convert
between the human binary and alien ternary and back. With two functions that take the
binary/ternary form as strings and output the form also as a string. The functions will be
called to_binary and to_ternary.
Example 1:
>>> to_binary(‘’༚߷߷”)
‘1001’
Example 2:
>>> to_ternary(‘101’)
‘༚༔’
Example 3:
>>> to_binary(to_ternary(‘10101’))
‘10101’
Example 4:
>>> to_ternary(to_binary(‘༔߷༚’))
‘༔߷༚’
Grading & Rubric
Test cases
The test cases total to 100 points. To get full marks, you must pass all the test cases. Some
tests are given to you (public), and you will receive feedback on whether or not you passed
them. These public tests will count for 50 points of your grade. But you won’t know if you
passed the private tests until we grade your assignment.
Style marks
You must satisfy various style conditions. Style points will be subtracted from your test case
grade as follows:
• -3: no docstring
Include a docstring for all the functions you write, including helper functions. The
docstring must follow the guidelines given in the lecture. It must have at least two
examples of calls to the function.
• -1: partial/incomplete docstrings
Missing some elements of the docstring.
• -1: complicated and/or unreadable expressions
• -1: very long lines
One should not have to scroll horizontally to read the code.
• -1: too many or too few comments
Place comments sparingly, e.g. when there is a complicated piece of logic. This will
help you when you take a break from your code and return to it later. It will also
help the T.A. if they need to understand your code.
Don’t put comments for things that are obvious e.g. explain basic features of the
Python language.
• -1: unnecessary global variables
• -1: code repetition
Use helper functions to eliminate code repetition.
• -1: long or unnecessary (chain of ) conditional statements.
• -1: comparing boolean expressions to True or False
• -1: redundant variables
Don’t introduce variables that aren’t needed, unless they represent a computed
value that should be named so that the code is more clear.
• -1: too much vertical spacing
We will only take the point off in extreme cases for this and the following one
• -1: no vertical spacing where it would be helpful
We will also take points off for the following issues:
• -5: used material not seen in class
• -15: submission had to be manually fixed and re-uploaded to correct a serious
error
Think of this as an admin fee.
• -5: otherwise not following assignment instructions