Download presentation
Presentation is loading. Please wait.
1
Compsci 101, APTs and Sequences and Selection
Owen Astrachan Kristin Stephens-Martinez January 25, 2018 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
2
Compsci 101, Spring 2018, Sequences+Selection
D is for … Debugging A key skill in making your programs run Data (Science) Creating information from 0's and 1's Dictionary Ultimate Python Data Structure 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
3
Compsci 101, Spring 2018, Sequences+Selection
PFTD and toward PFTW On Tuesday we saw: Functions in Python Decomposition, parameters, calling Flow of Control How does a program execute, how do function calls work? Program Development and Execution How do you run a program, test a program, debug a program Remind students what we did last week, review functions, function calls. Differentiate. We’ll discuss more of that today 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
4
Compsci 101, Spring 2018, Sequences+Selection
Today we’ll see … Quick review of functions Foundation of writing complex programs in any language, grow a program, don't build it Understanding and using sequences Indexing and Slicing Selective execution Guarding statements depending on conditions Functions are great. We need more tools. We need sequences, strings and lists and we need selective execution 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
5
Compsci 101, Spring 2018, Sequences+Selection
Organization Matters This is an all time favorite of mine, may not be something you want to do? Apparently Susan still uses it too I think when Robert de Niro looks at Cybil Shepard in the clip it's clear that he's infatuated with her. It's a long time ago. We are still infatuated with teaching and coding and we hope they will be. We also want to be organizized with building programs. 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
6
Compsci 101, Spring 2018, Sequences+Selection
WOTO Redux Sneak Preview: lists and tuples Last time, do it and review the concepts 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
7
Compsci 101, Spring 2018, Sequences+Selection
Functions Summarized Function call and Function definition related Call must provide correct arguments Names don’t matter, types are important: arguments match parameters Functions help design, implement, organize Without functions no APIs, no big programs Opening a file would be 100’s of lines, instead it’s f = open(“hello.txt”) Differentiate arguments and parameters 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
8
Lynn Conway See Wikipedia and lynnconway.com Joined Xerox Parc in 1979
Revolutionized VLSI design with Carver Mead Joined U. Michigan 1985 Professor and Dean, retired '98 NAE '89, IEEE Pioneer '09 Helped invent dynamic scheduling early '60s IBM Transgender, fired in '68
9
Compsci 101, Spring 2018, Sequences+Selection
Problem Interlude We’re going to solve an APT together. Help with APT-0 due today and APT-1 due a week from today 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
10
APT Pancake: http://bit.ly/pancakes101
How do you solve this (or any) problem? Run through examples, construct a method for solving – translate to algorithm for Python If you can’t do this with paper/pencil? Tough! Some APTs are hard problems to solve Translating to code easy, it’s the problem Some APTs have easy-to-see algorithms Translating to code is hard Read the problem. Look at an example and discuss why some are correct. 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
11
Solving (APT) Problems
Identify simple cases that are quickly solved What are these in Pancake problem? Don’t solve for N, solve for 5, then generalize When there are two parameters? Fix one, vary the other one Helps identify cases Basic problem solving ideas. 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
12
Compsci 101, Spring 2018, Sequences+Selection
WOTO: Pancakes Correctness counts, score > 1/3 == 100% After discussion test student understanding 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
13
Three pancakes in a two-cake pan
Number of cakes in the system First 5 minutes Number of cakes in the system Second 5 minutes Walk through cases after going over examples. A' means A cooked on one side. A'' means cooked on both sides 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
14
Three pancakes in a two-cake pan
Number of cakes in the system Third 5 minutes How many minutes to cook all three pancakes? 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
15
Methodically by hand, small values
Pan has capacity 8, vary #pancakes Can you cook 11 in 15 minutes? Why? Can you cook 13 in 15 minutes? Why? cakes 5 6 7 8 9 10 11 12 13 14 15 16 17 18 time ? 1 2 3 4 5 6 7 8 10 11 9 12 For 12, shown, we can cook in 15 minutes. Take 5-8 out and cook 1-4 all the way. Then finish them up. Can't do 13 though. Explain 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
16
Methodically by hand, small values
Pan has capacity 8, vary #pancakes, cook 12? cakes 5 6 7 8 9 10 11 12 13 14 15 16 17 18 time 1 2 3 4 5 6 7 8 1 2 Filled in more from last slide, can we do 2? 3 11 5 6 7 8 4 10 9 12 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
17
Methodically by hand, small values
Pan has capacity 8, vary #pancakes, 17 cakes? cakes 5 6 7 8 9 10 11 12 13 14 15 16 17 18 time 20 1 2 3 4 5 6 7 8 1 2 We can do in 20 minutes, what about 17? 3 11 5 6 7 8 4 10 9 12 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
18
Methodically by hand, small values
Pan has capacity 8, Generalize to algorithm? cakes 5 6 7 8 9 10 11 12 13 14 15 16 17 18 time 20 25 1 2 3 4 5 6 7 8 1 2 Can we generalize? 3 11 5 6 7 8 4 10 9 12 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
19
Compsci 101, Spring 2018, Sequences+Selection
Pancake Algorithm If you have pan of size 17 and 34 pancakes If you have pan of size 17 and 43 pancakes PAN fits 100 pancakes, but you have 452 PAN fits N pancakes, but you have P if P <= N then time needed is … X = P // N what does this mean for time? Y = P % N what does this mean for time? Summarize, review // and % with examples like N = 8 (PAN just did) and # pancakes = 12 and 13 (just did) 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
20
From Algorithm to Python
We need one more tool: selection Execute different code depending on … What is a vowel? When is a year a leap-year? Is there a minimal age for US President? If condition is true then do something Condition: true or false Something: any Python code Motivate selection, example coming 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
21
Three versions of is_vowel
def is_vowel(ch): if ch =='e': return True if ch == 'a': if ch == 'i': if ch == 'o': if ch == 'u': return False def is_vowel(ch): c = "aeiou".count(ch) if c > 0: return True else: return False def is_vowel(ch): return "aeiou".count(ch) > 0 Simple if statement, return. That’s all we need for APTs. Emphasize boolean part of if statement 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
22
Finishing the APT Pancake Problem
Use the algorithm developed AFTER verifying with pencil and paper aka by hand Test first in our Eclipse (VM or Local) Create main program block Call minutesNeeded with several examples Use APT testing system, submit, reflect Can we have more than one return statement? Develop from scratch with help from class. Get all green 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
23
How to teach pancake flipping
Is this computer science? For longer, more complex robotic tasks Do robots matter? Do they dream? Self-driving cars? Machine learning? Who developed a first upper bound on pancake sorting? Bill Gates 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
24
What problems can we solve?
We can create arithmetic expressions +, -, *, /, %, **, // and more Import math and math.sqrt(..), math.sin(..) We can create and combine strings Using + or * for example We can write and call functions That can use arithmetic expressions and strings 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
25
Compsci 101, Spring 2018, Sequences+Selection
Selection Summarized We can use selection: if statement if boolean condition: block What can change? Boolean and block What is a boolean condition? True/False See type(3 < 5) Relational operators: <, <=, >, >=, ==, != Boolean operators: and, or, not Won’t do this in detail, but a good summary. Show type(3 < 5) in pydev console 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
26
Additional Tools for Building
We need sequences to access and create data Documents rather than words Spread sheets rather than single data/formula Operations on these sequences Access all elements: loops Access elements selectively: if statements Motivate sequences. Collections of elements. Analogy as shown. We need operations on sequences. We'll show a few today, more next week 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
27
Compsci 101, Spring 2018, Sequences+Selection
Python Sequences Types String and List share characteristics Both are sequences, have a length Both support indexing and slicing Conversion functions help connect them x="hello world" y=["hello”, "world"] What is len(x)? String is immutable, list is mutable Two important examples. Preview with mutable, immutable, don't dwell 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
28
Indexing Python Sequences
x="hello world" y=["hello", "world"] Indexing provides access to individual elements Compare x[0]and y[0] Start with 0, what is last valid positive index? Compare x[-1] and y[-1] What is negative index of second to last element? Index –n is the same as index len(seq) - n 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
29
Slicing Python Sequences
x="hello world" y=["my", "big", "beautiful", "world"] Slicing provides sub-sequence (string or list) Compare x[0:3]and y[0:3] What is length of subsequence? seq[2:4] Compare x[4:-1] and y[2:-1] Is last index part of subsequence? We can omit value, e.g., x[2:] or x[:4], good shortcut! Show in pydev console 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
30
Compsci 101, Spring 2018, Sequences+Selection
One more APT Let’s have Brunch with the Digerati Read the problem statement carefully Why can’t we create netizen in function? Combination of “internet” and “citizen” How do we solve with slicing? 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
31
Anatomy of Python String
String is a sequence of characters These are strings of length 1 Functions can be applied to strings: len Operators can be applied: [n] and [m:n] Methods/functions applied to strings "HELLO".lower() "the duke way".split(" ") Review these, split needed for one APT 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection
32
Bug and Debug software 'bug' Start small Judicious 'print'
Easier to cope Judicious 'print' Debugger too Verify the approach being taken, test small, test frequently, add to working code What does “working code” mean?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.