CS 21, Spring 2017

Practice Questions, Week 3 - Wednesday

  1. Write a program that tells a user what Harry Potter character they are based on their responses to a few questions:

    $ python potterquiz.py
    Can you talk to snakes? n
    Are you brave? y
    
    You're Hermione!
    
    $ python potterquiz.py
    Can you talk to snakes? n
    Are you brave? n
    
    You're Draco :(
    
    $ python potterquiz.py
    Can you talk to snakes? y
    Are you brave? n
    
    You're Voldemort :(
    
    $ python potterquiz.py
    Can you talk to snakes? y
    Are you brave? y
    
    You're Harry!
    

    Feel free to expand the program later--either by asking more questions to identify more Harry Potter characters or by coming up with a new quiz for characters from a different fictional world.

  2. Write a program that counts the number of 'e's appearing in some text entered by the user.

    $ python count-es.py
    Enter text: Cheese
    I count 3 e's.
    $ python count-es.py
    Enter text: Eric
    I count 0 e's.
    

    Expand the program so it also counts uppercase 'E':

    $ python count-es.py
    Enter text: Eric
    I count 1 e's.
    

    Expand the program again to make the output more grammatical.

    $ python count-es.py
    Enter text: Eric
    I count 1 'E'.
    
    $ python count-es.py
    Enter text: E is the most common letter.
    I count 4 'E's.
    
    $ python count-es.py
    Enter text: octopus
    No 'E's.
    
  3. Write a program that substitutes numbers for some letters in the user-entered text. If you see an 'O' or an 'o' replace it with a zero. If you see 's' or 'S' replace it with a five. Hint: use an accumulator.

    $ python changetext.py
    Enter text: noobs
    n00b5
    
  4. Write a program that counts the number of times the sequence of characters 'ing' appears in user-entered text.

    $ python count-chars.py
    Enter text: Dishing and swishing
    The sequence 'ing' appeared 2 times.
    

    Extend the program so that it counts the occurences of some user-entered sequence of characters:

    $ python count-chars.py
    Enter text: abracadabra
    Search for sequence: ab
    The sequence 'ab' appeared 2 times.
    $ python count-chars.py
    Enter text: abracadabra
    Search for sequence: a
    The sequence 'a' appeared 5 times.
    
  5. (Challenge) Write a program that counts the number of words in some user-entered text. Consider any sequence of characters that's not interrupted by one or more space characters to be a word. Hint: traverse the entered text and keep track of whether you're in a word or not in a word. When you go from being in a word to not being in a word, increment (add 1 to) the word count.