All assignment work will be done in the included template files on GitHub as per usual. Please do not forget to fill out the metadata at the top of each template! At the very least fill out if you worked alongside anyone to give them proper credit.
Accept AssignmentProblem 1
Here your goal is to write a function named draw_console_pyramid
which takes a single argument equating to the desired pyramid height. Your function should then print to the terminal a pyramid (constructed of *
characters) of the specified height in rows, in which the width of each row increases by two as you move downward in the console (toward the base of the pyramid). Each of the rows should be centered with respect to the others, and the bottom row should begin at the left margin. Calling draw_console_pyramid(8)
should thus produce the following:
>>> draw_console_pyramid(8)
*
***
*****
*******
*********
***********
*************
***************
Notice how the bottom row of the pyramid starts immediately, with no space between it and the left margin, and that each row of the pyramid is centered on the others, increasing by 2 each time.
I’ve already set up the template to call this function to draw a pyramid of height 8 for your testing purposes, but you should absolutely test your function with other heights as well to ensure it is working as desired in all cases.
Hint: In order to keep things centered, you are going to need to add an amount of space characters to the front of most lines. Think about how you could assemble these strings, and how you’ll know how many characters to add. As always, decomposing the problem into smaller problems can be helpful!
Problem 2
Part A
As you will discover next week, implementing the Wordle project requires you to take account of the possibility that a word might contain more than one instance of the same letter. In preparation of that, your task here is to write a predicate function called contains_repeated_letters(word)
which returns True
if any of the characters in the word appear more than once, not necessarily consecutively. Thus,
"single") contains_repeated_letters(
should return False
, as no letter ever shows up twice. Conversely,
"repeating") contains_repeated_letters(
should return True
, as the letter "e"
shows up twice. Test your function against more words to ensure that it is working properly!
Part B
Now use contains_repeated_letters
together with the english.py
library to write a function called longest_no_repeats
that finds and returns the longest word in the English dictionary that contains no repeated letters.
Problem 3
Many people in English-speaking countries have played the Pig Latin game at some point in their lives. There are other invented “languages” in which words are created using some simple transformation of English. One such language is called Obenglobish, in which words are created by adding the letters ob before the vowels (a, e, i, o and u) in an English word. For example, under this rule, the word english gets the letters ob added before the e and the i, to form obenglobish, which is how the language got its name.
In official Obenglobish, the ob characters are only added before vowels that are pronounced, which means that a word like game would become gobame rather than gobamobe since the final e is silent. While it is impossible to implement this rule perfectly, you can do a pretty good job by adopting the rule that ob should be added before every vowel in the word except when:
- the vowel follows another vowel
- the vowel is an e occurring at the end of a word
Write a function to_obenglobish
that takes an English word as an argument and returns its Obenglobish equivalent, using the translation rules above. For example, your function should be able to output something similar to below:
>>> print(to_obenglobish("english"))
obenglobish>>> print(to_obenglobish("gooiest"))
gobooiest>>> print(to_obenglobish("amaze"))
obamobaze>>> print(to_obenglobish("rot"))
robot
Don’t forget about decomposition! I found it very useful on this problem to write predicate functions to take care of checking the above special conditions.