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

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

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

Name & Recitation Section:

Name & Recitation Section:

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!

MIT AITI Python Software Development

Turn in a printout of your code exercises stapled to your answers to the written exercises by 2:10 PM on Tuesday, January 18th.

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

MITOCW watch?v=flgjisf3l78

If Statements, For Loops, Functions

PROBLEM SOLVING 11. July 24, 2012

CSC148H Week 3. Sadia Sharmin. May 24, /20

The Dynamic Typing Interlude

Intro to Python & Programming. C-START Python PD Workshop

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

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

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Expressions and Variables

Lecture 6 CS2110 Spring 2013 RECURSION

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

ECE 364 Software Engineering Tools Laboratory. Lecture 4 Python: Collections I

Collections. Lists, Tuples, Sets, Dictionaries

Loop structures and booleans

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

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

CMSC 201 Computer Science I for Majors

CSC148 Week 2. Larry Zhang

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Text Input and Conditionals

TUPLES AND RECURSIVE LISTS 5

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

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

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

Accelerating Information Technology Innovation

Exercise 6.2 A generic container class

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

Programming Fundamentals and Python

PREPARING FOR PRELIM 1

Dmitriy Shironosov/ShutterStock, Inc. Abstract Data Types Abstract Data Types

3137 Data Structures and Algorithms in C++

Problem Solving for Intro to Computer Science

Recursion. Chapter Simple Recursion. Goals Trace recursive algorithms Implement recursive algorithms

CMSC th Lecture: Graph Theory: Trees.

Program Planning, Data Comparisons, Strings

CS 2316 Learning Objectives

Beyond Blocks: Python Session #1

Lecture 5: Implementing Lists, Version 1

Overview of List Syntax

Problem One: A Quick Algebra Review

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Discussion 4. Data Abstraction and Sequences

9/16/14. Overview references to sections in text RECURSION. What does generic mean? A little about generics used in A3

Homework: More Abstraction, Trees, and Lists

Artificial Intelligence A Primer for the Labs

Python: common syntax

CS Summer 2013

Practice Midterm Exam #1

Lecture 3. Input, Output and Data Types

Lecture Agenda. Objects. But First... Immutable Types and Nesting. Immutable Types and Nesting

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS 4349 Lecture October 18th, 2017

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

6.001 Notes: Section 15.1

Introduction to Python

Lecture 7 CS2110 Fall 2014 RECURSION

10. A Weekend to Remember

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

CSE 143. Computer Programming II

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008

Munster Programming Training - Cycle 2

For strings (and tuples, when we get to them), its easiest to think of them like primitives directly stored in the variable table.

Teaching London Computing

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Be careful when deciding whether to represent data as integers or floats, and be sure that you consider all possible behaviors in computation.

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Open and respond to this poll: PollEv.com/comp110. Calling Methods Mr. Roboto

6.001 Notes: Section 17.5

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

Building a snowman with Racket

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

csci 210: Data Structures Stacks and Queues

Programming Languages and Techniques (CIS120)

Types, Expressions, and States

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

Lecture 19: Recursion

Posting Bridge Game Results to VCBridge.org by Bob Gruber

CS Introduction to Programming Fall 2016

Major Assignment: Pacman Game

LECTURE 3 Python Basics Part 2

CMSC201 Computer Science I for Majors

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

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Programming in Python

Transcription:

6.189 Homework 3 Readings How To Think Like A Computer Scientist: Monday - chapters 9, 10 (all); Tuesday - Chapters 12, 13, 14 (all). Tuesday s reading will really help you. If you re short on time, skim 12 & 13, and read 14 well - get at least through Section 14.6. What to turn in Turn in a printout of your code exercises stapled to your answers to the written exercises at 2:10 PM on Thursday, January 13th. Exercise 3.1 Additional List Practice We suggest you do written exercises 3.7-3.9 before starting with the coding questions. Further, you ll know enough to work on OPT.1 by the end of Monday s lecture. Note: So far we have been using raw input to get user input. For the remainder of this course we will move away from this tool, instead writing functions that take in parameters as opposed to prompting the user for input. So, for this and all following problems, do not use raw input unless explicitly told to do so. Download and save the template homework 3.py from the course website. Write a function list intersection that takes two lists as parameters. Return a list that gives the intersection of the two lists- ie, a list of elements that are common to both lists. Run the following test cases and make sure your results are the same (nb: the ordering of your outputs does not matter - [3,2] is the same as [2,3]): >>> list_intersection([1, 3, 5], [5, 3, 1]) [1, 3, 5] >>> list_intersection([1, 3, 6, 9], [10, 14, 3, 72, 9]) [3, 9] >>> list_intersection([2, 3], [3, 3, 3, 2, 10]) [3, 2] >>> list_intersection([2, 4, 6], [1, 3, 5]) [] 1

3.2 Collision Detection of Balls Many games have complex physics engines, and one major function of these engines is to figure out if two objects are colliding. Weirdly-shaped objects are often approximated as balls. In this problem, we will figure out if two balls are colliding. You ll need to remember how to unpack tuples; refer to Chapter 9.2 or ask an LA if this is confusing. We will think in 2D to simplify things, though 3D isn t different conceptually. For calculating collision, we only care about a ball s position in space, as well as its size. We can store a ball s position with the (x, y) coordinates of its center point, and we can calculate its size if we know its radius. Thus, we represent a ball in 2D space as a tuple of (x, y, r). To figure out if two balls are colliding, we need to compute the distance between their centers, then see if this distance is less than or equal to the sum of their radii. If so, they are colliding. In homework 3.py, write a function ball collide that takes two balls as parameters and computes if they are colliding; your function should return a Boolean saying whether or not the balls are colliding. Optional: For a little extra challenge, write your function to work with balls in 3D space. How should you represent the balls? You will also need to write your own test cases - be sure to figure out any edge cases you need to test. Exercise 3.3 An Introduction to Dictionaries Quick Reference D = {} creates an empty dictionary D = {key1:value1,...} creates a non-empty dictionary D[key] returns the value thats mapped to by key. (What if there s no such key?) D[key] = newvalue maps newvalue to key. Overwrites any previous value. Remember - newvalue can be any valid Python data structure. del D[key] deletes the mapping with that key from D. len(d) returns the number of entries (mappings) in D. x in D, x not in D checks whether the key x is in the dictionary D. D.keys() - returns a list of all the keys in the dictionary. D.values() - returns a list of all the values in the dictionary. For this exercise, write a dictionary that catalogs the classes you took last term - the keys should be the class number, and the values should be the title of the class. Then, write a function add class that takes 2 arguments - a class number and a description - that adds new classes to your dictionary. Use this function to add the classes you re taking next term to the dictionary. Finally, write a function print classes that takes one argument - a Course number (eg 6 ) - and prints out all the classes you took in that Course. Example output: >> print_classes( 6 ) 6.189 - Introduction to Python 6.01 - Introduction to EECS >> print_classes( 9 ) No Course 9 classes taken For this exercise, we suggest using strings everywhere. Be sure to test with Course numbers that you both did and did not take! (if you re a Wellesley or grad student and are confused about course numbers, etc, ask an LA for explanations). 2

Exercise 3.4 More About Dictionaries In homework 3.py, under the Exercise 3.4 heading, notice that we ve defined two lists: NAMES = [ Alice, Bob, Cathy, Dan, Ed, Frank, Gary, Helen, Irene, Jack, Kelly, Larry ] AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19] These lists match up, so Alice s age is 20, Bob s age is 21, and so on. Write a function combine lists that combines these lists into a dictionary (hint: what should the keys, and what should the values, of this dictionary be? ). Then, write a function people that takes in an age and returns the names of all the people who are that age. Test your program s functions by running these lines (they are commented at the bottom of the code file; uncomment them to use them): print Dan in people(18) and Cathy in people(18) print Ed in people(19) and Helen in people(19) and\ Irene in people(19) and Jack in people(19) and Larry in people(19) print Alice in people(20) and Frank in people(20) and Gary in people(20) print people(21) == [ Bob ] print people(22) == [ Kelly ] print people(23) == [] All lines should print True. The last line is an edge condition that we re testing; your people function should be able to handle this condition (hint: what is this condition?) by simply returning an empty list... Exercise 3.5 Zeller s Algorithm, revisited This is similar to the optional problem from Homework 1, but in this version we will be writing a function that takes parameters, and using dictionaries to facilitate pretty printing (where the answer is given to the user in a nice looking fashion). For the rules of the algorithm, please take a look at the description written in Homework 1 (available as a pdf online, under the Materials section of the course website). Calling zellers( March, 10, 1940) should give the output: Sunday Hints: 1. Use a dictionary to map between the month and its numerical value. 2. You can use either a list or dictionary to convert the final output of the algorithm to the day of the week. 3. Make sure you handle the following three points correctly. Note: If the month is January or February, then the preceding year is used for computation. This is because there was a period in history when March 1st, not January 1st, was the beginning of the year. If the computed value of R is a negative number, add 7 to get a nonnegative number between 0 and 6. You might need to use one of the following (but, maybe not): To convert the string 90 to the number 90, use int( 90 ); to convert the int 90 to the string 90, use str(90). 3

Exercise 3.6 Your First Class This exercise, plus the remaining written questions (3.10-3.11) will be the start of our journey into object-oriented programming; we suggest you do these written exercises before tackling this question. For this exercise, you will be coding your very first class, a Queue class. Queues are a fundamental computer science data structure. A queue is basically like a line at Disneyland - you can add elements to a queue, and they maintain a specific order. When you want to get something off the end of a queue, you get the item that has been in there the longest (this is known as first-in-first-out, or FIFO). You can read up on queues at Wikipedia if you d like to learn more. Create a new file called queue.py to make your Queue class. In your Queue class, you will need three methods: init : to initialize your Queue (think: how will you store the queue s elements? You ll need to initialize an appropriate object attribute in this method) insert: inserts one element in your Queue remove: removes one element from your Queue and returns it. If the queue is empty, return a message that says it is empty. When you re done, you should test your implementation. Your results should look like this: >> queue = Queue() >> queue.insert(5) >> queue.insert(6) 5 >> queue.insert(7) 6 7 The queue is empty Be sure to handle that last case correctly - when popping from an empty Queue, print a message rather than throwing an error. 4

Exercise OPT.1 Palindromes! Write a function is palindrome which takes a string as parameter, and returns True if the string is a palindrome (meaning it is the same forwards as backwards), and False otherwise. Save your work in palindromes.py. This problem is kind of tricky so feel free to ask for help; turn in any progress you make on it as well as comments explaining what does and doesn t work. For an additional challenge, try writing this as a recursive function. Some useful things to remember: Use the function len to find the length of a string. To get just a piece of a string, use the slice operator. For example: astring = hello substr = astring[1:-1] #sets substr to ell You can decide for yourself whether you want your function to correctly identify palindromes that have spaces (such as able was i ere i saw elba ) - remember the string.join method we saw in the hangman project? string.lower may also be a useful function. BE SURE TO TEST WELL! Include multiple test cases, including one where the word isn t a palindrome, but the first and last letters are equal (such as yummy ). It is easiest to do this with a while loop, although there are a few different ways of structuring such a loop. Think about what conditions need to be met for a palindrome to be true, and when you can stop testing for one (hint: the words ana and anna are both palindromes; when do we know to stop checking?) Exercise OPT.2 Stacks This problem builds on your work in Exercise 3.6. Stacks are the opposite of queues - instead of being first-infirst-out, they use a last-in-first-out (LIFO) strategy. Think of them like a pop-up stack of plates at a restaurant; the first plate put in will be the last one pulled out and used. Again, check out Wikipedia for a more in-depth explanation. Make a new file stacks.py and implement a Stack class. It should be very similar to your Queue implementation; the three methods your class will need will be init, push, and pop. When you re done, you should test your implementation. Your results should look like this: >> stack = Stack() >> stack.push(5) >> stack.push(6) 6 >> stack.push(7) 7 5 The stack is empty 5

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