CS 151: Intro to Programming in Python

Problem Set 3: Strings

Due: Monday, 23 September, 11:59 PM

Github Classroom Link: https://classroom.github.com/a/ybD-IUqL


This is a problem set of 3-4 problems, all involving string operations.


Files:

1. draw_console_pyramid

  • Update (only) this file:
    • Prob1.py

2. contains_repeated_letters and longest_no_repeats

  • Update (only) this file:
    • Prob2.py
  • You will also need:
    • english.py

3. to_obenglobish

  • Update (only) this file:
    • Prob3.py

Others:

  • This problem set has "pytest" support.
  • After installing pytest via pip install pytest at command line.
  • You may run pytest via pytest at command line in your PS3 folder/directory.
  • Doing so will give you feedback on your work using the following files:
    • test_ps3.py
    • pytest.ini

Problems:

Problem 1: draw_console_pyramid

The function def draw_console_pyramid(n:int) -> NoneType: will

  1. Print 'n' lines,
  2. Print only spaces ' ' and asterisks '*' in each line,
  3. Print one (1) asterisk immediately, and then increase by two each row, and
  4. Center the previous row of asterisks over the next row.
    • We term this the "pyramid" pattern.

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***************
01234567891011121314

Problem 2: (Two Parts)

Part A: contains_repeated_letters

The function contains_repeated_letters(s:str) -> bool: will

  1. return True if any letter occurs more than once in 's'
  2. 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

Part B: 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)

Problem 3: to_obenglobish

The function to_obenglobish(s:str) -> str: will

  • Create and return a new string, which will be identical to the old string except...
  • Any instance of the letter 'a', 'e', 'i', 'o', 'u' in the string will be prefixed with 'ob', unless...
  • It follows another of these letters, which we term "vowels", or...
  • Is an 'e' and is the final letter.

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:
    • 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:

Here is demo:

__