1 / 20

EECS 110: Homework IV

Spring 2018. EECS 110: Homework IV. Homework IV. Problems: Mandelbrot set Time Travel Securities, Inc. Pi from pie Extra Credits: Sequence Sums. Problem 2: TT Securities The Menu.

sbowden
Download Presentation

EECS 110: Homework IV

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spring 2018 EECS 110: Homework IV

  2. Homework IV • Problems: • Mandelbrot set • Time Travel Securities, Inc. • Pi from pie • Extra Credits: • Sequence Sums

  3. Problem 2: TT SecuritiesThe 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) QuitEnter your choice:

  4. Problem 2: TT SecuritiesThe 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!

  5. Problem 2: TT SecuritiesThe 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

  6. Problem 2: TT SecuritiesThe Options • 3: Find Standard Deviation • List L has average Lav • Standard deviation issqrt((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

  7. Problem 2: TT SecuritiesExample 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 ...

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

  9. 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 π

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

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

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

  13. Problem 3: Calculating π Hints • Choosing random numberrandom.uniform(-1.0,1.0) • Distance between (x,y) and (0,0):sqrt(x**2+y**2)

  14. Extra Credit: Sequence SumsOverview • Look and Say • Harmonic series • Harmonic series without d (Kempner series)

  15. Extra Credit: Sequence SumsLook 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 '

  16. Extra Credit: Sequence SumsLook and Say • Weep(n) generates the first n terms: Example: Weep(8) prints1121121111122131221113112221111321321131131211131221 '

  17. Extra Credit: Sequence SumsHarmonic Series • Harmonic series is1 + 1/2 + 1/3 + 1/4 + 1/5 + … • Divergence (very slow) • harmonicN(numToReach) returns the smallest number of terms required to exceed numToReach '

  18. Extra Credit: Sequence SumsHarmonic 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 '

  19. Extra Credit: Sequence SumsHints • Python may lose precision • Use package decimal: • Preamble:from decimal import *getcontext().prec = 20 • Turn number to Decimal formatDecimal(n) • Operations stay the same • Turn Decimal format to string: str(x) '

  20. Have fun + Good luck :D

More Related