Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2015.  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums.

Similar presentations


Presentation on theme: "Spring 2015.  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums."— Presentation transcript:

1 Spring 2015

2  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums

3  The correct result:

4  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:

5  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!

6  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

7  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

8 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...

9  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

10  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 π

11 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...

12  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)

13 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...

14  Choosing random number random.uniform(-1.0,1.0)  Distance between (x,y) and (0,0) : sqrt(x**2+y**2)

15  Look and Say  Harmonic series  Harmonic series without d (Kempner series)

16  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 '

17  Weep(n) generates the first n terms: Example: Weep(8) prints 11 21 1211 111221 312211 13112221 1113213211 31131211131221 '

18  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 '

19  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 '

20  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) '

21 Have fun + Good luck


Download ppt "Spring 2015.  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums."

Similar presentations


Ads by Google