Spring 2018 EECS 110: Homework IV.

Slides:



Advertisements
Similar presentations
2/7/2008. >>> Overview * boolean * while * random * tuples.
Advertisements

HW 4 EECS 110.
HW 1: Problems 3 & 4 EC 1 & 2.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
30/10/ Iteration Loops Do While (condition is true) … Loop.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally.
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
Spring  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
8.1 – Sequences and Series. Sequences Infinite sequence = a function whose domain is the set of positive integers a 1, a 2, …, a n are the terms of the.
2.4 Measures of Variation The Range of a data set is simply: Range = (Max. entry) – (Min. entry)
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Input, Output and Variables GCSE Computer Science – Python.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Data Mining – Algorithms: K Means Clustering
EECS 110: Lec 10: Definite Loops and User Input
Python unit_4 review Tue/Wed, Dec 1-2
REPETITION CONTROL STRUCTURE
Algorithmic complexity: Speed of algorithms
Line Continuation, Output Formatting, and Decision Structures
CS1371 Introduction to Computing for Engineers
Functions.
Ch. 8 – Sequences, Series, and Probability
Lecture 07 More Repetition Richard Gesick.
Engineering Innovation Center
LOOPS.
Lecture 4B More Repetition Richard Gesick
While Loops.
Building Java Programs
Line Continuation, Output Formatting, and Decision Structures
EECS 110: Lec 10: Definite Loops and User Input
Random numbers What does it mean for a number to be random?
Python I/O.
Random numbers What does it mean for a number to be random?
© Akhilesh Bajaj, All rights reserved.
Chapter 10 Lists.
TIPS: How to Be Successful
Bryan Burlingame 28 November 2018
Math –Series.
We’re moving on to more recap from other programming languages
Do While (condition is true) … Loop
Coding Concepts (Basics)
Spring 2010 EECS 110: Homework I.
More Looping Structures
Variables and Expressions
Chapter 5 Loops.
Algorithmic complexity: Speed of algorithms
Writing Functions( ) (Part 4)
Python programming exercise
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
EECE.2160 ECE Application Programming
Flowcharts and Pseudo Code
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Algorithmic complexity: Speed of algorithms
EECE.2160 ECE Application Programming
Programming Dr. Jalal Kawash.
Question 1a) What is printed by the following Java program? int s;
Lecture 13 Teamwork Bryan Burlingame 1 May 2019.
More Looping Structures
Random numbers What does it mean for a number to be random?
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Python Creating a calculator.
Presentation transcript:

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