SOLVED:Homework Strings

25.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


5/5 - (1 vote)

General Comments: Please read this first Starting with this homework, almost all homeworks will require user input. To make sure we align your program with the correct inputs, we must note a few important things: • Your program must read the same number of inputs as are required according to the given problem. For example, if we tell you to read a name first and an email next, that means your program must have 2 raw input statements. If it does not, you will get an error like: EOFError: EOF when reading a line This means either you are trying to read too many or too few inputs. Read the problem specification carefully. • In all homeworks, we will use a convention from now. If we ask you to read an input, you will immediately print that input. For example, the following is the correct method: name = raw_input(“Please enter a name== “) print name email = raw_input(“Please enter your email== “) print email • Finally, your output will look slightly different in the submission server than what you see in Wing. For the above program, the following is a possible output in Wing: Please enter a name== Rensselaer Rensselaer Please enter your email== [email protected] [email protected] But, if the same inputs are used in the submission server, you will get the following: Please enter a name== Rensselaer Please enter your email== [email protected] And, if you forgot to add the print statements, you would actually see something like this in the submission server which will be considered incorrect output: Please enter a name== Please enter your email== The difference — and this is not really important for actually completing your homework — is that for each part of the homework, we place all the input into a file and do what’s called “running from the command-line.” In particular, in the above example, we are using an input a file (let’s assume it is called input.txt) that contains two strings: Rensselaer and [email protected] in two lines. When a program is run from a command shell, we use a command-line of the form: python < input.txt Unfortunately, the free version of the WingIDE that we have been using in class does not allow us to specify this input. This is easy to do if you are on a Linux machine or a Mac where you need to just start a Terminal window. You can create a command window in Windows or download and run the linux-derived Cygwin tools. However, for now, do not worry about running your program from the command-line. If you want to learn how to do this, come and ask advice from us during office hours. Fow now, simply make sure that you print all input that your program reads. I will not repeat this in every homework and assume that this is the way we read input from now on.

Part 1: Madlibs In this part you will write code to construct a madlib given below: Look, • 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 first, 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 fit 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 first, but when you break it down, it is not so difficult. 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 figure out how to place the two information strings inside your rectangle. First step is to figure out how to print them with the appropriate number of spaces and a star symbol at the two ends. When you are done, now figure out how to place it in the middle of it.
• Finally, the final step is figuring out when one of the message lines is too long to fit in it, and have a different rectangle string for this case. If you have come this far, you will find this is not that hard.