Computer Science 15-112, Fall 2012
Class Notes:  Practice (through week 4)


More Strings Problems
Do not use lists or recursion

  1. capitalizeFirstLetters
    Write the function capitalizeFirstLetters(s) which takes a string, s, and returns the original string with the first character of every word capitalized. For example, capitalizeFirstLetters("hello, my name is john.") will return "Hello, My Name Is John." Note: A new word starts after one or more spaces. Non-letters are capitalized as themselves. Punctuation does not affect the start of a new word. For example: capitalizeFirstLetters("don't do that (please)") returns "Don't Do That (please)" and not "Don'T Do That (please)" or "Don't Do That (Please)".
     
  2. interleave
    Write the function interleave(s1, s2) that takes two strings, s1 and s2, and interleaves their characters starting with the first character in s1. For example, interleave('pto', 'yhn') would return the string "python". If one string is longer than the other, concatenate the rest of the remaining string onto the end of the new string. For example ('a#', 'cD!f2') would return the string "ac#D!f2". Assume that both s1 and s2 will always be strings.
     
  3. mostFrequentLetter
    Write the function mostFrequentLetter(s) that takes a string s and returns the letter that occurs the most frequently in it. Your test should be case-insensitive, to "A" and "a" are the same, though your return value should always be uppercase. And if there is a tie, you should return a string with all the most frequent letters in alphabetic order. You should ignore non-letters. And if there are no letters, you should return the empty string.  
  4. hasBalancedParentheses
    Write the function hasBalancedParentheses, which takes a string and returns True if that code has balanced parentheses and False otherwise (ignoring all non-parentheses in the string).  We say that parentheses are balanced if each right parenthesis closes (matches) an open (unmatched) left parenthesis, and no left parentheses are left unclosed (unmatched) at the end of the text.  So, for example, "( ( ( ) ( ) ) ( ) )" is balanced, but "( ) )" is not balanced, and "( ) ) (" is also not balanced.
     
  5. encodeOffset
    Write the function encodeOffset(s, d) that takes a string and a possibly-negative int offset d (for "delta"), and returns the string formed by replacing each letter in s with the letter d steps away in the alphabet. So: encodeOffset("ACB", 1) return "BDC" encodeOffset("ACB", 2) return "CED" This works with wraparound, so: encodeOffset("XYZ", 1) returns "YZA" And with negative offsets, so: encodeOffset("ABC", -1) returns "ZAB" And the wraparound repeats with d>26, so: encodeOffset("ABC", -27) returns "ZAB" And it is case-preserving, so: encodeOffset("Abc", -27) returns "Zab" And it does not affect non-alphabetic characters (non-letters), so: encodeOffset("A2b#c", -27) returns "Z2a#b"
     
  6. decodeOffset
    Write the function decodeOffset(s, d) that takes a string that was encoded by encodeOffset using the given offset d, and returns the original string.
     
  7. Poker Hands
    Background: This problem involves 5-card hands in the game of poker, represented as strings. Hands are represented by 5 cards separated by spaces, with each card composed of two characters, the first for its rank and the second for its suit. There are 13 ranks. We use "T", "J", "Q", "K", and "A" to represent the ranks Ten, Jack, Queen, King, and Ace, and the characters "2"-"9" for ranks 2-9. There are four suits: Clubs, Diamonds, Hearts, and Spades. The first letter of each suit is used to represent the suit. For example, "JD JS TH 2D 5C" is a hand with a Jack of Diamonds, a Jack of Spades, a Ten of Hearts, a Two of Diamonds, and a Five of Clubs. You will write 4 functions that deal with these hands.
     
    1. isValidHand  This function takes a string and returns True if it follows the above specification, and False otherwise. For example, if the hand does not have exactly 5 cards, if the cards are not separated by exactly one space, or if a rank or suit is not valid, return False. You do not need to check that the 5-card hand could have come from a standard deck (so "KD KD KD KD KD" is a valid hand!).
       
    2. isFlush  This function takes a string and returns True if it is a valid hand and all of its cards have the same suit. It returns False otherwise.
       
    3. isRoyalFlush  This function takes a string and returns True if the hand is a royal flush, and False otherwise. A royal flush has three properties: it is a valid hand, all of its cards have the same suit, and there is exactly one of each of the following ranks: Ten, Jack, Queen, King, and Ace. These cards can be in any order.
       
    4. hasPair  This function takes a string and returns True if it is a valid hand and that hand contains at least two cards of the same rank, and False otherwise. Note that this does not correspond exactly to the Poker hand of "one pair", since hasPair(hand) will return True for a hand with three-of-a-kind.  For example, hasPair("KD KS 7H 8C KC") returns True.
       
  8. pigLatin
    Background: Pig Latin is an alteration of English. To make a word in Pig Latin, take an English word, move the leading consonants to the end, followed by " ay " (e.g. " school " -> " oolschay "). If the word begins with a vowel, or if it contains no vowels, then simply add " ay " to the end (e.g. " in " -> " inay ").

    Write a function named pigLatin which takes a string of English words and translates it into Pig Latin. The function should return the result as a string. You should treat "qu" as a single consonant, and "y" should be treated as a vowel. Words are divided by spaces, and any non-alphabetic characters (punctuation, numbers, etc.) inside a word should remain where they are. If outside of a word, they should remain outside (e.g. "hey, don‘t do that!" -> "eyhay, on‘tday oday atthay!"). If a "word" (such as "9000") contains no letters, do not add "ay" to the end. Finally, if the first letter of a word is capitalized, then the first letter of the pig latin word should be capitalized (e.g. "Professor Kosbie" -> "Ofessorpray Osbiekay". All letters except the first will be lowercase in the input, and should be lower case in your output as in the previous example.
     
  9. justifyText
    Write the function justifyText that takes a string (which may be multi-line, or just one potentially very long line) and a width (which you may assume is a positive integer), and returns that same text as a multi-line string that is left- and right- justified to the given line width.  For example, consider the following text:
    text = """\
    We hold these truths to be self-evident:  that all men are created equal;
    that they are endowed by their Creator with certain unalienable rights;
    that among these are life, liberty, and the pursuit of happiness."""

    WIth this text, a call to justifyText(text, 30) would return this text left- and right-justified with 30 characters per line, as such:

    """\
    We  hold  these  truths  to be
    self-evident: that all men are
    created  equal;  that they are
    endowed  by their Creator with
    certain   unalienable  rights;
    that  among  these  are  life,
    liberty,  and  the  pursuit of
    happiness."""
    Here are some specifics to keep in mind:
    • You should first replace all sequences of any length of whitespace (spaces, tabs, newlines) with a single space.
    • Then, for each line except the last line, find the space as far to the right as possible while still allowing the line to remain under the given width restriction.  That space will be replaced with a newline ("\n").
    • What if a line has no such space, and instead it is solid non-spaces?  This is unlikely, but we still have to deal with it.  In this case, just insert a newline at the given line width.  Do not add a hyphen in this case.
    • Now, unless the line is a perfect fit in the given width (as in the second line above ), you will have to add some extra spaces to make it fit exactly.  Consider the first line in the example above ("We hold these truths to be").  There are 5 total spaces (one between each word), and since the line is 26 characters long, it requires 4 more spaces to pad out to a width of 30.  This is done by adding an extra space to each space from left-to-right.  In this case the first 4 spaces are now 2 spaces, and the last space is still one space.  This process works in general, so you may have to add more than one extra space of padding to each gap (such as the 3 total spaces between "certain" and "unalienable"), and there cannot be more than a 1-space difference between any two gaps on a line, and the extra spaces must all be on the left side of the line.
    • The last row is not justified.

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem