Compsci 101 Announcements Sorting, CSV Live Lecture...Compsci 101 Sorting, CSV Live Lecture...

5
Compsci 101 Sorting, CSV Live Lecture 10/15/2020 Compsci 101, Fall 2020 1 Susan Rodger October 15, 2020 Announcements Nothing due today! Assignment 4 due Tue, Oct 20 APT-6 due Thur, Oct 22 Assignment 5 out today, due Tues, Oct 29 Discuss on Oct 20 th Lab 7 Friday There is a prelab! Do before attending lab! It is on the lab page 10/15/2020 Compsci 101, Fall 2020 2 Announcements (page 2) APT 7 out next week, due after exam Mid-Semester Survey – at 110/194 = 57% If we reach 75% - 1 extra point on Exam 2 for everyone Exam 2 is Oct 29 Nov 3 – Voting Day, Vote early now at Duke! 10/15/2020 Compsci 101, Fall 2020 3 PFTD Sorting Sorting using standard Python APIs CSV Library How to read data using standard Python APIs Lambda Language construct to make sorting simpler (next week) 10/15/2020 Compsci 101, Fall 2020 4

Transcript of Compsci 101 Announcements Sorting, CSV Live Lecture...Compsci 101 Sorting, CSV Live Lecture...

Page 1: Compsci 101 Announcements Sorting, CSV Live Lecture...Compsci 101 Sorting, CSV Live Lecture 10/15/2020 Compsci 101, Fall 2020 1 Susan Rodger October 15, 2020 Announcements • Nothing

Compsci 101Sorting, CSVLive Lecture

10/15/2020 Compsci 101, Fall 2020 1

Susan Rodger

October 15, 2020

Announcements• Nothing due today!

• Assignment 4 due Tue, Oct 20

• APT-6 due Thur, Oct 22

• Assignment 5 out today, due Tues, Oct 29

• Discuss on Oct 20th

• Lab 7 Friday

• There is a prelab! Do before attending lab!

• It is on the lab page

10/15/2020 Compsci 101, Fall 2020 2

Announcements (page 2)• APT 7 out next week, due after exam

• Mid-Semester Survey – at 110/194 = 57%• If we reach 75% - 1 extra point on Exam 2 for

everyone

• Exam 2 is Oct 29

• Nov 3 – Voting Day, Vote early now at Duke!

10/15/2020 Compsci 101, Fall 2020 3

PFTD

• Sorting

• Sorting using standard Python APIs

• CSV Library

• How to read data using standard Python APIs

• Lambda

• Language construct to make sorting simpler (next week)

10/15/2020 Compsci 101, Fall 2020 4

Page 2: Compsci 101 Announcements Sorting, CSV Live Lecture...Compsci 101 Sorting, CSV Live Lecture 10/15/2020 Compsci 101, Fall 2020 1 Susan Rodger October 15, 2020 Announcements • Nothing

WOTO-1 Popular Musichttp://bit.ly/101f20-1015-1

• Make a copy of this spreadsheet:• http://bit.ly/101f20-1015-data

• Who are top two artists? Most Songs

• How did you do it?

10/15/2020 Compsci 101, Fall 2020 5

Solve a Larger Problem

• Suppose I were to give you the top 1000 artists

• Top 1,000 songs, find top 10 artists

• How many songs per artist?

10/15/2020 Compsci 101, Fall 2020 6

Scale

• As the size of the problem grows we want …

• The algorithm to still work and be fast!

• What to do?

• Search example

• Google search results work

• SoundHound/Shazam results work

• ContentID on YouTube results work

10/15/2020 Compsci 101, Fall 2020 7

Python to the Rescue

• Using .sort(…), sorted(…), and lambda• Using CSV library and its API

• CSV – Comma Separated Values

• Why use the CSV library?

• How to handle the song “Hello, I Love You”?

• Row 166 in spreadsheet

10/15/2020 Compsci 101, Fall 2020 8

Page 3: Compsci 101 Announcements Sorting, CSV Live Lecture...Compsci 101 Sorting, CSV Live Lecture 10/15/2020 Compsci 101, Fall 2020 1 Susan Rodger October 15, 2020 Announcements • Nothing

Hits by Artists: SongReader.py

• What is returned by this function?• details of csv: next and no split and …

10/15/2020 Compsci 101, Fall 2020 9

What is new? What does it do?

WOTO-2 countByArtisthttp://bit.ly/101f20-1015-2

10/15/2020 Compsci 101, Fall 2020 10

Sorting to Print/Visualize

• Dictionary is ('Beatles', 51) tuples

• But tuples not in order, so we must …

10/15/2020 Compsci 101, Fall 2020 11

Why more complicated than

lines 28 & 29?

What is going on here?

WOTO-3 Calling countByArtisthttp://bit.ly/101f20-1015-3

10/15/2020 Compsci 101, Fall 2020 12

Page 4: Compsci 101 Announcements Sorting, CSV Live Lecture...Compsci 101 Sorting, CSV Live Lecture 10/15/2020 Compsci 101, Fall 2020 1 Susan Rodger October 15, 2020 Announcements • Nothing

Two APIs: CSV and Sorting

• CSV Library to read and process data

• Comma-separated, but can by ":" separated, or any character as we'll see later

• Similar to reading a file – returned by open• Iterable is returned by csv.reader• The next function advances iterable

• Don't call split, we can access by index• Also by header-row label with csv.dictreader

10/15/2020 Compsci 101, Fall 2020 13

Sorting API and Sorting Concepts

• What is counts.items() – how is it sorted?

• What does sorted return?

• A list, you can slice a list, look for clues!

• What can be sorted? A sequence• sorted(counts.items())

10/15/2020 Compsci 101, Fall 2020 14

How does Python evaluate slice?

Sorting by Number of Songs

• Sort by first value vs sort by second value

• Need to put sequence back to original format

10/15/2020 Compsci 101, Fall 2020 15

If we comment out 33, what's printed? Why?

Python Sorting API

• We'll use both sorted() and .sort() API

• How to call, what options are

• How to sort on several criteria

• Creating a new list, modifying existing list• sorted(..) creates list from .. Iterable

• x.sort() modifies the list x, no return value!

10/15/2020 Compsci 101, Fall 2020 16

Page 5: Compsci 101 Announcements Sorting, CSV Live Lecture...Compsci 101 Sorting, CSV Live Lecture 10/15/2020 Compsci 101, Fall 2020 1 Susan Rodger October 15, 2020 Announcements • Nothing

API to change sorting

• In SongReader.py we changed order of tuples to change sorting order

• Then we sliced the end to get "top" songs

• Can supply a function to compare elements

• Function return value used to sort, key=function

• Change order: reverse=True

10/15/2020 Compsci 101, Fall 2020 17

Sorting Examples

• Use key=function argument and reverse=True

• What if we want to write our own function?

10/15/2020 Compsci 101, Fall 2020 18

WOTO-4 Sortinghttp://bit.ly/101f20-1015-4

10/15/2020 Compsci 101, Fall 2020 19

Assignment 5 – Clever Hangman

• Must finish Hangman assignment first!

• This is a copy and modify

• Make it hard to win

• Keep changing the word• More than that

• We will discuss next time

10/15/2020 Compsci 101, Fall 2020 20