# Homework Strings solution

• Write a second function that takes a string and a letter as an argument and returns the total number of words that start with the given letter. You can assume there is no punctuation and all the letters are lower case. (Hint: words are separated by spaces. If there is no space in the beginning of the sentence, you can always add one, right?) String function to use To solve this part, you need a string function called replace. That is the only function you need, but we will see others in class on monday. You are free to use any function you desire. The replace function is called as follows: name = "Rensselaer" name.replace("e", "^_^") ’R^_^nss^_^la^_^r’ name ’Rensselaer’ "abracadabra".replace("ab","z") ’zracadzra’ Basically, mystring.replace(old,new) replaces all instances of substring old with the string new, and returns a new string. You can see that it does not change the actual string it operates on. Cipher rules

The sentence to be deciphered is constructed by using a replacement cipher with the following rules: ’he’ = ’bb’ replace all occurrences of string he with bb ’e’ = ’az az’ valid only for any e that is not part of he, replace e with az az ’an’ = ’he’ replace all occurrences of string an with he ’th’ = ’xx’ replace all occurrences of string th with xx ’u’ = ’yyy’ replace all occurrences of string u with yyy ’o’ = ’twt’ replace all occurrences of string o with twt ’ a’ = ’rxr’ for any a that is not part of an, note the space before a. For example cipher for methane is maz azxxheaz az. Here is how we get this: ’methane’.replace(’th’,’xx’) ’mexxane’ ’mexxane’.replace(’e’,’az az’) ’maz azxxanaz az’ ’maz azxxanaz az’.replace(’an’,’he’) ’maz azxxheaz az’ The trick here is that we can do the enciphering (and deciphering) by continuously replacing a part of the string with a new one. But, the replacement must be done in a logical order. For example, in the above example, if we replaced an with he ﬁrst, then we cannot use the rule of replacing e with ax ax because the newly added e was not part of the original word and should not be changed.
Part 3: Playing with Strings and Numbers

Write a program that takes as input two numbers, height and width of a rectangle. It then prints a rectangle with height lines and width characters in each line. The rectangle should contain information regarding its dimensions and area inside (centered vertically but not horizontally). If any part of the information (including two stars on each end and a space before and after the line) does not ﬁt in the rectangle, then print the complete information after the rectangle. Here are two example runs of the program: Height== 6 6 Width== 15 15 *************** * * * h: 6, w: 15 * * area: 90 * * * *************** Height== 6 6 Width== 10 10 ********** * * * * * * * * ********** h: 6, w: 10 area: 60 To simplify this problem, you will make some assumptions: • Assume the user enters a correct integer for height and width. • Assume the height is an even number greater than or equal to 4. This part will require you to use a lot of the constructs we have learnt so far. This is a good example of something that looks hard at ﬁrst, but when you break it down, it is not so diﬃcult. You do not need loops, and you should try hard not to use loops. You are not required to use functions. However, you will need an if statement for a special case explained below. Leave that part as the last thing to work on. We will see if statements on monday. Here are a few pointers for you to get started: • Read and print values. That should be easy to do.
• Remember that raw input reads values as a string, but you need to convert them to integer to compute the area. You will need these integers throughout your program.
• You can create two separate strings for the information to be displayed. One will have the height and width information, and the other will have the area. Create and print these two strings. Don’t worry about printing them inside the rectangle yet.
• Make sure you can create a rectangle without anything in it of the correct dimensions. Create this rectangle as a string with new lines in it. Basically, it is a special instance of what you have done in Lab 2. Once you are done, print it out.
• Now, you have to ﬁgure out how to place the two information strings inside your rectangle. First step is to ﬁgure out how to print them with the appropriate number of spaces and a star symbol at the two ends. When you are done, now ﬁgure out how to place it in the middle of it.
• Finally, the ﬁnal step is ﬁguring out when one of the message lines is too long to ﬁt in it, and have a diﬀerent rectangle string for this case. If you have come this far, you will ﬁnd this is not that hard.