Homework Four (retake)

Published

March 28, 2025

Due 2025-04-03 at the start of class

Download Starter Files Submit on Gradescope

This is your second shot at the problems from Homework Four. If you missed any of the problems, you are strongly advised to try again. Your goal is to complete (Satisfactory or Excellent) up to four problem across Homework Four and this set, but you are encouraged to do more – especially if you were challenged by the first set of problems. Remember that we evaluate each problem individually, so there are no issues if you only do a subset of the problems (including none).

Objectives

  • Demonstrate your ability to write a for loop
  • Demonstrate your ability to write a while loop
  • Demonstrate your ability to write a recursion function
  • Demonstrate your expanding ability to use conditional statements and manipulate strings

Getting started

For this assignment, we have another four functions for you to write. Please pay attention to all of these instructions. Even this front matter, which may look the same (and many of you skip over) has changed.

For this assignment, we have provided a starter file so that we can give you some extra helper functions. Please do all of your work in the enclosed file.

Feel free to work through the functions in any order. We do, however, suggest that you take the time to test each one as you go rather than trying to write all four out like they were part of an essay and then testing at the very end. Treat these are four totally separate and distinct problems (which they are).

Submit your solution on Gradescope using the button above. Feel free to resubmit as your complete each problem to check your progress.

Python subset

For this assignment, you now have recursion, while loops, and for loops in your toolbox. We are still working on your skills and intuition with respect to this, so read the problem description closely. Most of them specify which approach we would like you to use, and your solution should use the specified approach to receive credit.

While you are not reimplementing any core functions with this assignment, we are still getting very close to some standard methods that we would like you to continue to avoid for the time being. Unless explicitly permitted in the problem description, the only functions you are allowed are

  • the type converters (bool(), float(), int(), str())
  • string methods upper() and lower()
  • character converters ord() and char()
  • len()
  • range()

Satisfactory vs. Excellence

A solution for these questions that is excellent will have the all of the following qualities

Style

An excellent function will have a docstring that is formatted in the way shown in the lectures. It should include:

  • the purpose of the function
  • the type and purpose of each parameter (if any)
  • the type and meaning of the output (if any)

In addition, you should follow some of the PEP8 guidelines on whitespace. The ones we will be looking at are:

  • no whitespace between names and ( or [ (e.g., f (5) should be f(5) and s [3:5] should be s[3:5])
  • there should be a single space around operators (e.g., x=4+1 should be x = 4 + 1 and y = 3 -2 should be y = 3 - 2)
  • there should be a space after commas, but not before (e.g., f(4 , 5) or f(4,5) should be f(4, 5))

Special cases and requirements

For some of the problems, we have identified special cases or requirements that we have deemed potentially more challenging and not essential to a satisfactory solution. An excellent solution will cover all cases. These cases will be identified in the autograder by tests with * at the end of the title (so make sure you submit frequently as you are working).

Problem 1: censor

Write a recursive function called censor. The function takes a single string parameter text. It returns a copy of text with all of the vowels “censored out” (replaced by the ’*’).

censor
Parameters
text str
Return type str

Examples

>>> censor("fork")
'f*rk'
>>> censor("SPOOOOON!")
'SP*****N!'
>>> censor("very funny")
'v*ry f*nny'
>>> censor("banana")
'b*n*n*'

Problem 2: grid_code

Write a function called grid_code that takes in a string input text and an integer width and returns an encoded version of the string.

Our grid code is a very simple cipher. We imagine a grid (with width columns). The grid contains the alphabet, in order, one letter per cell, wrapping down to a new row when we run out of columns. We will number the columns and use letters for the rows. Each letter has a unique grid location that we can express as a row/column (letter/number) pair.

Given a width of 5. The grid would look like:

0 1 2 3 4
A A B C D E
B F G H I J
C K L M N O
D P Q R S T
E U V W X Y
F Z

Thus, "a" would map to "A0", "g" would map to "B1", and "cat" would be "A2A0D4".

Your solution should be insensitive to case (uppercase and lowercase letter should map to the same code) and anything that isn’t a letter should be passed through untouched. Your solution should use a loop.

Once again, we are supplying you with the letter_to_index and index_to_letter functions to convert letters to their numerical placement in the alphabet and back.

Hint: it is not necessary to actually construct the grid.

grid_code
Parameters
text str
width int
Return type str

Examples

>>> grid_code("z", 1)
'Z0'
>>> grid_code("z", 4)
'G1'
>>> grid_code("z", 10)
'C5'
>>> grid_code("Z", 10)
'C5'
>>> grid_code("abc ghi mno stu xyz", 5)
'A0A1A2 B1B2B3 C2C3C4 D3D4E0 E3E4F0'
>>> grid_code("pass through numbers like 42", 4)
'D3A0E2E2 E3B3E1D2F0B2B3 D1F0D0A1B0E1E2 C3C0C2B0 42'
>>> grid_code("Secrets are leaked... by punctuation!", 6)
'D0A4A2C5A4D1D0 A0C5A4 B5A4A0B4A4A3... A1E0 C3D2C1A2D1D2A0D1B2C2C1!'

Problem 3: is_alphabetical

Write a function called is_alphabetical that takes in a single string called text. text is a string containing (potentially) multiple words, separated by spaces. The function should return True if the words are in alphabetical order and False if they are not. Your implementation should use a loop.

You may assume all inputs are lowercase.

is_alphabetical
Parameters
text str
Return type bool

Examples

>>> is_alphabetical("abcdef")
True
>>> is_alphabetical("afbecd")
True
>>> is_alphabetical("abc def ghi")
True
>>> is_alphabetical("abc def def ghi")
True
>>> is_alphabetical("abc def xyz ghi")
False
>>> is_alphabetical("abc xyz def ghi")
False
>>> is_alphabetical("abc  def    ghi")
True

Problem 4: is_palindrome

Write a function called is_palindrome that takes a single string input text and returns True if the input is a palindrome and False if it is not. When we previously wrote a palindrome checker, we used a very strict interpretation that the string had to read exactly the same forwards and backwards. This checker should be looser. It should ignore case and anything that isn’t a letter or a number. Your solution should also use a loop.

is_palindrome
Parameters
text str
Return type bool

Examples

>>> is_palindrome("")
True
>>> is_palindrome("a")
True
>>> is_palindrome("racecar")
True
>>> is_palindrome("22/02/2022")
True
>>> is_palindrome("taco cat")
True
>>> is_palindrome("Was it a car or a cat I saw?")
True
>>> is_palindrome("ab")
False
>>> is_palindrome("this isn't a palindrome")
False