PFTWeek 914 918 l Incremental construction as design

  • Slides: 27
Download presentation
PFTWeek 9/14 -9/18 l Incremental construction as design pattern Ø Ø Build programs: start

PFTWeek 9/14 -9/18 l Incremental construction as design pattern Ø Ø Build programs: start small, add with confidence Build new strings: append/concatenate values • Also use join to create a string from a list Ø Build lists: append values, alter existing values • Also use. split() to create list from a string l Compsci 101 specifics: Python -> Course Ø APT Quiz and ensuring you do well Compsci 101. 2, Fall 2015 7. 1

Software Dreams l Translating ideas into (Python) code Ø Create interesting “heads”, “totem poles”

Software Dreams l Translating ideas into (Python) code Ø Create interesting “heads”, “totem poles” ? Ø Create software for face recognition? Gait? Ø Create "five four" from "four five"? Ø Create "SCUBA" from "self contained underwater breathing apparatus" l Master the syntax of the language? Ø Organization of program constructs Ø Knowledge of libraries Ø Practice and experience! Compsci 101. 2, Fall 2015 7. 2

Top 10 list for surviving in Comp. Sci 101 Compsci 101. 2, Fall 2015

Top 10 list for surviving in Comp. Sci 101 Compsci 101. 2, Fall 2015 7. 3

Top 10 list for surviving in Comp. Sci 101 10 - Ask Questions 9

Top 10 list for surviving in Comp. Sci 101 10 - Ask Questions 9 - Eat lots of pizza 8 – Learn how to spell Rodger/Astrachan 7 – Read the online textbook 6 – Do the reading quizzes 5 – Check Piazza every dat 4 – Visit your professors in their office hours 3 – Learn how to debug your programs 2 – Seek help (one hour rule!) 1 – Start programming assignments early! Compsci 101. 2, Fall 2015 7. 4

Why is this person so important to this course? Compsci 101. 2, Fall 2015

Why is this person so important to this course? Compsci 101. 2, Fall 2015 7. 5

Why is this person so important to this course? l l Compsci 101. 2,

Why is this person so important to this course? l l Compsci 101. 2, Fall 2015 Brad Miller Have you donated yet? 7. 6

Translating Ideas Into Code http: //bit. ly/101 fall 15 -0910 -2 Compsci 101. 2,

Translating Ideas Into Code http: //bit. ly/101 fall 15 -0910 -2 Compsci 101. 2, Fall 2015 7. 7

Incremental + : numbers and strings l Wtht vwls cn y stll rd ths

Incremental + : numbers and strings l Wtht vwls cn y stll rd ths sntnc? Ø Ø Ø Create a no-vowel version of word Examine each character, if it's not a vowel … Pattern of building a string def no. Vowels(word): ret = "" for ch in word: if not is_vowel(ch): ret = ret + ch return ret Compsci 101. 2, Fall 2015 7. 8

Counting vowels in a string l Accumulating a count in an int is similar

Counting vowels in a string l Accumulating a count in an int is similar to accumulating characters in a string def vowel. Count(word): value = 0 for ch in word: if is_vowel(ch): value = value + 1 return value l Alternative version of adding: value += 1 Compsci 101. 2, Fall 2015 7. 9

From high- to low-level Python def reverse(s): 7 r = "" 8 for ch

From high- to low-level Python def reverse(s): 7 r = "" 8 for ch in s: r = ch + r return r 9 l Create version on the right using dissassembler dis(code. py) Compsci 101. 2, Fall 2015 0 LOAD_CONST 3 STORE_FAST 1 ('') 1 (r) 6 9 12 >> 13 16 SETUP_LOOP LOAD_FAST GET_ITER FOR_ITER STORE_FAST 19 22 25 26 29 >> 32 LOAD_FAST 2 (ch) LOAD_FAST 1 (r) BINARY_ADD STORE_FAST 1 (r) JUMP_ABSOLUTE 13 POP_BLOCK 10 >> 33 LOAD_FAST 36 RETURN_VALUE 24 (to 33) 0 (s) 16 (to 32) 2 (ch) 1 (r) 7. 10

Bug and Debug l l software 'bug' Start small Ø l Easier to cope

Bug and Debug l l software 'bug' Start small Ø l Easier to cope Judicious 'print' Ø l Debugger too Verify the approach being taken, test small, test frequently Ø How do you 'prove' your code works? Compsci 101. 2, Fall 2015 7. 11

Anatomy of a Python String l l String is a sequence of characters Ø

Anatomy of a Python String l l String is a sequence of characters Ø Functions apply to sequences: len, slice [: ], sorted, Ø Methods applied to strings, specific to strings: Ø st. split(), st. startswith(), st. strip(), st. lower(), st. find(), st. count(), st. join() Strings are immutable sequences Ø Ø l Cannot change a string, can only create new one • What does upper do? See resources for functions/methods on strings Iterable: Can loop over it, Indexable: can slice it Compsci 101. 2, Fall 2015 7. 12

Anatomy of a Python List l Lists are indexable Ø Ø l Lists are

Anatomy of a Python List l Lists are indexable Ø Ø l Lists are iterable: for x in [1, 2, 3]: Ø l l Confusing boolean use, if 3 in [1, 2, 3]: Lists are mutable Ø l Start with index 0, index with [int], slice too Indexing past end? Change: lst[0] = 5, can append, can extend Lists are heterogenous, can store any type of element, including lists! Methods. count(), . append(), . index(), . sort() Compsci 101. 2, Fall 2015 7. 13

Lynn Conway See Wikipedia and lynnconway. com l Joined Xerox Parc in 1973 Ø

Lynn Conway See Wikipedia and lynnconway. com l Joined Xerox Parc in 1973 Ø Revolutionized VLSI design with Carver Mead Joined U. Michigan 1985 Ø Professor and Dean, retired '98 l l NAE '89, IEEE Pioneer '09 Helped invent dynamic scheduling early '60 s IBM l l Transgender, fired in '68 Compsci 101. 2, Fall 2015 7. 14

Standard accumulation idiom def wcount(collection, word): total = 0 for elt in collection: if

Standard accumulation idiom def wcount(collection, word): total = 0 for elt in collection: if elt == word: total = total + 1 return total l How do we count 'scarlet' in Scarlet Letter? Ø Ø Ø Or dagger in Hamlet or Romeo? Or friend in Little Brother? Or CGAT in a genome? Compsci 101. 2, Fall 2015 7. 15

If we knew all Python's built ins, … l Suppose we want to (what

If we knew all Python's built ins, … l Suppose we want to (what are types and values) f = open("/data/kjv 10. txt") st = f. read() words = st. split() angels = wcount(words, "angel") # can use Python built in too devils = words. count("devil") Compsci 101. 2, Fall 2015 7. 16

Accumulation revisited def get. Firsts(collection, letter): total = [] for elt in collection: if

Accumulation revisited def get. Firsts(collection, letter): total = [] for elt in collection: if elt. startswith(letter): total. append(elt) return total l Finding words that start with 't', The Bible? Ø Or words that start with 'U' in The Illiad? Compsci 101. 2, Fall 2015 7. 17

Work Together on Expression Review http: //bit. ly/101 fall 15 -0915 -1 Compsci 101.

Work Together on Expression Review http: //bit. ly/101 fall 15 -0915 -1 Compsci 101. 2, Fall 2015 7. 18

New APT out late today - Txtmsg Compsci 101. 2, Fall 2015 7. 19

New APT out late today - Txtmsg Compsci 101. 2, Fall 2015 7. 19

Examples Compsci 101. 2, Fall 2015 7. 20

Examples Compsci 101. 2, Fall 2015 7. 20

Debugging APTs: Going green l Tx. Msg APT: from ideas to code to green

Debugging APTs: Going green l Tx. Msg APT: from ideas to code to green Ø Ø Ø What are the main parts of solving this problem? Transform words in original string • Abstract that away at first Finding words in original string • How do we do this? def get. Message(original): ret = "" for word in original. split() : ret = ret + " " + transform(word) return ret #initial space? Compsci 101. 2, Fall 2015 7. 21

Debugging APTs: Going green l Tx. Msg APT: from ideas to code to green

Debugging APTs: Going green l Tx. Msg APT: from ideas to code to green Ø Ø Ø What are the main parts of solving this problem? Transform words in original string • Abstract that away at first Finding words in original string • How do we do this? def get. Message(original): ret = "" for word in original. split() : ret = ret + " " + transform(word) return ret #initial space? Compsci 101. 2, Fall 2015 7. 22

Why do we write code this way? l Structure of code is easier to

Why do we write code this way? l Structure of code is easier to reason about Ø Ø l Harder to develop this way at the beginning Similar to accumulate loop, build on what we know We can debug pieces independently Ø Ø What if transform returns "" for every string? Can we test transform independently of get. Message? Compsci 101. 2, Fall 2015 7. 23

How is search done quickly? l https: //books. google. com/ngrams/graph? content=dystopian%2 Cpanopticon&year_ start=1940&year_end=2008&corpus=15&s moothing=3&share=&direct_url=t

How is search done quickly? l https: //books. google. com/ngrams/graph? content=dystopian%2 Cpanopticon&year_ start=1940&year_end=2008&corpus=15&s moothing=3&share=&direct_url=t 1%3 B%2 Cdystopian%3 B%2 Cc 0%3 B. t 1%3 B%2 Cpa nopticon%3 B%2 Cc 0 Compsci 101. 2, Fall 2015 7. 24

Start simple, solve problems l How does Ngram Viewer work? Ø Ø l books.

Start simple, solve problems l How does Ngram Viewer work? Ø Ø l books. google. com/ngrams Try: North Carolina, South Carolina, Virginia, Georgia How many words start with 't', how many times 'coward' occurs, how many times … Ø Ø Loop over collection of words Increment a count in certain conditions Compsci 101. 2, Fall 2015 7. 25

Searching, Counting, Comparing l Count plays of a song played on Spotify? Ø l

Searching, Counting, Comparing l Count plays of a song played on Spotify? Ø l What songs have staying power? Ø l http: //poly-graph. co/timeless/index. html What do I really like? Ø l Loop over songs, increment a count http: //fivethirtyeight. com/features/spotifyknows-me-better-than-i-know-myself/ We can almost do this with Python, but desktop API deprecated, going mobile Compsci 101. 2, Fall 2015 7. 26

Understanding Totem. Crawler http: //bit. ly/101 fall 15 -0915 -2 Compsci 101. 2, Fall

Understanding Totem. Crawler http: //bit. ly/101 fall 15 -0915 -2 Compsci 101. 2, Fall 2015 7. 27