Github Classroom Link: https://classroom.github.com/a/ybD-IUqL
This is a problem set of 3-4 problems, all involving string operations.
draw_console_pyramid
contains_repeated_letters
and longest_no_repeats
to_obenglobish
pip install pytest
at command line.
pytest
at command line in your PS3 folder/directory.
draw_console_pyramid
The function def draw_console_pyramid(n:int) -> NoneType:
will
How I solved it. I used a 'for' loop over the number of rows, computed the number of space ' ' and asterisk '*' characters using the current and total number of rows, and used string multiplication and addition to build each row, which I then printed. There are at least two other very good solutions.
>>> draw_console_pyramid(8)
*
***
*****
*******
*********
***********
*************
***************
It helped me to make this table. I denote spaces as empty and count from zero:
0 | * | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | * | * | * | ||||||||||||
2 | * | * | * | * | * | ||||||||||
3 | * | * | * | * | * | * | * | ||||||||
4 | * | * | * | * | * | * | * | * | * | ||||||
5 | * | * | * | * | * | * | * | * | * | * | * | ||||
6 | * | * | * | * | * | * | * | * | * | * | * | * | * | ||
7 | * | * | * | * | * | * | * | * | * | * | * | * | * | * | * |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
contains_repeated_letters
The function contains_repeated_letters(s:str) -> bool:
will
return True
if any letter occurs more than once in 's'
return False
for all other cases.
Note that these are return values, and not printed values.
How I solved it. I used a 'for' loop over the number of letters in 's', and checked if the letter at index 'i' was present in the rest of the string 's' excluding position 'i'. There are many other good solutions.
>>> contains_repeated_letters("") # no repeats
False
>>> contains_repeated_letters("single") # no repeats
False
>>> contains_repeated_letters("repeating") # 'e' repeats
True
>>> contains_repeated_letters("!!") # '!' repeats
True
longest_no_repeats
The function longest_no_repeats() -> str:
will return the longest word in 'ENGLISH_WORDS', which is defined in the template file.
How I solved it. I started with 'candidate' (""), then used a 'for' loop of ENGLISH_WORDS, checking for greater length and repeated characters to determine if there were a new 'candidate'. At the end of the loop, I returned whatever the current 'candidate' was. There are many other good solutions.
It is likely non-obvious how to interact with ENGLISH_WORDS. This example code shows how to print every word. It took my computer 4 seconds.
def print_every_word():
for word in ENGLISH_WORDS:
print(word)
to_obenglobish
The function to_obenglobish(s:str) -> str:
will
How I solved it. I made a variable to track whether the previous letter was a vowel and a variable to contain the list to return. I looped over the letters in the argument/parameter string (the string that was provided) and then added each letter to the return variable, with the prefix if I encountered (1) vowel, with (2) no preceding vowel, that was not (3) an 'e' in the last position.
Here is another way of describing this problem:
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:
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:
Here is demo:
__