V is for Viral Video Husky Dog sings with iPAD 18 million views - - PowerPoint PPT Presentation

v is for
SMART_READER_LITE
LIVE PREVIEW

V is for Viral Video Husky Dog sings with iPAD 18 million views - - PowerPoint PPT Presentation

V is for Viral Video Husky Dog sings with iPAD 18 million views https://www.youtube.com/watch?v=Mk4bmK-acEM Virtual Memory It is and is not there! Virtual Reality Augmenting IRL http://bit.ly/vr-playlist


slide-1
SLIDE 1

V is for …

  • Viral Video
  • Husky Dog sings with iPAD – 18 million views
  • https://www.youtube.com/watch?v=Mk4bmK-acEM
  • Virtual Memory
  • It is and is not there!
  • Virtual Reality
  • Augmenting IRL
  • http://bit.ly/vr-playlist

11/5/2020 Compsci 101, Fall 2020 2

slide-2
SLIDE 2

Announcements

  • APT

APT-7 du due T Thur ursda day, N Nov 5 5, T TODAY!

  • APT

APT-8 d due e Tues esday, N Nov 10

  • Assign 6

n 6 Recommender, d due ue T Thur urs 1 11/12

  • Assign 7

n 7 Create due ue, Monda nday, N Nov 1 16!

  • Final E

Exam Su m Sunday, N Nov 2 22 – ta take o

  • n th

this day

11/5/2020 Compsci 101, Fall 2020 3

slide-3
SLIDE 3

PFTD

  • Colla

laboration and C Creativ ivit ity

  • The power of working together with code
  • Revie

iew m w module les a and e exceptio ions

  • Concepts used in Lab 9, leveraging creativity
  • How

w to s solv lve it it

  • Thinking about steps 5-7, Python, and scale
  • Med

edal alTabl able APT APT

11/5/2020 Compsci 101, Fall 2020 4

slide-4
SLIDE 4

The Power of Collaboration: Ge Wang, Duke Prof. at Stanford

  • Duk

uke 2 2000: M Mus usic a and nd Computer S Science

  • https://www.stanforddaily.com/2016/03/09/qa-with-ge-wang-

father-of-stanford-laptop-orchestra/

  • http://www.youtube.com/watch?v=ADEHmkL3HBg
  • Abou
  • ut D

Design i in Compsci ci 308 308

11/5/2020 Compsci 101, Fall 2020 5

Our investment into a huge and meticulous design process was a huge factor in making later progress. 35000+ lines of code / design / documentation gave us a project we were all very happy and proud to be a part of.

slide-5
SLIDE 5

Assignment 7: Create, Due Nov. 16

Grace period til Nov. 19, No late days! Must be turned in by Nov 19 This assignment is required!

Pick o

  • ne

ne: Video: : Green en d dance, advertisemen ement f for 101, s , song, o , other er Poem or

  • em or Multiple H

e Hai aikus Sto tory Comi Comic One-pag ager er Feedback Let's s see s some e examples

11/5/2020 Compsci 101, Fall 2020 6

slide-6
SLIDE 6

Comic

11/5/2020 Compsci 101, Fall 2020 9

slide-7
SLIDE 7

Comic

11/5/2020 Compsci 101, Fall 2020 10

slide-8
SLIDE 8

Lab 9 Modules for Creating

11/5/2020 Compsci 101, Fall 2020 11

  • “MadLibs”

” → Ta Tag-a-Sto tory

  • User chooses template
  • Computer fills everything in

In lecture I saw a <color> <noun> For lunch I had a <adjective> <food> The day ended with seeing a <animal> <verb> in <place>

slide-9
SLIDE 9

Main Parts for tag-a-story

  • Put e

everything t together, t the template a and words

  • Storyline.py
  • Loadi

ding ng and h d handl ndling ng u user choosing ng t templates

  • TemplateChooser.py
  • Loadi

ding ng and p d picking ng t the word f for a a given n tag

  • Replacements.py

11/5/2020 Compsci 101, Fall 2020 12

slide-10
SLIDE 10

From <noun> to story

In lecture I saw a <color> <noun> For lunch I had a <adjective> <food> The day ended with seeing a <animal> <verb> in <place> In lecture I saw a magenta house For lunch I had a luminous hummus The day ended with seeing a cow sleep in Mombasa

11/5/2020 Compsci 101, Fall 2020 13

This Photo by Unknown Author is licensed under CC BY-NC-ND This Photo by Unknown Author is licensed under CC BY-NC-ND This Photo by Unknown Author is licensed under CC BY-SA

slide-11
SLIDE 11

Let's create/modify a story

  • Choose a

a template o

  • r make a

a new one

  • We'll choose lecturetemplate.txt first
  • Add a

a new category/replacement

  • We'll choose number and list some choices
  • Run the p

program a and te test o

  • ur modification
  • ns
  • Randomized, hard to test, but doable

11/5/2020 Compsci 101, Fall 2020 14

slide-12
SLIDE 12

Modules in Action makeStory in Storyline.py

  • How c

can an we a acces ess TemplateChooser functi tion

  • ns?
  • import and access as shown

11/5/2020 Compsci 101, Fall 2020 15

slide-13
SLIDE 13

Understanding Code/Module doWord in StoryLine.py

  • What d

does get etRep eplac acemen ent do? ?

  • How does getReplacement do it?

11/5/2020 Compsci 101, Fall 2020 16

slide-14
SLIDE 14

The other module’s “what”

  • Get te

t template

  • TemplateChooser.getTemplateLines(DIR)
  • What:
  • From the templates in the directory DIR (type: str)
  • Return a list of strings, where each element is a line

from one of the templates in DIR

  • Wor
  • rd f

for

  • r a

a tag

  • Replacements.getReplacement(TAG)
  • What:
  • Return a random word that matches TAG (type: str)

11/5/2020 Compsci 101, Fall 2020 17

slide-15
SLIDE 15

Main Parts for tag-a-story

  • Put e

everything t together, t the template a and words

  • Storyline.py
  • Loadi

ding ng and h d handl ndling ng u user choosing ng t templates

  • TemplateChooser.py
  • Loadi

ding ng and p d picking ng t the word f for a a given n tag

  • Replacements.py

11/5/2020 Compsci 101, Fall 2020 18

slide-16
SLIDE 16

TemplateChooser.py Steps

  • Lis

ist a all t ll templates in in the fold lder

  • Get

t user i input t that c t choos

  • oses o
  • ne
  • Load t

that template

  • Return as l

list of

  • f strings

11/5/2020 Compsci 101, Fall 2020 19

slide-17
SLIDE 17

These Steps in Code getTemplateLines in TemplateChooser.py

  • Read d

director

  • ry o
  • f te

templates, c con

  • nvert to

to d dicti tion

  • nary
  • Let user choose one, open and return it

11/5/2020 Compsci 101, Fall 2020 21

slide-18
SLIDE 18

Creating User Menu dirToDictionary in TemplateChooser.py

  • What d

does this f function r return? W What t type?

11/5/2020 Compsci 101, Fall 2020 22

slide-19
SLIDE 19

pathlib Library

  • Pa

Path: “rodger/Pycharm/cps101/lab09/temp/haiku.txt”

  • Th

The e pathlib lib library is is more recent/ t/Pyth thon3

  • Simpler, easier to use than functions from os
  • Handles d

doma

  • main s

speci cifics cs!

  • Doesn’t matter if on Windows, Mac, etc.
  • We worry about the what

at, it handles the how

  • w

11/5/2020 Compsci 101, Fall 2020 23

slide-20
SLIDE 20

pathlib Library cont.

  • Pa

Path: “rodger/Pycharm/cps101/lab09/temp/haiku.txt”

  • pathlib.Path(DIR).iterdir()
  • Returns iterable of Path objects representing each

“thing” in the directory DIR

  • Path o
  • bject’s .

s .parts s – tuple o

  • f strings, eac

each el elem emen ent i is a a pi piece of a fi filename’s pa s path

  • (‘rodger’, ‘Pycharm’, ‘cps101’,’lab09’,

‘temp’, ‘haiku.txt’)

11/5/2020 Compsci 101, Fall 2020 24

slide-21
SLIDE 21

Understanding the Unknown chooseOne in TemplateChooser.py

  • We w

will r return t to this, bu but a analyze pa parts n now

  • What's familiar? What's not familiar …

11/5/2020 Compsci 101, Fall 2020 25

slide-22
SLIDE 22

Python exceptions

  • What s

should you

  • u d

do i

  • if you
  • u prom
  • mpt user f

for

  • r a

a num number and nd t the hey ent nter " "one"

  • Test to see if it has digits?
  • Us

Use e exceptio ions wit with try: and except:

  • See code in function chooseOne from

TemplateChooser.py

11/5/2020 Compsci 101, Fall 2020 26

slide-23
SLIDE 23

Handling Exceptions

  • What h

happe pens: x = int("123abc")

11/5/2020 Compsci 101, Fall 2020 27

slide-24
SLIDE 24

WOTO-1 Modules http://bit.ly/101f20-1105-1

11/5/2020 Compsci 101, Fall 2020 28

slide-25
SLIDE 25

Christos Papadimitriou

  • Gödel

el p prize f for c concep ept o

  • f "price o

e of anar archy" y"

11/5/2020 Compsci 101, Fall 2020 29

I would say that, quite generally, computer scientists are going to find themselves interacting more with other fields. I encourage my students to go completely wild in their curriculum, to go out and learn not only that which they think they should learn in order to be good computer scientists—usually mathematics and programming and engineering and so on— but learn about everything else, about psychology, economics, about business, about biology, about the humanities. (2008)

slide-26
SLIDE 26

When and What’s in CompSci 101

  • Problem t

to s

  • sol
  • lve
  • Use 7 steps
  • Step 5: How do you translate algorithm to

code?

  • What do you use to solve it?
  • When do you use it?

11/5/2020 Compsci 101, Fall 2020 30

slide-27
SLIDE 27

What are the “what’s”?

  • Data

ta Structures: list, t, s set, d dicti tion

  • nary, tu

tuple

  • Loops a

and nd iterables: f from

  • m for
  • r t

to

  • while t

to

  • it

iterdir ir() ()

  • Other

er:

  • List comprehensions
  • Parallel lists
  • Lambda
  • If…if…if
  • If…elif…else

11/5/2020 Compsci 101, Fall 2020 31

slide-28
SLIDE 28

Quick When’s and What’s for 101

  • Whichever m

makes es m more s e sense e to you:

  • Parallel lists vs dictionaries
  • If…if…if vs if…elif…else
  • List comprehension vs for loop
  • Tuples vs Lists
  • If you want to prevent mutation -> tuples
  • Need single line function
  • Lambda vs create normal helper function

11/5/2020 Compsci 101, Fall 2020 32

slide-29
SLIDE 29

APT – Sorted Freqs

11/5/2020 Compsci 101, Fall 2020 33

slide-30
SLIDE 30

What's the best way to …

  • Sorted

edFreq eqs

  • https://www2.cs.duke.edu/csed/pythonapt/sortedfreqs.html
  • Count

unt how m many ny t times each s string ng o

  • ccur

urs

  • Create d = {}, iterate over list updating values
  • Use data.count(w) for each w
  • Wait, that looks like …

11/5/2020 Compsci 101, Fall 2020 34

slide-31
SLIDE 31

APT: SortByFreqs

11/5/2020 Compsci 101, Fall 2020 36

slide-32
SLIDE 32

Wait, wait, but what's …

  • SortByFreqs
  • https://www2.cs.duke.edu/csed/pythonapt/sortbyfreqs.html
  • Sort b

by # # occurren ences h high t to l low

  • Tuples with count/lambda and reverse=True?
  • Break ties in alphabetical order: two passes

11/5/2020 Compsci 101, Fall 2020 37

slide-33
SLIDE 33

WOTO-2 http://bit.ly/101f20-1105-2 https ps://bi bit.ly/duk ukecs1 s101sp2 sp20-fre reqs

11/5/2020 Compsci 101, Fall 2020 41

slide-34
SLIDE 34

APT: MedalTable

  • http://bit.ly/apt-med

edal al-table

11/5/2020 Compsci 101, Fall 2020 42

slide-35
SLIDE 35

11/5/2020 Compsci 101, Fall 2020 43

slide-36
SLIDE 36

Tracking the Data

  • Wha

hat d do w we need t d to obt btain f n for e each h count untry?

  • What's the data, how do we store it?
  • What's the data, how do we calculate it?
  • Method a

and d code de to trans nsform i input ut

  • What will we store, how do we initialize/update
  • Verifying we've done this properly

11/5/2020 Compsci 101, Fall 2020 44

slide-37
SLIDE 37

Use a dictionary?

  • 3 d

dictionar aries es

  • Country to Gold count
  • Country to Silver count
  • Country to Bronze count
  • 1 d

dicti tion

  • nary
  • Map country to [gld cnt, sil cnt, bro cnt]

11/5/2020 Compsci 101, Fall 2020 45

slide-38
SLIDE 38

Example: dictionary d

  • Process fir

irst strin ing: "KOR TPE UKR“

  • d[“KOR”] = [1, 0, 0] # gold
  • d[“TPE”] = [0, 1, 0] # silver
  • d[“UKR”] = [0, 0, 1] # bronze
  • Process second string: "KOR KOR TPE”
  • d[“KOR”] = [2, 0, 0] # gold
  • d[“KOR”] = [1, 1, 0] # silver
  • d[“TPE”] = [0, 1, 1] # bronze

11/5/2020 Compsci 101, Fall 2020 47

slide-39
SLIDE 39

Sorting the Data

  • Use

se d dic ictionary t to get list list o

  • f t

tuples [('JPN', [0, 1, 0]), ('KOR', [3, 1, 0]), ('TPE', [0, 1, 1]), ('UKR', [0, 0, 1])]

  • Sort by country
  • Sort by bronze medals in reverse
  • Sort by silver medals in reverse
  • Sort by gold medals in reverse
  • Convert to strings to return
  • Print to verify, two passes like SortByFreqs

11/5/2020 Compsci 101, Fall 2020 49

slide-40
SLIDE 40

Sorting dictionary in MedalTable

  • Write h

helper functi tion

  • n to

to c create t that t dicti tion

  • nary
  • Sort b

t by [g [gold, silver, b bron

  • nze].

]. J Just s t sort? t?

  • Tuples and lists sorted in index order
  • Why is lambda x: x[1] a list below?

11/5/2020 Compsci 101, Fall 2020 50