array algorithms - amazon s3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select...

32
Array Algorithms Lecture 08 npm run pull … npm run start pollev.com/comp110

Upload: others

Post on 08-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Array AlgorithmsLecture 08

npm run pull … npm run start

pollev.com/comp110

Page 2: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Fixing the "Black Screen of Death"

• When you see a screen that looks like the black screen to the right:

• The error may be in a file other than the one you are working on. The file with the error is this one.

• The simplest short-term fix in lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save.

• The specific line # of the file that TypeScript believes the error is on is this number in parenthesis.

Page 3: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Use Chrome

• Chrome tends to handle accidental infinite loops better than other web browsers.

• When working on 110 assignments and lectures, we suggest you always use Chrome.

• If you do not want Chrome to be your default browser outside of 110, that's fine, just open up Chrome to http://localhost:3000 each time you start and close the browser that opens automatically for you.

Page 4: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Office Hours Tickets

• No matter how small the problem, or how empty office hours is, you must always submit the office hours help request form.

• Why? It ensures two things:

• First, that the stats for how busy office hours are always accurate. It's super frustrating to show up believing there's no one being helped when actually there is no one free. Simple questions often take 10 minutes.

• Second, it shows a track record on your profile of coming in for help and putting in an effort to use the resources you have available.

• TAs are instructed to only help those who have submitted tickets.

Page 5: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Email & Office Hours Best Practices

• By and large you all have been incredibly awesome to work with in office hours and over e-mail this semester. Keep it up!

• In a few isolated instances, though, I've read e-mails to TAs or heard reports of office hours interactions that are far beneath Carolina standards. The TAs and I have feelings, too.

• If you get a warning e-mail from me with suggestions on how to be a decent human being, that's the only warning I'll give. Continued issues will be escalated.

Page 6: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Midterm 0 – Next Thursday 9/28

• Special Review Session next Wednesday 9/27 in GSB100 from 5-7pm

• Additional practice problems will be released soon

• Spend quality time understanding everything on the next Problem Set and Worksheet. These are designed to help prepare you for the midterm!

Page 7: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

PS2 - Weather Stats

• Analyze the last 30 years of weather data from RDU Airport.

• Autograding will open up by Saturday evening.

• Due Tuesday 9/26 at 11:59pm• Submit Sunday for full EC• Submit Monday for partial EC

• Pay close attention to what we do in class today. It will help you with the problem set.

Page 8: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

WS2 and WS3

• Worksheet 2 due tomorrow by 11:59pm

• TAs will not directly answer questions of "is this right?"

• Instead: • Try asking more pointed questions about topics your uncertain of

• Try coming up with another example just like it

• Worksheet 3 will go out tonight and be due Monday.• Shorter turn around than normal to help be guided preparation for Midterm.

Page 9: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Warm-up #1: What is printed when this code completes?

let i: number = 10;while (i > 0) {

print(i);i = i - 4;

}

Page 10: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Answer: 10, 6, 2

let i: number = 10;while (i > 0) {

print(i);i = i - 4;

}

Page 11: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Warm-up: Array Question

• Write this down on pen/paper as you work through it!

• What are the elements of array a after the while loop completes?

let a: number[] = [];

let i: number = 0;while (i < 4) {

if (i <= 0) {a[i] = i;

} else {a[i] = a[i - 1] + i;

}i = i + 1;

}

print(a);

Page 12: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Warm-up: Array Question

• Answer: let a: number[] = [];

let i: number = 0;while (i < 4) {

if (i <= 0) {a[i] = i;

} else {a[i] = a[i - 1] + i;

}i = i + 1;

}

print(a);

Index Element

0 0

1 1

2 3

3 6

Page 13: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Hands-on #1: Write a loop

• Open 00-increment-operator-app.ts

• Write a while loop that prints numbers 0 through 3

• Be very careful not to write an infinite loop!

• Done? Check-in on PollEv.com/comp110

Page 14: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

import "introcs";

function main(): void {

let i: number = 0;while (i < 3) {

print(i);i++;

}

}

main();

Page 15: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

The Increment Operator: ++

• Incrementing a variable's value by 1 is so common we have a special operator for doing so.

<variable>++ ex: i++;

• The ++ operator increments a variable's value by 1.

• Using it as a statement, (i.e. i++;) it is equivalent to i = i + 1;

• Read as "Increment <variable> by 1."

Page 16: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Working with Data

• Today's Goal: Use arrays and loops to analyze Joel Berry II's game data from last year.

Page 17: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Today's Data

• Data source: ESPN.com

• The Game Log table to the right was copied and pasted into Excel

Page 18: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Today's Data

• The table was cleaned up a bit in Excel and formatting removed

• Column header names were changed to match properties we'll use in our code (we'll come back to this soon)

Page 19: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Today's Data

• Finally it was saved as a special type of file:

• CSV UTF-8 (Comma delimited) (*.csv)

• This is a common data table format that is easy to work with in code.

Page 20: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Today's Data

• Here's what the contents of the CSV file look like.

• It is stored in:

data/joel-berry-ii.csv

• Notice it's just plain text!

• Each row gets a line, each column is separated by a comma, hence "Comma Separated Values (CSV)" file.

Page 21: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Modelling a "Game" with a class

• Each Game has properties associated with it:• date• opponent• points• and more...

• These are column names in our data table

• In our program, we'll declare a class to model a single Game's stats with properties for each column in the table we care about.

• Note: we do not need to use every column but the names of properties much match the column headers in the CSV file.

class Game {

date: string = "";opponent: string = "";points: number = 0;fouls: number = 0;

}

Page 22: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Reading a CSV into an Array of Game Objects

• Last week we saw how to process an "array of numbers", i.e. number[]

• We want to work with our data table as an "array of Games", i.e. Game[]

• Each row in the data table will have a Game object associated with it. Each column in the data table is a property of the Game object.

index date opponent points fouls0 11/11/2016 TULANE 23 3

1 11/13/2016 CHATTANOOGA 18 2

2 11/15/2016 LONG BEACH ST 23 3

3 11/19/2016 HAWAI'I 2 2

4 11/21/2016 CHAMINADE 8 1

games[2]

games[4].points

Page 23: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

How do we prompt the user for a CSV file?

• There's a function in the introcs library for that!

• Documentation:

promptCSV(prompt:string, cname:Class, callback:Function): void

• Parameters:1. prompt - a string value presented to the user as instructions2. cname - the name of the class (i.e. Game) each row of the CSV corresponds to3. callback - the name of the function that will called once the user selects a CSV.

The function must declare a single parameter of type cname[] (i.e. Game[]).

Page 24: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Follow-along: Calling promptCSV

• Open 01-csv-app.ts

• In the main function, call:

• In the process function, print:

promptCSV("Select player data CSV", Game, process);

print(games[29].opponent);print("Points: " + games[29].points);

Page 25: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Hands-on #2

• In the process function:

1. Declare a counting variable named i2. Write a while loop that repeats while i is less than games.length3. Inside the while loop's repeat block:

1. Call the statLine function with games[i] and print the returned string

2. Increment i by 1

4. Test and confirm that stat lines of all games are printing out.

5. Check-in on PollEv.com/comp110

Page 26: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

function process(games: Game[]): void {print("Processing CSV...");let i: number = 0;while (i < games.length) {

print(statLine(games[i]));i++;

}}

Page 27: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Pseudo-algo: counting # of games fouled out

• When a college basketball player accumulates 5 fouls, (s)he fouls out of the game. How can we count the # of games Joel fouled out?

1. Set count to 0.

2. Loop element-by-element through an array.

3. If the element's # of fouls is equal to 5, then increment count.

4. Reached the end of the array? The count variable now contains the # of games the player fouled out.

Page 28: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Follow-Along: Let's implement a function to calculate the number of games fouled out.• Open 02-berry-stats-app.ts

• Notice our main function is setup to prompt for a CSV and call the process function already. We also have two functions defined whose implementations are incorrect: gamesFouledOut and totalPoints.

• First, we need to call the gamesFouledOut function from the processfunction and print the number it returns.

• Then, we need to implement the algorithm from the previous slide in code inside of the gamesFouledOut function.

print("Games fouled out: " + gamesFouledOut(games));

Page 29: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Counting the # of games Joel fouled out…

function gamesFouledOut(games: Game[]): number {let count: number = 0;let i: number = 0;while (i < games.length) {

if (games[i].fouls === 5) {count++;

}i++;

}return count;

}

1. Set count to 0.

2. Loop element-by-element through an array.

3. If the element's # of fouls is equal to 5, then increment count.

4. Reached the end of the array? The count variable now contains the # of games the player fouled out.

Page 30: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Hands-on #3: Summing Points

• Together: Let's call the totalPoints function from process:• totalPoints(games)

• Your goal: Correctly implement the totalPoints function with a sum algorithm. It should return the sum of points across all of Joel Berry's games last season.

• Hint #1: You'll need a variable to store the sum as you work element-by-element through the games array.

• Hint #2: You wrote a sum algorithm in lec07 / 03-sum-array-app.ts – how can you modify it to make use of the games array instead?

• Done? Check-in on PollEv.com/comp110 once you've got sum working.

• Done early? Try writing an averagePoints function.

Page 31: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

function totalPoints(games: Game[]): number {let sum: number = 0;let i: number = 0;while (i < games.length) {

sum = sum + games[i].points;i++;

}return sum;

}

Page 32: Array Algorithms - Amazon S3s3.amazonaws.com/comp110/2017-08-fall/slides/08... · lecture is select all text in the file (Ctrl+A) and then comment it out (Ctrl+/) and save. •The

Problem Set 2: Weather Stats

• The work you do in PS2 will be similar to what we did in class today!

• There are some algorithms you will need to spend some time thinking through in order to find solutions to. This will take time!

• Suggestion: lay out some cards on a table face down and get out a sheet of paper. Try writing down each variable you need and how you would update the variables as you move through your array of cards element-by-element.

• Before you write code, try writing down steps in English.