Python Homework #6

Instructions:

Here's some more string functions to work on. Remember: For all of these, make sure you return the result, not just print it. You should make sure you test each function on at least 2 inputs.
  1. Write a function countLetter(text, letter). text is a string, letter is a one-character string (a letter). The function should return how many times that letter appears in the string
    # countLetter("trees", "t") -> 1
    # countLetter("trees", "e") -> 2
    # countLetter("trees", "E") -> 0
    
  2. Write a function findLetter(A, B). A is a string, B is a one-character string (a letter). This should return the index of the left-most position where B can be found in A. If the letter B does not occur in the string A, return -1.
    # findLetter("fish","f") -> 0
    # findLetter("fish","F") -> -1
    # findLetter("fish","h") -> 3
    

  3. Write a function backwards(A). A is a string. Return a string like A but going through the characters backward. For example: backwards("pig milk") should return "klim gip"

  4. Write a function emphasize(text). This should return a string like text, but with every "." changed to a "!". e.g., emphasize("Hi. You're great.") should return "Hi! You're great!"

  5. Write a function isVowel(c). This should return True if c is an uppercase or lowercase vowel, and False otherwise.

  6. Write a function countVowels(A). A is a string. This should return the number of vowels in the string A.

  7. Write a function justTheVowels(A). A is a string. Return a string with only the vowels from A extracted.
    justTheVowels("Aw, seriously??") -> "Aeiouy"
    justTheVowels("Bzzt chhhggk") -> ""
    

  8. Write a function everyOther(A, startAt). A is a string. startAt is an integer. Return a string composed of every second character of A, starting at startAt.
    # everyOther("a1b2c3",0) -> "abc"
    # everyOther("a1b2c3",1) -> "123"
    # everyOther("a1b2c3",3) -> "23"
    
    Try using it on this string: secret = "abnedf_omriel_eIs__stloe egpo"

Challenge problems:

  1. Write a function boxText2(text). This should draw a box around text, but also handle text with newlines in it correctly.
    print(boxText2("SALE\n50% OFF")) should output:
    
    /-------\
    |SALE   |
    |50% OFF|
    \-------/
    
    This is harder than it might seem. You may find it helpful to write some helper functions like countLines() and getIthLine(i).

  2. Write a function emotify(text). This should take a string of text and automatically convert "emote sequences" to smileys. An "emote sequence" will be a dollar sign followed by a letter. emotify("$h Hello! $s") could return: ":-D Hello! (._.)"

    You should support at least 3 different emote sequences, happy ($h), sad ($s), and one or more others that you come up with. Feel free to output whatever smileys you like for each of the sequences, but make sure you have a detailed comment for your emotify function describing what sequences it supports and what they output.

    NOTE: If you happen to know other string methods, try first solving this problem using only the tools we've covered in class (+, [], *, len).