csci 127: introduction to computer science...functions functions are a way to break code into...

104
CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 7 22 October 2019 1 / 46

Upload: others

Post on 12-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

CSci 127: Introduction to Computer Science

hunter.cuny.edu/csci

CSci 127 (Hunter) Lecture 7 22 October 2019 1 / 46

Page 2: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Announcements

Drop-in Sessions for the CSCI 127 & 135students:

I Amanda Bell, department advisorI 1001-L Hunter NorthI Tuesday 11am-12pmI Thursday 3pm-4pm

CS Survey:

Brian Campbell, ‘19Grubhub

CSci 127 (Hunter) Lecture 7 22 October 2019 2 / 46

Page 3: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Announcements

Drop-in Sessions for the CSCI 127 & 135students:

I Amanda Bell, department advisorI 1001-L Hunter NorthI Tuesday 11am-12pmI Thursday 3pm-4pm

CS Survey:

Brian Campbell, ‘19Grubhub

CSci 127 (Hunter) Lecture 7 22 October 2019 2 / 46

Page 4: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Today’s Topics

Recap: Slicing & Images

Introduction to Functions

NYC Open Data

CS Survey

CSci 127 (Hunter) Lecture 7 22 October 2019 3 / 46

Page 5: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Today’s Topics

Recap: Slicing & Images

Introduction to Functions

NYC Open Data

CS Survey

CSci 127 (Hunter) Lecture 7 22 October 2019 4 / 46

Page 6: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

CSci 127 (Hunter) Lecture 7 22 October 2019 5 / 46

Page 7: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

CSci 127 (Hunter) Lecture 7 22 October 2019 6 / 46

Page 8: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

CSci 127 (Hunter) Lecture 7 22 October 2019 7 / 46

Page 9: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

How would you select the lower left corner?img2 = img[height//2:, :width//2]

How would you select the upper right corner?img2 = img[:height//2, width//2:]

How would you select the lower right corner?img2 = img[height//2:, width//2:]

CSci 127 (Hunter) Lecture 7 22 October 2019 8 / 46

Page 10: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

How would you select the lower left corner?

img2 = img[height//2:, :width//2]

How would you select the upper right corner?img2 = img[:height//2, width//2:]

How would you select the lower right corner?img2 = img[height//2:, width//2:]

CSci 127 (Hunter) Lecture 7 22 October 2019 8 / 46

Page 11: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

How would you select the lower left corner?img2 = img[height//2:, :width//2]

How would you select the upper right corner?img2 = img[:height//2, width//2:]

How would you select the lower right corner?img2 = img[height//2:, width//2:]

CSci 127 (Hunter) Lecture 7 22 October 2019 8 / 46

Page 12: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

How would you select the lower left corner?img2 = img[height//2:, :width//2]

How would you select the upper right corner?

img2 = img[:height//2, width//2:]

How would you select the lower right corner?img2 = img[height//2:, width//2:]

CSci 127 (Hunter) Lecture 7 22 October 2019 8 / 46

Page 13: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

How would you select the lower left corner?img2 = img[height//2:, :width//2]

How would you select the upper right corner?img2 = img[:height//2, width//2:]

How would you select the lower right corner?img2 = img[height//2:, width//2:]

CSci 127 (Hunter) Lecture 7 22 October 2019 8 / 46

Page 14: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

How would you select the lower left corner?img2 = img[height//2:, :width//2]

How would you select the upper right corner?img2 = img[:height//2, width//2:]

How would you select the lower right corner?

img2 = img[height//2:, width//2:]

CSci 127 (Hunter) Lecture 7 22 October 2019 8 / 46

Page 15: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Challenge: Cropping Images

0 width

height

width/20

height/2

How would you select the lower left corner?img2 = img[height//2:, :width//2]

How would you select the upper right corner?img2 = img[:height//2, width//2:]

How would you select the lower right corner?img2 = img[height//2:, width//2:]

CSci 127 (Hunter) Lecture 7 22 October 2019 8 / 46

Page 16: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Today’s Topics

Recap: Slicing & Images

Introduction to Functions

NYC Open Data

CS Survey

CSci 127 (Hunter) Lecture 7 22 October 2019 9 / 46

Page 17: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 10 / 46

Page 18: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 10 / 46

Page 19: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 10 / 46

Page 20: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:

Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 10 / 46

Page 21: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 10 / 46

Page 22: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,

which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 10 / 46

Page 23: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Functions

Functions are a way to break code into pieces,that can be easily reused.

Many languages require that all code must beorganized with functions.

The opening function is often called main()

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 10 / 46

Page 24: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

“Hello, World!” with Functions

CSci 127 (Hunter) Lecture 7 22 October 2019 11 / 46

Page 25: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Python Tutor

(Demo with pythonTutor)

CSci 127 (Hunter) Lecture 7 22 October 2019 12 / 46

Page 26: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

In Pairs or Triples:

Predict what the code will do:

CSci 127 (Hunter) Lecture 7 22 October 2019 13 / 46

Page 27: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Python Tutor

(Demo with pythonTutor)

CSci 127 (Hunter) Lecture 7 22 October 2019 14 / 46

Page 28: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Input Parameters & Return Values

Functions can have inputparameters.

Surrounded by parentheses,both in the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 7 22 October 2019 15 / 46

Page 29: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Input Parameters & Return Values

Functions can have inputparameters.

Surrounded by parentheses,both in the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 7 22 October 2019 15 / 46

Page 30: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Input Parameters & Return Values

Functions can have inputparameters.

Surrounded by parentheses,both in the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 7 22 October 2019 15 / 46

Page 31: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Input Parameters & Return Values

Functions can have inputparameters.

Surrounded by parentheses,both in the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 7 22 October 2019 15 / 46

Page 32: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Input Parameters & Return Values

Functions can have inputparameters.

Surrounded by parentheses,both in the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 7 22 October 2019 15 / 46

Page 33: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Input Parameters & Return Values

Functions can have inputparameters.

Surrounded by parentheses,both in the function definition,and in the function call(invocation).

The “placeholders” in thefunction definition: formalparameters.

The ones in the function call:actual parameters.

Functions can also returnvalues to where it was called.

CSci 127 (Hunter) Lecture 7 22 October 2019 16 / 46

Page 34: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

In Pairs or Triples:Circle the actual parameters and underline the formal parameters:

CSci 127 (Hunter) Lecture 7 22 October 2019 17 / 46

Page 35: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

In Pairs or Triples:Circle the actual parameters and underline the formal parameters:

CSci 127 (Hunter) Lecture 7 22 October 2019 18 / 46

Page 36: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

In Pairs or Triples:Predict what the code will do:

CSci 127 (Hunter) Lecture 7 22 October 2019 19 / 46

Page 37: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Python Tutor

(Demo with pythonTutor)

CSci 127 (Hunter) Lecture 7 22 October 2019 20 / 46

Page 38: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

In Pairs or Triples:

Predict what the code will do:

CSci 127 (Hunter) Lecture 7 22 October 2019 21 / 46

Page 39: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Python Tutor

(Demo with pythonTutor)

CSci 127 (Hunter) Lecture 7 22 October 2019 22 / 46

Page 40: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

In Pairs or Triples:

Fill in the missing code:

CSci 127 (Hunter) Lecture 7 22 October 2019 23 / 46

Page 41: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

IDLE

(Demo with IDLE)

CSci 127 (Hunter) Lecture 7 22 October 2019 24 / 46

Page 42: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap: Functions

Functions are a way to break code into pieces,that can be easily reused.

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 25 / 46

Page 43: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap: Functions

Functions are a way to break code into pieces,that can be easily reused.

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:

Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 25 / 46

Page 44: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap: Functions

Functions are a way to break code into pieces,that can be easily reused.

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 25 / 46

Page 45: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap: Functions

Functions are a way to break code into pieces,that can be easily reused.

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,

which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 25 / 46

Page 46: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap: Functions

Functions are a way to break code into pieces,that can be easily reused.

You call or invoke a function by typing its name,followed by any inputs, surrounded by parenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

CSci 127 (Hunter) Lecture 7 22 October 2019 25 / 46

Page 47: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Today’s Topics

Recap: Slicing & Images

Introduction to Functions

NYC Open Data

CS Survey

CSci 127 (Hunter) Lecture 7 22 October 2019 26 / 46

Page 48: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Accessing Structured Data: NYC Open Data

Freely available source of data.

Maintained by the NYC data analytics team.

We will use several different ones for this class.

Will use pandas, pyplot & folium libraries to analyze, visualize andmap the data.

Lab 7 covers accessing and downloading NYC OpenData datasets.

CSci 127 (Hunter) Lecture 7 22 October 2019 27 / 46

Page 49: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Accessing Structured Data: NYC Open Data

Freely available source of data.

Maintained by the NYC data analytics team.

We will use several different ones for this class.

Will use pandas, pyplot & folium libraries to analyze, visualize andmap the data.

Lab 7 covers accessing and downloading NYC OpenData datasets.

CSci 127 (Hunter) Lecture 7 22 October 2019 27 / 46

Page 50: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Accessing Structured Data: NYC Open Data

Freely available source of data.

Maintained by the NYC data analytics team.

We will use several different ones for this class.

Will use pandas, pyplot & folium libraries to analyze, visualize andmap the data.

Lab 7 covers accessing and downloading NYC OpenData datasets.

CSci 127 (Hunter) Lecture 7 22 October 2019 27 / 46

Page 51: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Accessing Structured Data: NYC Open Data

Freely available source of data.

Maintained by the NYC data analytics team.

We will use several different ones for this class.

Will use pandas, pyplot & folium libraries to analyze, visualize andmap the data.

Lab 7 covers accessing and downloading NYC OpenData datasets.

CSci 127 (Hunter) Lecture 7 22 October 2019 27 / 46

Page 52: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Accessing Structured Data: NYC Open Data

Freely available source of data.

Maintained by the NYC data analytics team.

We will use several different ones for this class.

Will use pandas, pyplot & folium libraries to analyze, visualize andmap the data.

Lab 7 covers accessing and downloading NYC OpenData datasets.

CSci 127 (Hunter) Lecture 7 22 October 2019 27 / 46

Page 53: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

CSci 127 (Hunter) Lecture 7 22 October 2019 28 / 46

Page 54: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

What’s the most popular street for filming?

What’s the most popular borough?

How many TV episodes were filmed?

CSci 127 (Hunter) Lecture 7 22 October 2019 29 / 46

Page 55: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

What’s the most popular street for filming?

What’s the most popular borough?

How many TV episodes were filmed?

CSci 127 (Hunter) Lecture 7 22 October 2019 29 / 46

Page 56: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

What’s the most popular street for filming?

What’s the most popular borough?

How many TV episodes were filmed?

CSci 127 (Hunter) Lecture 7 22 October 2019 29 / 46

Page 57: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

Download the data as a CSV file and store on your computer.

Python program:

CSci 127 (Hunter) Lecture 7 22 October 2019 30 / 46

Page 58: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

Download the data as a CSV file and store on your computer.

Python program:

CSci 127 (Hunter) Lecture 7 22 October 2019 30 / 46

Page 59: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

Download the data as a CSV file and store on your computer.

Python program:

CSci 127 (Hunter) Lecture 7 22 October 2019 31 / 46

Page 60: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

Download the data as a CSV file and store on your computer.

Python program:

CSci 127 (Hunter) Lecture 7 22 October 2019 32 / 46

Page 61: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

Download the data as a CSV file and store on your computer.

Python program:

CSci 127 (Hunter) Lecture 7 22 October 2019 33 / 46

Page 62: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

Download the data as a CSV file and store on your computer.

Python program:

CSci 127 (Hunter) Lecture 7 22 October 2019 34 / 46

Page 63: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Example: OpenData Film Permits

Can approach the other questions in the same way:

What’s the most popular street for filming?

What’s the most popular borough?

How many TV episodes were filmed?

CSci 127 (Hunter) Lecture 7 22 October 2019 35 / 46

Page 64: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that finds the closest collision.

CSci 127 (Hunter) Lecture 7 22 October 2019 36 / 46

Page 65: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that finds the closest collision.

CSci 127 (Hunter) Lecture 7 22 October 2019 37 / 46

Page 66: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 67: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 68: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 69: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 70: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 71: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).

2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 72: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.

3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 73: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.

4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 74: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.

5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 75: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Question

Design an algorithm that uses NYC OpenData collision data and computesthe closest collision to the location the user provides.

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Find data set (great place to look: NYC OpenData).2 Ask user for current location.3 Open up the CSV file.4 Check distance to each to user’s location.5 Save the location with the smallest distance.

CSci 127 (Hunter) Lecture 7 22 October 2019 38 / 46

Page 76: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Today’s Topics

Recap: Slicing & Images

Introduction to Functions

NYC Open Data

CS Survey

CSci 127 (Hunter) Lecture 7 22 October 2019 39 / 46

Page 77: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

CS Survey

CS Survey:Brian Campbell ‘19Grubhub

CSci 127 (Hunter) Lecture 7 22 October 2019 40 / 46

Page 78: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

CSci 127 (Hunter) Lecture 7 22 October 2019 41 / 46

Page 79: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

Possible solutions:

I 7 orders of mixed fruit, orI 2 orders hot wings, 1 order mixed fruit, and 1 sampler plate.

Input: List of items with prices and amount to be spent.

Output: An order that totals to the amount or empty list if none.

Possible algorithms: For each item on the list, divide total by price. If noremainder, return a list of that item. Repeat with two items, trying 1 of thefirst, 2 of the first, etc. Repeat with three items, etc.

“NP-Complete” problem: possible answers can be checked quickly, but notknown how to compute quickly.

CSci 127 (Hunter) Lecture 7 22 October 2019 42 / 46

Page 80: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

Possible solutions:I 7 orders of mixed fruit, or

I 2 orders hot wings, 1 order mixed fruit, and 1 sampler plate.

Input: List of items with prices and amount to be spent.

Output: An order that totals to the amount or empty list if none.

Possible algorithms: For each item on the list, divide total by price. If noremainder, return a list of that item. Repeat with two items, trying 1 of thefirst, 2 of the first, etc. Repeat with three items, etc.

“NP-Complete” problem: possible answers can be checked quickly, but notknown how to compute quickly.

CSci 127 (Hunter) Lecture 7 22 October 2019 42 / 46

Page 81: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

Possible solutions:I 7 orders of mixed fruit, orI 2 orders hot wings, 1 order mixed fruit, and 1 sampler plate.

Input: List of items with prices and amount to be spent.

Output: An order that totals to the amount or empty list if none.

Possible algorithms: For each item on the list, divide total by price. If noremainder, return a list of that item. Repeat with two items, trying 1 of thefirst, 2 of the first, etc. Repeat with three items, etc.

“NP-Complete” problem: possible answers can be checked quickly, but notknown how to compute quickly.

CSci 127 (Hunter) Lecture 7 22 October 2019 42 / 46

Page 82: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

Possible solutions:I 7 orders of mixed fruit, orI 2 orders hot wings, 1 order mixed fruit, and 1 sampler plate.

Input: List of items with prices and amount to be spent.

Output: An order that totals to the amount or empty list if none.

Possible algorithms: For each item on the list, divide total by price. If noremainder, return a list of that item. Repeat with two items, trying 1 of thefirst, 2 of the first, etc. Repeat with three items, etc.

“NP-Complete” problem: possible answers can be checked quickly, but notknown how to compute quickly.

CSci 127 (Hunter) Lecture 7 22 October 2019 42 / 46

Page 83: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

Possible solutions:I 7 orders of mixed fruit, orI 2 orders hot wings, 1 order mixed fruit, and 1 sampler plate.

Input: List of items with prices and amount to be spent.

Output: An order that totals to the amount or empty list if none.

Possible algorithms: For each item on the list, divide total by price. If noremainder, return a list of that item. Repeat with two items, trying 1 of thefirst, 2 of the first, etc. Repeat with three items, etc.

“NP-Complete” problem: possible answers can be checked quickly, but notknown how to compute quickly.

CSci 127 (Hunter) Lecture 7 22 October 2019 42 / 46

Page 84: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

Possible solutions:I 7 orders of mixed fruit, orI 2 orders hot wings, 1 order mixed fruit, and 1 sampler plate.

Input: List of items with prices and amount to be spent.

Output: An order that totals to the amount or empty list if none.

Possible algorithms: For each item on the list, divide total by price. If noremainder, return a list of that item. Repeat with two items, trying 1 of thefirst, 2 of the first, etc. Repeat with three items, etc.

“NP-Complete” problem: possible answers can be checked quickly, but notknown how to compute quickly.

CSci 127 (Hunter) Lecture 7 22 October 2019 42 / 46

Page 85: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Design Challenge

Possible solutions:I 7 orders of mixed fruit, orI 2 orders hot wings, 1 order mixed fruit, and 1 sampler plate.

Input: List of items with prices and amount to be spent.

Output: An order that totals to the amount or empty list if none.

Possible algorithms: For each item on the list, divide total by price. If noremainder, return a list of that item. Repeat with two items, trying 1 of thefirst, 2 of the first, etc. Repeat with three items, etc.

“NP-Complete” problem: possible answers can be checked quickly, but notknown how to compute quickly.CSci 127 (Hunter) Lecture 7 22 October 2019 42 / 46

Page 86: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Today’s Topics

Recap: Slicing & Images

Introduction to Functions

NYC Open Data

CS Survey

CSci 127 (Hunter) Lecture 7 22 October 2019 43 / 46

Page 87: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 88: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 89: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:

Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 90: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 91: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:Example: print("Hello", "World")

Can write, or define your own functions,

which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 92: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 93: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 94: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Recap

On lecture slip, write down a topic you wishwe had spent more time (and why).

Functions are a way to break code intopieces, that can be easily reused.

You call or invoke a function by typing itsname, followed by any inputs, surrounded byparenthesis:Example: print("Hello", "World")

Can write, or define your own functions,which are stored, until invoked or called.

Accessing Formatted Data: NYC OpenData

Pass your lecture slips to the aisles for theUTAs to collect.

CSci 127 (Hunter) Lecture 7 22 October 2019 44 / 46

Page 95: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 96: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 97: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:

I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 98: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;

I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 99: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; and

I repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 100: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 101: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 102: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!

Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 103: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Practice Quiz & Final Questions

Since you must pass the final exam to pass the course, we end everylecture with final exam review.

Pull out something to write on (not to be turned in).

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions!Starting with Summer 18, #4.

CSci 127 (Hunter) Lecture 7 22 October 2019 45 / 46

Page 104: CSci 127: Introduction to Computer Science...Functions Functions are a way to break code into pieces, that can be easily reused. Many languages require that all code must be organized

Writing Boards

Return writing boards as you leave...

CSci 127 (Hunter) Lecture 7 22 October 2019 46 / 46