Gold Hw8 Problem 3: TT Securities, Incorporated

Similar documents
Using Objects and Images in Python

Homework 2. Due 4:00pm, Wednesday, February 13, Installation and Handin 2

Gold Problem 3: Fun with Functions

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CMSC 201 Fall 2016 Homework 6 Functions

Loop structures and booleans

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

Copied from: on 3/20/2017

CS150 - Assignment 5 Data For Everyone Due: Wednesday Oct. 16, at the beginning of class

Lecture. Loops && Booleans. Richard E Sarkis CSC 161: The Art of Programming

1 Getting used to Python

printf( Please enter another number: ); scanf( %d, &num2);

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

(Ca...

Python Programming: An Introduction To Computer Science

COMP 105 Homework: Type Systems

CS 111X - Fall Test 1

Intro. Scheme Basics. scm> 5 5. scm>

***PRACTICE EXAM 02 for*** CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Actual exam scheduled for: 11/18/2009

Black Problem 2: Huffman Compression [75 points] Next, the Millisoft back story! Starter files

Final Project: LC-3 Simulator

(Python) Chapter 3: Repetition

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Structure and Interpretation of Computer Programs

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Introduction to Computer Programming for Non-Majors

Intro. Comparisons. > x > y if and only if x is bigger than y. < x < y if and only if x is smaller than y.

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

Python Programming: An Introduction to Computer Science

MITOCW watch?v=0jljzrnhwoi

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Reversing. Time to get with the program

CS02b Project 2 String compression with Huffman trees

CALCULUS LABORATORY ACTIVITY: Numerical Integration, Part 1

CPSC W2 Midterm #2 Sample Solutions

CMSC 201 Spring 2017 Lab 05 Lists

Compsci 101 Fall 2015 Exam 1 Rubric. Problem 1 (24 points)

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

CS 3114 Data Structures and Algorithms READ THIS NOW!

15-110: Principles of Computing, Spring 2018

This homework has an opportunity for substantial extra credit, which is described at the end of this document.

PA101 Learning the Ropes

Spring CS Homework 3 p. 1. CS Homework 3

HOW TO INTEGRATE A PAYPAL BUTTON INTO YOUR WEBSITE

Lab 4. Out: Friday, February 25th, 2005

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

Copyright. For more information, please read the Disclosures and Disclaimers section at the end of this ebook. First PDF Edition, February 2013

MITOCW watch?v=zm5mw5nkzjg

Variables, expressions and statements

Computer Science 217

CS 1301 Exam 1 Spring 2014

MITOCW watch?v=rvrkt-jxvko

Programming with Python

Please write your answers clearly and neatly we can t give you credit if we can t decipher what you ve written.

CS 134 Programming Exercise 2:

Introduction to Python (All the Basic Stuff)

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

CS 1301 Exam 1 Spring 2014

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Lecture 3. Input, Output and Data Types

CS 2316 Individual Homework 5 Joint Probability Out of 100 points

Formulas Learn how to use Excel to do the math for you by typing formulas into cells.

CS 051 Homework Laboratory #2

CMSC 201 Computer Science I for Majors

(Refer Slide Time: 01.26)

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

Fundamentals of Programming (Python) Getting Started with Programming

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Programming Assignment Multi-Threading and Debugging 2

Skill 1: Multiplying Polynomials

Introduction... 3 Introduction... 4

CS 1110, LAB 1: PYTHON EXPRESSIONS.

MITOCW watch?v=9h6muyzjms0

Module 3: Strings and Input/Output

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Ruby on Rails Welcome. Using the exercise files

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

PATH FINDING AND GRAPH TRAVERSAL

GENERAL MATH FOR PASSING

Module 05: Types of recursion

More Programming Constructs -- Introduction

CMSC201 Computer Science I for Majors

Question 1: knn classification [100 points]


CMSC 201 Spring 2019 Lab 06 Lists

Variable and Data Type I

CSE 115. Introduction to Computer Science I

CS 11 Spring 2008 Mid-term exam 2 Answer Key

The Stack, Free Store, and Global Namespace

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

Part 6b: The effect of scale on raster calculations mean local relief and slope

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

This is a combination of a programming assignment and ungraded exercises

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

Spring 2017 CS 1110/1111 Exam 1

If Statements, For Loops, Functions

Transcription:

Gold Hw8 Problem 3: TT Securities, Incorporated Copied from: https://www.cs.hmc.edu/twiki/bin/view/cs5/ttsecuritiesgold on 3/29/2017 [25 points; individual or pair] Filename: hw8pr3.py Using your own loops... Note for this problem: Do not use the built-in functions sum, min, or max here instead, as you work through the different menu options, write helper functions that handle the data appropriately. If you really want to use those functions, feel free to write versions of your own (also, we worked through examples in class, in fact - or will do so on Thu 10/27) Starter code In class, we looked at an example user-interaction loop. Here is a variation on that code this may be a good place to start with this problem. We've named the primary function main() in order to set things up similar to how you'll write the larger version described below: example user-interaction looping program (a variant of the one done in class) def menu(): """ a function that simply prints the menu """ print() print("(0) Continue!") print("(1) Enter a new list") print("(2) Predict the next element") print("(9) Break! (quit)") print() def predict(l): """ predict ignores its input and returns what the next element should have been """ return 42 def find_min(l): """ find min uses a loop to return the minimum of L input L: a nonempty list of numbers output: the smallest value in L """

result = L[0] for x in L: if x < result: result = x return result def find_min_loc(l): """ find min loc uses a loop to return the minimum of L and the location (index or day) of that minimum input L: a nonempty list of numbers outputs: the smallest value in L, its location (index) """ minval = L[0] minloc = 0 for i in list(range(len(l))): if L[i] < minval: a smaller one was found! minval = L[i] minloc = i return minval, minloc def main(): """ the main user-interaction loop """ secret_value = 4.2 L = [30,10,20] an initial list while True: the user-interaction loop print("\n\nthe list is", L) menu() uc = input( "Choose an option: " ) "clean and check" the user's input try: uc = int(uc) make into an int! except: print("i didn't understand your input! Continuing...") continue run the appropriate menu option if uc == 9: we want to quit break leaves the while loop altogether elif uc == 0: we want to continue... continue goes back to the top of the while loop elif uc == 1: we want to enter a new list newl = input("enter a new list: ") enter _something_ "clean and check" the user's input try: newl = eval(newl) eval runs Python's interpreter! Note: Danger! if type(newl)!= type([]): print("that didn't seem like a list. Not changing L.") else: L = newl here, things were OK, so let's set our list, L except: print("i didn't understand your input. Not changing L.") elif uc == 2: predict and add the next element n = predict(l) get the next element from the predict function print("the next element is", n) print("adding it to your list...") L = L + [n] and add it to the list elif uc == 3: unannounced menu option! pass this is the "nop" (do-nothing) statement in Python elif uc == 4: unannounced menu option (slightly more interesting...) m = find_min(l) print("the minimum value in L is", m)

elif uc == 5: another unannounced menu option (even more interesting...) minval, minloc = find_min_loc(l) print("the minimum value in L is", minval, "at day ", minloc) else: print(uc, "? That's not on the menu!") print() print("see you yesterday!") The TTS problem... 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). This provides an opportunity to use loops in a variety of ways, as well as experience handling user input. The top-level function to write for this problem is called main(). Note that it takes no inputs. Instead, it should offer the user a menu with these choices: Enter your choice: Feel free to change the wording or text, but please keep the functionality of these choices intact. If you'd like to add additional menu options of your own design, please use different values for them (see extra credit, below). Once the menu is presented, the program should wait for the user's input. (You may assume that the user will type an integer as input.) The function should then print a warning message if the integer is not a valid menu option quit if the user inputs 9 allow the user to input a new list of stock prices, if the user selects choice 0 print a table of days and prices, with labels, if the user selects choice 1 compute the appropriate statistics about the list for choices 2-6

For any option except 9, the function should reprompt the user with the menu after each choice. Many of the pieces are straightforward, but here are a couple of pointers on two of them: formatting neatly... This is optional, but if you'd like to print neatly-formatted columns, the following approach will help. Try pasting the at the Python prompt, just to get the hang of it). The formatting-string indicates how things are formatted, and the inputs to.format pass what values will get formatted that way. Here, the printed values are in blue: In [3]: print("{0: >4} : $ {1: >6}".format("Day","Price")) Day : $ Price In [4]: print("{0: >4} : $ {1: >6}".format(3,42)) 3 : $ 42 In [5]: print("{0: >4} : $ {1: >6}".format(11,27042)) 11 : $ 27042 Briefly, the formatting string, namely "{0: >4} : $ {1: >6}" is saying three things: print the format-input 0 right-justified in a space with a width of 3" then, print a space, a colon, a space, a dollar sign, and a space (all as expected) then, print the format-input 1 right-justified in a space with a width of 4 Notice that the.format( input0, input1 ) after the formatting string provides the inputs needed! Experiment with this to get it to look the way you'd like. And -- printing without worrying about the format is totally fine, too! 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. Calculating the standard deviation: Please use this formula to calculate the standard deviation of the stock. Note that L av is the average (mean) value of the elements of the list L. Also, it's OK to assume that L will always be non-empty. Helper functions You need to write a helper function for each of the menu options. You can always return more than one value from a function. For example def f(x): return x, (x+4) This function can be called as follows: a, b = f(38) where a will hold the value 38 and b will hold the value 42 Cool! We include an example run that illustrates one possible interface for your main() function a little further down on this page... Extra credit: creative menu options (up to +5 pts) If you'd like, you may add other menu options (under different numeric labels) that process the list in some other way of your design -- it can be serious or not. Either way, your options will be graded on what they do and how smoothly they interact with the user.

Remember that the CS 5 graders get lonely -- conversational programs are welcome! Example run of the basic TTS program Here is an example run -- you do not need to follow this format exactly (though you may), but it's meant to show an example of each menu possibility: In [1]: main() Enter your choice: 0 Enter a new list of prices: [20,10,30] Enter your choice: 1 Day Price --- ----- 0 20.00 1 10.00 2 30.00

Enter your choice: 2 The average price is 20.0 Enter your choice: 3 The st. deviation is 8.16496580928 Enter your choice: 4 The min is 10 on day 1 Enter your choice: 5

The max is 30 on day 2 Enter your choice: 6 Your TTS investment strategy is to Buy on day 1 at price 10 Sell on day 2 at price 30 For a total profit of 20 Enter your choice: 7 The choice 7 is not an option. Try again

Enter your choice: 9 See you yesterday!