[Solved] CSCI1100 Homework 2 Functions Part 1: Geometry and rain

$25

File Name: CSCI1100_Homework_2_Functions_Part_1:_Geometry_and_rain.zip
File Size: 518.1 KB

SKU: [Solved] CSCI1100 Homework 2 Functions Part 1: Geometry and rain Category: Tag:
5/5 - (1 vote)

This problem is a warm up of the use of functions. You will be asked to write a few simple functions.Did you know that Panama Canal runs on rain water? Every time a ship passes through a lock,the canal is filled from a lake in the middle of the Panama canal, and then this water is releasedto the ocean and lost forever. Basically, this man-made lake is filled with rain water. Yes, it rainsa lot in Panama. We will compute the change in the depth of the lake during the dry season, i.e.the three months in the year when it rains a bit less.We will make some simplifying assumptions: locks are rectangular prisms and the lake is a cylinder.We will ask to the user the dimensions of the rectangular prism (length, width, height) andthe radius of the cylinder. You will then compute and output the following: Total amount of water needed to fill the rectangular prism 70 times a day for 3 months,assuming a month is 30 days (basically, about 35 ships pass through two locks every day.)Remember the volume of a rectangular prism is given by length * width * height. Writea function volume_prism(length, width, height) to compute this and use it in your computation. Then, you will find the height of a cylinder with the given radius needed to hold this muchwater. Remember, the volume of a cylinder given by pi* radius**2 * height.Write a function find_length(radius, volume) that finds and returns the depth of a cylinderneeded to hold the computed amount of water. You must use the math module forthe value of pi.Here is an expected output of your program (how it will look on Wing IDE):Length of rectangular prism (m) == 12.512.5Width of rectangular prism (m) == 15.815.8Height of rectangular prism (m) == 18.118.1Water needed for (12.5m,15.8m,18.1m) locks is 22520925.0m^3.Radius of cylinder (m) == 11501150Lake with radius 1150.0m will loose 5.4m depth in three months.Remember, using formatted strings will be very useful in this homework.When you have tested your code, please submit it as hw2_part1.py.Part 2: Find the hidden message!Write a program that asks the user for a sentence written in a cipher using (raw_input). You needto decrypt and print the resulting sentence. Then, read a second sentence in plain English using(raw_input) and convert it to a cipher by encrypting it. For both sentences, you must also printthe total difference in length between the cipher and clear text versions. The difference shouldalways be printed as a positive number.Here is an example run of the program (what you will see on Wing):Enter cipher text == wheie stzzt saz azritzztyyyswheie stzzt saz azritzztyyysDeciphered as == why so seriousDifference in length == 14Enter regular text == back off man I am a scientistback off man I am a scientistEncrypted as == back tzztff muq Irxrmrxr sciaz azntistDifference in length == 9The encryption rules are based on a pretty simple set of string replacements, they should be appliedin this order exactly: a = rxr replace any a after a space with rxr.he = vw replace all occurrences of string he with vwe = az az replace any remaining e with az azy = eie replace all occurrences of string y with eieu = yyy replace all occurrences of string u with yyyan = uq replace all occurrences of string an with uqth = aige replace all occurrences of string th with aigeo = tzzt replace all occurrences of string o with tzztFor example cipher for methane is maz azaigeuqaz az. Here is how we get this:methane.replace(e,az az)maz azthanaz azmaz azthanaz az.replace(an,uq)maz azthuqaz azmaz azthuqaz az.replace(th,aige)maz azaigeuqaz azOf course, decrypting will involve using the rules in reverse order.Your program must use two functions: Write one function encrypt(word) that takes as an argument a string in plain English, andreturns a ciphered version of it as a string. Write a second function decrypt(word) that does the reverse: takes a string in cipher andreturns the plain English version of it.Both functions will be very similar in structure, but they will use the string replacement rules indifferent order. You can now test whether your functions are correct by first encrypting a string,and then decrypting. The result should be identical to the original string (assuming the replacementrules are not ambiguous).Use these functions to implement the above program. When you have tested your code, pleasesubmit it as hw2_part2.py.Part 3: A simple trick (numerical functions and one if statement)!This is a simple parlor trick attributed to Albert Einstein, but it probably dates to an earlier date!The trick: First, write the number 1089 on a piece of paper, fold it, and put it away. Next, askyour friend to write down any three-digit number, emphasizing that the first and last digits mustdiffer by at least two. Dont watch your friend doing the arithmetic. After your friend has writtendown the three-digit number, ask him to reverse it, then subtract the smaller from the larger.Example: 321 123 = 198.Tell your friend to reverse the new number.Example: 198 becomes 891.Next ask your friend to add the new number and its reverse together.Example: 198 + 891 = 1089.If all goes as planned, your friend will be amazed. The number you wrote down at the start 1089will always be the same as the end result of this mathematical trick.As we have just learnt how to do if statements, we will do a simple version of this program. Yourprogram must contain one function called reverse that works as follows:reverse(123)321reverse(264)462Make sure you write this function first and test it with many integer inputs.Then, write a program that produces the output given below by reading an input value from theuser and then carrying out the computation outlined above by calling the above function in twoplaces where you reverse the number. Show the steps of the computation. Then, add a simple ifstatement at the end of your program: if the final result is 1089, you should print You see, I toldyou, otherwise, you should print Are you sure your input is valid?Your output must match the following (this is how it will look on Wing):Enter a value == 294294Here is the computation:492 294 = 198198 + 891 = 1089You see, I told you.Here is another run:Enter a value == 242242Here is the computation:242 242 = 00 + 0 = 0Are you sure your input is valid?When you have tested your code, please submit it as hw2_part3.py.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCI1100 Homework 2 Functions Part 1: Geometry and rain
$25