Computer science

CSE 101: Introduction to Computers Lab #2 Spring 2017 Assignment Due: February 17, 2017 by 11:59 pm Assignment Objectives By the end of this assignment you should be able to design, code, run and test original Python functions that solve simple problems involving Python strings, forloops, and lists.

Getting Started For the labs and homework assignments in this course you will be writing functions that solve computational problems. To help you get started on each assignment, we will give you on Piazza a “bare bones” le with a name like lab2.py . These les will contain function stubsand a few tests you can try out to see if your code seems to be correct. Stubs are functions that have no bodies (or have very minimal bodies). You will ll in the bodies of these functions for the assignments. Do not, under any circumstance, change the names of the functions or their parameter lists. The automated grading system will be looking for exactly those functions provided in lab2.py.

Please note that the test cases we provide you in the bare bones les will not necessarily be the same ones we use during grading!

Directions Solve the following problems to the best of your ability.

At the top of the lab2.py le, include the following information in comments, with each item on a separate line:

your rst and last name as they appear in Blackboard your Stony Brook ID # the course number (CSE 101) the assignment name and number (Lab #2) Your functions must be named as indicated below. Submissions that have the wrong function names can't be graded by our automated grading system.

Upload your .py le to Blackboard by the indicated due date and time. Late work will not be accepted for grading. Work is late if it is submitted after the due date and time.

Programs that crash will likely receive a grade of zero, so make sure you thoroughly test your work before submitting it. CSE 101 – Spring 2017 Lab #2 Page 1 Part I: Duck, Duck, Goose! (3 points) Assume that you are working at a poultry farm, counting animals that are traveling down a conveyor belt (don't ask). Each duck is worth 1 point. Geese are larger, so they are worth 2 points each. Eggs reset your current count to 0. For example, the sequence [“duck”, “duck”, “goose”] would produce the count value 4 (1 for each of the ducks, plus 2 for the goose). [“duck”, “goose”, “egg”, “duck”] returns 1 (the egg resets the earlier count to 0, so only the last “duck” contributes to the nal score).

Complete the ddg()function, which takes a list of strings as its only argument. Each string value will be either “duck”, “goose”, or “egg” (all lowercase). The function returns a non-negative integer according to the rules above.

Examples: Function Call Return Value ddg(["goose", "duck", "duck", "duck"]) 5 ddg(["duck", "egg", "goose", "egg"]) 0 ddg(["duck", "duck", "egg", "goose"]) 2 Part II: Activity Scheduling (3 points) Camp Sunny Daze has hired you as their new Activity Director. You are required to choose activities for the campers based on the weather predictions for that week. Given a list containing some number of integer values (each representing the average temperature for one day), your schedule()function should return a corre- sponding list of strings representing daily activities. Your menu of possible activities, based on temperature, is as follows:

90 degrees or higher: swimming 80–89 degrees: hiking 70–79 degrees: tennis 60–69 degrees: softball 50–59 degrees: football 49 degrees or below: movie For example, if the day's predicted temperature is 72 (degrees), that day's activity will be tennis. Complete the schedule() function, which takes a list of positive integer values as its sole argument, and returns an equivalently-sized list of strings. Each string will match one of the activities listed above (in all lowercase), according to the value of the integer in that position in the parameter list (for example, if the rst value in the input list is 61, the rst value in the output list will be “softball”).

Examples: Function Call Return Value schedule([72, 84, 55]) ["tennis", "hiking", "football"] schedule([95, 63, 70]) ["swimming", "softball", "tennis"] schedule([44, 56, 68, 92]) ["movie", "football", "softball", "swimming"] CSE 101 – Spring 2017 Lab #2 Page 2 Part III: Set Intersection (3 points) Consider two lists of integers, where each list is a set(i.e., contains no duplicate values). The intersectionof these two sets will be a list containing only those integers that appear in both of the source lists/sets. For example, given the lists [1, 2, 3, 4, 5] and [2, 4, 6, 8, 10], their intersection would be the list [2, 4] (note that the source lists and the intersection set do not need to contain values in sorted order).

Complete the intersection() function, which takes two integer lists as its arguments. This function returns a Python list whose only elements are those integers that appear in bothof the list parameters. If the two source lists have no elements in common, then your function should return an empty list.

Examples: Function Call Return Value intersection([1,2,3,4,5], [2,4,6,8,10]) [2, 4] intersection([12,45,91], [36,91]) [91] intersection([2,3,4,5,6], [7,8,9,10,11]) [] How to Submit Your Work for Grading To submit your .py le for grading:

1.Login to http://blackboard.stonybrook.edu and locate the course account for CSE 101.

2.Click on “Assignments” in the left-hand menu and nd the link for this lab assignment.

3.Click on the link for this lab assignment.

4.Click the “Browse My Computer” button and locate the .py le you wish to submit. You should be submitting only one le!

5.Click the “Submit” button to submit your work for grading.

Oops, I messed up and I need to resubmit a le!

No worries! Just follow the above directions again. We will grade only your last submission. CSE 101 – Spring 2017 Lab #2 Page 3