HW 4 EECS 110.

Slides:



Advertisements
Similar presentations
The Random Class.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Introduction to Excel Formulas, Functions and References.
SEQUENCES and INFINITE SERIES
Math Calculus I Part VII: More tests for convergence; Power series.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Math Calculus I August 9 (but first, a quick review…)
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Notes, part 4 Arclength, sequences, and improper integrals.
LIMITS The Limit of a Function LIMITS Objectives: In this section, we will learn: Limit in general Two-sided limits and one-sided limits How to.
Rational and Irrational
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 6 By Herb I. Gross and Richard A. Medeiros next.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Introduction We have seen series that are finite, meaning they have a limited number of terms, but what happens to a series that has infinite terms? A.
8.1 Sequences and Series Essential Questions: How do we use sequence notation to write the terms of a sequence? How do we use factorial notation? How.
CHAPTER 2 LIMITS AND DERIVATIVES. 2.2 The Limit of a Function LIMITS AND DERIVATIVES In this section, we will learn: About limits in general and about.
Interest ratesslide 1 INTEREST RATE DETERMINATION The rate of interest is the price of money to borrow and lend. Rates of interest are expressed as decimals.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 2 - Algorithms and Design
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Section 8.4: Other Convergence Tests Practice HW from Stewart Textbook (not to hand in) p. 592 # 3-7, 12-17, odd, 33.
9.4 Part 1 Convergence of a Series. The first requirement of convergence is that the terms must approach zero. n th term test for divergence diverges.
9.5 Part 1 Ratio and Root Tests
In section 11.9, we were able to find power series representations for a certain restricted class of functions. Here, we investigate more general problems.
Number System. Do not train children to learning by force and harshness, but direct them to it by what amuses their minds, so that you may be better able.
Copyright © Cengage Learning. All rights reserved.
This is an example of an infinite series. 1 1 Start with a square one unit by one unit: This series converges (approaches a limiting value.) Many series.
Newton-Raphson Method. Figure 1 Geometrical illustration of the Newton-Raphson method. 2.
Spring  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
9.5 Testing for Convergence Remember: The series converges if. The series diverges if. The test is inconclusive if. The Ratio Test: If is a series with.
1 Standard Deviation of the Difference When multiple samples are analyzed by a proposed and standard methods, S d is the calculated standard deviation.
Types of curves simple curves: A curve is simple if it does not cross itself.
INFINITE SEQUENCES AND SERIES The convergence tests that we have looked at so far apply only to series with positive terms.
8.1 Sequences and Series Essential Questions: How do we use sequence notation to write the terms of a sequence? How do we use factorial notation? How.
Estimating the Area of a Circle Math 8 Stewart COPY SLIDES THAT HAVE A PENCIL.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
13.5 – Sums of Infinite Series Objectives: You should be able to…
9-5 Alternating Series Rizzi – Calc BC. Objectives Use the Alternating Series Test to determine whether an infinite series converges. Use the Alternating.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
1 2 Linear Programming Chapter 3 3 Chapter Objectives –Requirements for a linear programming model. –Graphical representation of linear models. –Linear.
Part VII: More tests for convergence; Power series
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Functions.
Computational Thinking
Computational Thinking
Arrays, Part 1 of 2 Topics Definition of a Data Structure
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Numeric Accuracy and Precision
Random numbers What does it mean for a number to be random?
Infinite Geometric Series
A First Book of ANSI C Fourth Edition
Coding Concepts (Basics)
Writing Functions( ) (Part 4)
Confidence intervals for the difference between two means: Independent samples Section 10.1.
Other types of variables
Spring 2018 EECS 110: Homework IV.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Random numbers What does it mean for a number to be random?
Presentation transcript:

HW 4 EECS 110

Problem #2 For this problem, you will implement a (text-based) menu of options for analyzing a list of stock prices (or any list of floating-point values). The top-level function to write is called tts() -- it takes no inputs. Instead, it offers the user a menu with these choices: (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

Problem #2 (0) Input a new list Hint: use the input() (instead of raw_input) so the user can input a list (1) Print the current list Hint: print "%3d %5.2f" % (i, L[i])

Problem #2 (2) Find the average price … (3) Find the standard deviation

Problem #2 (4) Find the min and its day … (5) Find the max and its day … (6) Your TT investment plan The time-travel strategy: For menu option 6, you will want to find the best day on which to buy and sell the stock in question in order to maximize the profit earned. However, the sell day must be greater than or equal to the buy day. You may want to adapt the example function diff from class in order to find this maximum profit.

Problem #2 Other menu options… print a warning message if the integer is not a valid menu option quit if the user inputs #ask for input #get input While… #menu options

Problem #3 Imagine a circle inscribed within a square that spans the area where -1 ≤ x ≤ 1 and -1 ≤ y ≤ 1. The area of the inscribed circle, whose radius is 1.0 would be pi If you were to throw darts at random locations in the square, only some of them would hit the circle inscribed within it. The ratio area of the circle / area of the square can be estimated by the ratio # of darts that hit the circle / total # of darts thrown As the number of darts increases, the second ratio, above, gets closer and closer to the first ratio. Since three of the four quantities involved are known, they can be used to approximate the area of the circle - this in turn can be used to approximate pi

Problem #3 The forPi( n ) function takes in a positive integer n and should "throw" n darts at the square. Each time a dart is thrown, the function should print the number of darts thrown so far the number of darts thrown so far that have hit the circle the resulting estimate of pi def forPi(n) #initiate variables for i in ….: #use random to find an x and a y between -1 & 1 #make equation to find if the (x,y) coord is in the circle #if its in the circle, you’ve got a hit! #print the num of throws, num. of hits, estimate of pi

Problem #3 def whilePi(error): #initiate variables The whilePi( error ) function, on the other hand, will take in a positive floating-point value, error, and should then continue to throw darts at the dartboard (the square) until the resulting estimate of pi is less than error. As with the forPi function, this function should print the number of darts thrown so far the number of darts thrown so far that have hit the circle the resulting estimate of pi def whilePi(error): #initiate variables while (difference between your pi estimate and pi is greater than error): #find x and y between -1 and 1 #calculate if x & y are in circle # if in circle you’ve got a hit! #print the num of throws, num. of hits, estimate of pi

Problem #3 Hints… The distance equation can be useful… Remember that random.uniform( -1.0, 1.0 ) returns a floating pt. number between -1 & 1

Extra Credit: Problem #4 Part a This problem investigates the "Read-it-and-weep" sequence: The sequence starts like this: 1, 11, 21, 1211, 111221, 312211, 13112221, ... This sequence is called the Look and Say sequence, or sometimes, the "Read it and Weep" sequence. (Read it out loud and you will understand why.)

Problem #4 Part a Write a helper function readOne( s ) that takes in a string of digits s and returns the string that represents the "reading" of s. For example: >>> readOne( '1' ) '11'   >>> readOne( '11' ) '21' >>> readOne( '21' ) '1211' >>> readOne( '1211' ) '111221' A special case to consider for readOne is the case when the input, s, has length 1.

Problem #4 Part a >>> Weep(10) 11 21 1211 111221 312211 Write a function called Weep(n) that generates and prints the first n terms in the sequence. >>> Weep(10) 11 21 1211 111221 312211 13112221 1113213211 31131211131221 13211311123113112211 11131221133112132113212221 '11131221133112132113212221'

Problem 4 Part b The harmonic series is defined as: 1/1 + 1/2 + 1/3 + 1/4 + ... This series can be shown to diverge; that is, the series goes to infinity. However, this series diverges very slowly. Write a function called harmonicN( numToReach ) that returns the least number of terms that must be summed in order to reach or exceed numToReach.

Problem 4 Part b While this series goes to infinity, python will have trouble as the terms get very small. To handle very small numbers, you need to use a special representation that is not built into Python but available in a package called decimal. from decimal import * getcontext().prec = 20 Decimal(1) / Decimal(3) Decimal("0.33333333333333333333")

Problem 4 Part c While the harmonic series diverges, a very slight modification to this series turns it into one that converges! For any digit d between 0 and 9, if you remove those terms that contain the digit d anywhere in the denominator, then this series converges. Example, w/ digit as 2… if we remove the terms 1/2, 1/12, 1/20, 1/21, 1/22, 1/23, ..., then this series will converge! While this may seem counter-intuitive, note that as the numbers in the denominator get large, the denominators without d become quite sparse, and the series eventually converges (although very slowly).

Problem 4 Part c Write a function called withoutd( d, Numterms ) that evaluates this new series with respect to digit d by adding up the first Numterms terms of the series and returning this value. Note that Numterms does not include the terms that were "thrown out" - it is the actual number of terms that were added. Here are a few things that will be useful to you: A number of type Decimal can be cast into a string. For example: >>> x = 1/Decimal(3) >>> x Decimal("0.33333333333333333333") >>> str(x) '0.33333333333333333333'   You can determine how many times a particular symbol occurs in a string using the built-in count method. It works like this: If s is some string then s.count("5") will return the number of times that the symbol 5 occurs in string s. Notice that the argument to count is a string itself! For example, "foo".count("o") will return 2.