Homework 5: Loops and Iteration using while and for COMP 112 winter 2019 Wesleyan University Due Jan 12 The following problems are specified in the file homework_5_skel.py. Complete the prob- lems in...

1 answer below »
i am in a intro to programming class and need help with an assignment


Homework 5: Loops and Iteration using while and for COMP 112 winter 2019 Wesleyan University Due Jan 12 The following problems are specified in the file homework_5_skel.py. Complete the prob- lems in that file and rename it homework_5.py before uploading it to Moodle. Remember len(s) returns the length of a string s, and that s[i] returns the ith character of s. Some of these problems must use while instead of for. Make sure their termination is guaranteed. A counter Define a function counter0 which takes no inputs and has the following effect when run: It repeatedly queries the user, asking for either a numerical input, or the letter ’q’ (to quit). The user types in a sequence of numbers and finally the letter ’q’. The program then returns • how many numbers entered • the sum of the numbers entered • the maximal number entered • the average of the numbers entered Here is a sample run of counter0() type a whole number or ’q’ to quit: 10 type a whole number or ’q’ to quit: 20 type a whole number or ’q’ to quit: 30 type a whole number or ’q’ to quit: 40 type a whole number or ’q’ to quit: q you typed 4 numbers sum = 100.0 average = 25.0 max = 40.0 """ 1 2 The power series for sin(x) The value of sin(x) can be approximated by evaluating the sum of the first n terms in the following series: sin(x) = x− x 3 3! + x5 5! − x 7 7! + · · · . Increasing n increases the accuracy of the approximation. The n-th term in this series (where x is the 0-th term) is given by (−1)nx2n+1/(2n + 1)!. m! = m · (m− 1) · (m− 2) · . . . · 3 · 2 · 1 and can be evaluated using math.factorial. (remember to write import math at the top of your file. Suppose you wanted to approximate the sine of a given angle accurately to 2 decimals, i.e. with an error no greater than 0.001. How many terms would you have to add up? The answer is that you should add terms until you get to one whose absolute value is smaller than 0.001. In this problem you are asked to define a function power_sin(x, epsilon): that takes two arguments: the angle x (in radians) and a bound epsilon on the error you are willing to tolerate. See the specification of the function power_sin in hw3_skel.py for more details. The modified Collatz sequence The modified Collatz sequence for n is the sequence of words defined as follows: 1. The first word is the string consisting of n "a" characters. 2. Set the “current word” to be the first word and repeatedly transform the current word as follows until the current word has only 0 or 1 characters: (a) Let x be the first letter of the current word. (b) Delete the first two letters from the current word. (c) If x is "a", append "bc" to the current word. (d) If x is "b", append "a" to the current word. (e) If x is "c", append "aaa" to the current word. So one transformation consists of deleteing the first two letters and appending "bc", "a", or "aaa". You can see an example of this process by looking at the Wikipedia article for “Tag systems,” where there is an example titled “Computation of Collatz sequences.” It is a conjecture that this process always terminates—i.e., you will always eventually end up with a word with 0 or 1 characters. However, so far this conjecture has not been proved true. 3 Recognizing a prefix A prefix of a string is an initial segment of it, also a string. For example, the prefixes of “hello” are "", "h","he","hel","hell","hello". In this problem you are asked to define a function is_prefix which takes two arguments, call them s1 and s2, and returns True if and only if s1 is a prefix of s2. You must write code that uses a loop for this assignment1. A sample run >>>is_prefix("miss","missouri") True >>>is_prefix("moss","missouri") False 1Without using a loop the problem can be solved by evaluating s1 == s2[:len(s1)]. This solution is not allowed here. """ HW5 : Iteration with WHILE Fill in the code for the following 4 functions * counter0 * power_sin * modified_collatz * is_prefix specified below. See the PDF for an explanation. Due October 9 """ """ 1 """ def counter0(): """ result: ask user repeatedly (in a loop) to type in a whole number OR 'q' to quit and then, when the user types 'q' print: --how many numbers entered, --sum of numbers entered, --max of numbers entered, --average of numbers entered Sample run: type a whole number or 'q' to quit: 10 type a whole number or 'q' to quit: 20 type a whole number or 'q' to quit: 30 type a whole number or 'q' to quit: 40 type a whole number or 'q' to quit: q you typed 4 numbers sum = 100.0 average = 25.0 max = 40.0 """ """ 2 """ def power_sin(x, epsilon): """ x : float epsilon : float Return: float Compute sin(x) by summing terms in its power series until a term of magnitude (absolute value) < epsilon="" is="" added.="" the="" series="" is="" sin(x)="x" -="" (x**3/3!)="" +="" (x**5/5!)="" -="" +="" ..="" notice="" the="" series="" is="" alternating="" (it="" changes="" sign="" from="" term="" to="" term)="" the="" factorial="" x!="" of="" an="" int="" x="">0 is x*(x-1)*(x-2)*...*2*1 it can be computed by callin g math.factorial(x) Notice that the magnitude of the nth term is (x**n/n!) >>> power_sin(.1, 1e-4) 0.0998334166666... >>> import math >>> power_sin(math.radians(90), 1e-8) 0.999999999993976... """ sum = 0 n = 1 abs_term = 1 sign = 1 while abs_term >= epsilon: abs_term = (x**n)/(math.factorial(n)) pass #your code here """ 3 """ def collatz(n): """ n : int>0 Return: int Returns the number of transforms made when computing the modified Collatz sequence for n. The rules: Start-->n*"a" a?z -->zbc, b?z-->za, c?z-->zaaa where ? stands for ANY character, and z is a string. You keep on transforming the string called Start above until its length is 1. (See the homework sheet for an explanation) if you want to obtain a string 's' with the first two characters missing write s[2:]. If you want to know what the first character of s is , evaluate s[0]. sample run: collatz(2) = 2 collatz(8) = 14 """ count = 0 s = n*"a" pass #use a while loop here. """ 4 """ def max_collatz_height(n): """ n:int>0 returns:int = the largest number produced in the collatz sequence for n e.g. The collatz sequence for 5 is 5 -> 16 -> 8 -> 4 -> 2 ->1 The largest number is 16. """ pass """ 5 """ def is_prefix(s1,s2): """ s1:str s2:str return:bool - True if and only if s1 is a prefix of s2 """ #Do this with a loop. #return s1 == s2[:len(s1)] will work, but is not allowed.
Answered 442 days AfterJan 11, 2021

Answer To: Homework 5: Loops and Iteration using while and for COMP 112 winter 2019 Wesleyan University Due Jan...

Arun Shankar answered on Mar 30 2022
103 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here