Spring 2018 EECS 110: Homework IV
Homework IV Problems: Extra Credits: Mandelbrot set Time Travel Securities, Inc. Pi from pie Extra Credits: Sequence Sums
Problem 2: TT Securities The Menu Function tts() prints a menu (0) Input a new list (1) Print the current list (2) Find the average price (3) Find the standard deviation (4) Find the min and its day (5) Find the max and its day (6) Your TT investment plan (9) Quit Enter your choice:
Problem 2: TT Securities The Input and Options Take user’s choice Can be integer or a string Print warning if choice is not valid 9: Quit 0: Take input as a list Ex: [20,10,30] Start with an empty list New list will be appended to the current Change the type of input()if necessary!
Problem 2: TT Securities The Options 1: Print out the current list For each entry, print out the index and the value Ex: Day | Price -----+-------- 0 | 20.00 1 | 10.00 2 | 30.00 2, 4, 5: Straight-forward
Problem 2: TT Securities The Options 3: Find Standard Deviation List L has average Lav Standard deviation is sqrt((sum(L[i]-Lav)**2)/len(L)) 6: Find the best buy day and sell day To maximize profit (note: buy day < sell day) Comments: Add new options for extra credit (up to 10 pts) Should have a helper function for each option
Problem 2: TT Securities Example Flow def tts(): quit = False while ... # quit is not True # print menu ... # get user input # if input is 9, quit = True # else print out the answer
Problem 3: Calculating π Overview Idea: π = Area of the circle inscribed a 2 by 2 square Estimating π using random dart-throwing A square from (-1,-1) to (1,1) -1 1 Area of Circle = Number of darts hit the circle Area of Square Total number of darts
Problem 3: Calculating π forPi(n) Take input n as a positive integer Sequentially throw n darts into the square: Each throw = randomly choose a point A point has coordinate (x,y) between -1 and 1 Determine if (x,y) is in the circle After each throw: Print out number of darts thrown so far Number of darts hit the circle The current estimate of π
Problem 3: Calculating π Example Flow def forPi(n): # initiate variables ... for i in ... # pick (x,y) randomly # if (x,y) is in the circle # update the number of hits # print out things
Problem 3: Calculating π whilePi(error) Take input error as a positive floating point Throw darts until the estimate error < error Estimate error = |estimate π – actual π| After each throw print as in forPi(n)
Problem 3: Calculating π Example Flow def whilePi(error): # initiate variables ... while # estimate error > error # pick (x,y) randomly # if (x,y) is in the circle # update the number of hits # update the estimation # print out things
Problem 3: Calculating π Hints Choosing random number random.uniform(-1.0,1.0) Distance between (x,y) and (0,0): sqrt(x**2+y**2)
Extra Credit: Sequence Sums Overview Look and Say Harmonic series Harmonic series without d (Kempner series)
Extra Credit: Sequence Sums Look and Say Look and Say sequence: 1, 11, 21, 1211, 111221, 312211, 13112221,… readOne(s) returns the reading of string s: Examples: readOne('11') returns '21' readOne('312211') returns '13112221' Should use both recursion and loop Base case: len(s) == 1 '
Extra Credit: Sequence Sums Look and Say Weep(n) generates the first n terms: Example: Weep(8) prints 11 21 1211 111221 312211 13112221 1113213211 31131211131221 '
Extra Credit: Sequence Sums Harmonic Series Harmonic series is 1 + 1/2 + 1/3 + 1/4 + 1/5 + … Divergence (very slow) harmonicN(numToReach) returns the smallest number of terms required to exceed numToReach '
Extra Credit: Sequence Sums Harmonic Series without d Harmonic series without digit d: remove all term that has digit d in the denominator Example, d = 2: remove the following terms: 1/2, 1/12, 1/20, 1/21,…, 1/32, 1/42,… Convergence (very slow) Withoutd(d,Numterms) returns the Harmonic series without d, calculated using exactly Numterms terms '
Extra Credit: Sequence Sums Hints Python may lose precision Use package decimal: Preamble: from decimal import * getcontext().prec = 20 Turn number to Decimal format Decimal(n) Operations stay the same Turn Decimal format to string: str(x) '
Have fun + Good luck :D