Compsci 101 Debugging and Thinking Owen Astrachan Kristin
Compsci 101, Debugging and Thinking Owen Astrachan Kristin Stephens-Martinez April 19, 2018 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 1
X is for … • XOR • (a or b) and not (a and b) • XML • e. Xtensible Markup Language • Xerox Parc • From Mice to Windows 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 2
Y is for … • YAML and YACC • Yet Another. . . • Y 2 K • How many bits are enough bits? • You. Tube • Connected to computing. . . 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 3
Plan for Last Thursday • Discuss how to tackle data APT/quiz • How do you verify your code is correct? • The good and bad of APTs and green dance • Discuss test-taking and the second midterm • What about the final? How do you prepare? • How can you get better in the last 10 days? 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 4
Explore Data • http: //bit. ly/durham-houses • Who owns the most houses in Durham? • What is average square feet for N-bedrooms? 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 5
Accessing the Data • Exporting data and reading with Python • https: //www 2. cs. duke. edu/courses/spring 18/compsci 101/code/april 19/ • What libraries will we use? • How will we verify we've got the data? 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 6
What You'll Start With • Website says 114, 391 records with header • ' , 0)]TAX YEAR'), . . . (12, 'MAILING NAME 1'). . . 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 7
Actual Data and Data APT • You're given 114 thousand lines of data and each line contains 64 data items • How will you debug your ideas? • What's the downside of so much data? • What entity owns the most houses/properties? • Identify the entity, count # records 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 8
Dictionaries for Data • Using the data whose index is 12 on each row, determine the top 20 or top N property owners • We will provide the index to use: 12 • We will help identify output, see below • The fourth largest owner is the USA with 484 • So line[12] is something like "USA" 484 times • How do we use a dictionary to solve? 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 9
What are Keys and Values? • For each N-bedroom property, where N is an int • Determine average # square feet for N-beds • # beds is index 57, sqft is index 46 • List in order of # properties with N bedrooms • Print 3 bedroom data first if more 3 -bedrooms • # 2 bedroom properties is 14, 932 • Average sq feet for 2 -bedroom is 1221. 63 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 10
Calculating Average • We need at least two values • # N bedroom houses for each N • Total number of square feet for each N • Average is Total Sqft/ Total Number • Key: N, Value: [sqft, count] • We could keep a list of all the square footage • Value is list of sqfoot ints • Key is still N, # bedrooms 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 11
More details for Averages • { 2 : [2500, 10], 3 : [3300, 20], . . . } • Key is # bedrooms, Value is [total sqft, # props] • We will sort by d[N][1] • We will print d[N][0]/d[N][1] • Or (k, v) {2 : [2300, 2400, 2728, . . . ] • For each N, keep list of sqft for each property • Sort by len(d[N]) • Print sum(d[N])/len(d[N]) 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 12
WOTO http: //bit. ly/101 spring 18 -april 19 -1 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 13
Barbara Liskov • Among first women in US to earn Ph. D. in Computer Science: 1968 • Turing Award 2008, SE and PL • Prof. Stephens-Martinez's academic great, grandparent • Ksm>Fox>Brewer>Weihl>Liskov “It's much better to go for the thing that's exciting. But the question of how you know what's worth working on and what's not separates someone who's going to be really good at research and someone who's not. There's no prescription. It comes from your own intuition and judgment. ” 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 14
From Debugging to Testing • What can you learn from the second midterm • How to solve a problem partially on a test • How to go from ideas to code to credit • What to do when you don't know what to do • Can you proof-read your answers? • Utilizing test-taking skills, reading skills, . . . 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 15
Midterm 2 • What will you learn from your score, rubrics? • Be sure to review answers, understand how rubric applies • If you think there's a mistake, request regrade • Work toward improvement on final as needed • What do ola and ksm learn from midterm? • Where there are conceptual gaps, where we need to put resources for you to succeed 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 16
Reversing Dictionary • From d = {k: v, . . . } to dd = {v: k, . . . } • What is key in new dd? Where do we get it? • What is the value in new dd? Init and upate 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 17
I don't know how to get new key • Invent a helper function and use it • You don't see v//100 – create func(. . . ) 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 18
How to find closest cities? ")]Reykjavik", 64. 1), ("Berlin", 52. 3), ("Paris", 48. 5 , ( ")Nairobi", -1. 17), ("Canberra", -35. 15[( • What are we comparing? Conceptually? Code? • Latitudes, second elements of tuples, floats • Finding minimal distance between all pairs • Could find all, sort, find smallest • Could simply remember smallest 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 19
Rubrics try to be helpful • "If I had data in this format I could solve it, I don't know how to do that, I don't have time to do that" • Show format for data • Write actual Python code • You might get credit for the code you write • You won't likely get credit for knowing what to do • You might bias the grader in a good way! 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 20
Uh-Oh, . . . What do I do? • If you can't start, solve a simpler problem? • Invent 3 -tuple, find smallest one 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 21
Creating new form of data • Looping over all pairs in a list • midterm woto: http: //bit. ly/101 spring 18 -mid 2 -2 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 22
Pattern Matching, Debugging • What if you match the wrong code/concept/idea? • I have code to read a csv file, I should use it • I wrote a function in Part A, I should call it • Remind yourself: names, types, values • What is the type here? How do I use it? 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 23
Proof-Reading, Debugging on Paper • Edge/corner/special cases I should consider? • If I recognize them, should I write code? • What about negative differences in latitudes? • Invent a function like abs(. . ) • You could write it, ask, or presume ok • Helper functions are your friend! • Is it worth writing code to handle corner/edge cases? 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 24
Format of Final Exam • You have three hours. We plan for two hour exam • Accommodation start early or go late • We must grade exam quickly • We have 20 -25 multiple choice problems • There is no partial credit, 40% to 50% of score • We have other problems similar to midterms • How to practice? This and previous midterms 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 25
Going Green • Interesting. Party • https: //www 2. cs. duke. edu/csed/pythonapt/inte restingparty. html • How do I answer this question? • How do I write Python code for that? • Algorithm not always obvious, code. . . Easy? • return max(d. values()) 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 26
Going Green • Friend Score • https: //www 2. cs. duke. edu/csed/pythonapt/frie ndscore. html • My friends specified by indexes • Transform "YYNNY" to [0, 1, 4] • Find friends of 0 and 1 and 4 • get. Friends(index) returns set of friends? 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 27
WOTO http: //bit. ly/101 spring 18 -april 19 -2 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 28
Questions 4/192018 Compsci 101, Spring 2018, Debugging and Thinking 29
- Slides: 29