How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

Similar documents
6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

Recall that strings and tuples are immutable datatypes, while lists are mutable datatypes. What does this mean?

Functions and Decomposition

Chapter 2 Programming in Python

T H E I N T E R A C T I V E S H E L L

61A Lecture 2. Friday, August 28, 2015

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

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

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

1.7 Limit of a Function

Math 5320, 3/28/18 Worksheet 26: Ruler and compass constructions. 1. Use your ruler and compass to construct a line perpendicular to the line below:

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Math Day 2 Programming: How to make computers do math for you

Lecture 4. Defining Functions

Python Programming: An Introduction to Computer Science

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Lecture 3. Functions & Modules

n! = 1 * 2 * 3 * 4 * * (n-1) * n

15-122: Principles of Imperative Computation, Fall 2015

4. Java Project Design, Input Methods

Lecture 3. Functions & Modules

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

Dr Richard Greenaway

Section 0.3 The Order of Operations

Algorithms and Programming I. Lecture#12 Spring 2015

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

SP 713 Slide Rule Practice for our MIT Museum visit By Alva Couch and Elizabeth Cavicchi

Sets. Sets. Examples. 5 2 {2, 3, 5} 2, 3 2 {2, 3, 5} 1 /2 {2, 3, 5}

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Python Programming Exercises 1

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

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

Table of Laplace Transforms

COMP1730/COMP6730 Programming for Scientists. Functions

Name & Recitation Section:

1 Lecture 7: Functions I

LOOPS. Repetition using the while statement

SCHEME AND CALCULATOR 5b

Python Programming: An Introduction to Computer Science

DECOMPOSITION, ABSTRACTION, FUNCTIONS

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

6.1 Evaluate Roots and Rational Exponents

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

C Functions. 5.2 Program Modules in C

Recursively Enumerable Languages, Turing Machines, and Decidability

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

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

Introduction to Computer Programming for Non-Majors

The Dynamic Typing Interlude

Computing with Numbers Zelle - Chapter 3

Introduction to Computer Programming for Non-Majors

Condition Controlled Loops. Introduction to Programming - Python

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Turn in a printout of your code exercises stapled to your answers to the written exercises at 2:10 PM on Thursday, January 13th.

Computer Lab 1: Introduction to Python

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Downloaded from Chapter 2. Functions

Lecture 3: Functions & Modules

2.1 Basics of Functions and Their Graphs

CITS 1401 Problem Solving & Programming

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

4.7 Approximate Integration

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

MIT AITI Python Software Development

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Testing and Debugging

Variable and Data Type 2

PROBLEM SOLVING 11. July 24, 2012

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Part II Composition of Functions

Introduction to Computer Programming for Non-Majors

Here is a sample IDLE window illustrating the use of these two functions:

3x - 5 = 22 4x - 12 = 2x - 9

, etc. Let s work with the last one. We can graph a few points determined by this equation.

Lecture 3: Recursion; Structural Induction

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Decision structures. A more complex decision structure is an if-else-statement: if <condition>: <body1> else: <body2>

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

Math 1: Solutions to Written Homework 1 Due Friday, October 3, 2008

6.189 D ay 6. Name: R eadings. E x er cise 6. 1 { W ar mup: F inding B ugs

The Beauty and Joy of Computing 1 Lab Exercise 9: Problem self-similarity and recursion - Python version

Starting. Read: Chapter 1, Appendix B from textbook.

CS1 Lecture 5 Jan. 25, 2019

Trombone players produce different pitches partly by varying the length of a tube.

6.149 Checkoff 2. What to complete. Recall: Creating a file and running a program in IDLE.

Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

1) Complete problems 1-65 on pages You are encouraged to use the space provided.

CS1 Lecture 5 Jan. 26, 2018

Binary Search. Roland Backhouse February 5th, 2001

Graphics calculator instructions

Python for Informatics

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

Transcription:

6.189 Day 3 Today there are no written exercises. Turn in your code tomorrow, stapled together, with your name and the file name in comments at the top as detailed in the Day 1 exercises. Readings How To Think Like A Computer Scientist, chapter 3; chapter 6, sections 6.5-6.9. 6.01 Fall 2009 Course Notes sections 2.1, 2.2, and 2.3 (up to the heading List Comprehensions ). Note that the 6.01 course notes are very fast paced- if you see concepts you are confused about, or we haven t covered, don t fret- we will cover them, and you are free to come to office hours to ask questions. Exercise 3.0 Print vs Return Note: this exercise is in section 2.2 of the 6.01 Course Notes, if you have already gone through it you may skip it. This isn t really an exercise, just an important bit of reading. Make a new file called Day3.py and define these two different functions: def f1(x): print x + 1 def f2(x): return x + 1 Run your code in the shell. What happens when we call these functions? >>> f1(3) >>> f2(3) It looks like they behave in exactly the same way. But they don t, really. Try this: >>> print(f1(3)) None >>> print(f2(3)) In the case of f1, the function, when evaluated, prints ; then it returns the value None, which is printed by the Python shell. In the case of f2, it doesn t print anything, but it returns, which is printed by the Python shell. Finally, we can see the difference here: 1

>>> f1(3) + 1 Traceback (most recent call last): File "<stdin>", line 1, in? TypeError: unsupported operand type(s) for +: NoneType and int >>> f2(3) + 1 5 In the first case, the function doesn t return a value, so there s nothing to add to 1, and an error is generated. In the second case, the function returns the value, which is added to 1, and the result, 5, is printed by the Python read-eval-print loop. The book How To Think Like A Computer Scientist was translated from a version for Java, and it has a lot of print statements in it, to illustrate programming concepts. But for just about everything we do, it will be returned values that matter, and printing will be used only for debugging, or to give information to the user. Print is very useful for debugging. It s important to know that you can print out as many items as you want in one line: >>> x = 100 >>> print x, x, x squared, x*x, sqrt(x), x**0.5 x 100 x squared 10000 sqrt(x) 10.0 Exercise 3.1 Defining A Function Recall how we define a function using def, and how we pass in parameters. In Day3.py, transform your code from exercise 2. (the rock, paper, scissors game) into a function that takes parameters (instead of asking the user for input). Make sure to return your answer, rather than printing it. Exercise 3.2 Math Module In this exercise, we will play with some of the functions provided in the math module. A module is a Python file with a collection of related functions. To use the module, you need to add the following line at the beginning of your program: import math Now if you want to find out what is sin(90 ), we first need to convert from degrees to radians and then use the sin function in the math module: radians = (90.0 / 360.0) * 2 * math.pi print math.sin(radians) Many computations can be expressed concisely using the multadd operation, which takes three operands and computes a b+c. One of the purposes of this exercise is to practice pattern-matching: the ability to recognize a specific problem as an instance of a general category of problems. For the mathematical functions, you can generally call math.func, where func is whatever function you want to call. For example, if you want the sine of an angle a (where a is in radians), you can call math.sin(a). For logarithms, the function math.log(n) calculates the natural logarithm of n. You can calculate a log of any base using math.log(n, b), where b is the base of the logarithm. The math module even includes constants such as e (math.e) and π (math.pi). Documentation for the math module is available at http://www.python.org/doc/2.5.1/lib/module-math.html. 2

In the last part, you get a chance to write a method that invokes a method you wrote. Whenever you do that, it is a good idea to test the first method carefully before you start working on the second. Otherwise, you might find yourself debugging two methods at the same time, which can be very difficult. 1. Write a function multadd that takes three parameters, a, b and c. Test your function well before moving on. 2. Underneath your function definition, compute the following values using multadd and print out the result: ( π ) cos( π sin + ) 2 276 + 2 log 7 (12) 19 Hint: If you are unfamiliar with the notation, this represents the ceiling of a number. The ceiling of some float x means that we always round up x. For example, 2.1 = 2.9 = 3.0. Look at the math module documentation for a way to do this! If everything is working correctly, your output should look like: sin(pi/) + cos(pi/)/2 is: 1.06066017178 ceiling(276/19) + 2 log_7(12) is: 17.5539788165 3. Write a new function called yikes that has one argument and uses the multadd function to calculate the following: xe x + (1 e x ) There are two different ways to raise e to a power- check out the math module documentation. Be sure to return the result! Try x=5 as a test; your answer should look like: yikes(5) is 1.0303150673. Exercise 3.3 Writing Simple Methods In this problem you ll be asked to write three simple methods (method is an interchangeable term for function ). Be sure to test your functions well! 1. Write a method is divisible that takes two integers, m and n. The method returns True if m is divisible by n, and returns False otherwise. 2. Write a method rand divis 3 that takes no parameters and returns True if a randomly generated number is divisible by 3, and False otherwise. For this method we ll use a new module, the random module. At the top of your code, underneath import math, add the line import random. We ll use this module to generate a random integer using the function randint, which works as follows: random.randint(lo, hi) where lo and hi are integers that tell the code the range in which to generate a random integer. 0 to 100 is probably a decent range. 3. Imagine that Python doesn t have the!= operator built in. Write a method not equal that takes two parameters and gives the same result as the!= operator. Obviously, you cannot use!= within your function! Test if your code works by thinking of examples and making sure the output is the same for your new method as!= gives you. 3

Exercise 3. Quadratic Formula Write a function roots that computes the roots of a quadratic equation. Check for complex roots and either print an error message saying that the roots are complex, or just compute and print out the complex roots if you wish. Hint 1: Your function should take three parameters- what are they? Hint 2: We know the roots are complex when what condition about the discriminant is met? Exercise 3.5 Café menu Write a function that first displays a simple café menu (see example below), asks the user to enter the number of a choice, and either prints the appropriate action OR prints an error message that their choice was not valid. Example output: >>> menu() 1. Soup and salad 2. Pasta with meat sauce 3. Chef s special Which number would you like to order? 2 One Pasta with meat sauce coming right up! Another example output: >>> menu() 1. Soup and salad 2. Pasta with meat sauce 3. Chef s special Which number would you like to order? 5 Sorry, that is not a valid choice. Exercise 3.6 The game of Nims/Stones In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins. Make a new file called nims.py. Write a function to play this game (should it take any parameters?). This may seem tricky, so break it down into parts. Like many programs, we have to use nested loops (one loop inside another). In the outermost loop, we want to keep playing until we are out of stones. Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way is slightly trickier since we haven t learned lists yet, but it s definitely do-able! Feel free to ask the LAs for help if you want to look ahead and challenge yourself with this approach. Finally, we might want to have an innermost loop that checks if the user s input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question.

If you choose to write two blocks of code, the basic outline of the program should be something like this: def play_nims(pile, max): An interactive two-person game; also known as Stones. @param pile: the number of stones in the pile to start @param max: the maximum number of stones you can take on one turn while [pile is not empty]: while [player 1 s answer is not valid]: [ask player 1] [execute player 1 s move] [same as above for player 2] Note the lines of code underneath the definition of play nims. This is called a docstring, and is handy to use to tell users of your program what parameters to pass in, and what your program does. Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a variable that tracks whether their answer is valid, and set it to False initially. When you re finished, test each other s programs by playing them! 5

MIT OpenCourseWare http://ocw.mit.edu 6.189 A Gentle Introduction to Programming Using Python January (IAP) 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.