Compsci 101, APTs and Sequences and Selection

Slides:



Advertisements
Similar presentations
Group practice in problem design and problem solving
Advertisements

1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
Testing. Definition From the dictionary- the means by which the presence, quality, or genuineness of anything is determined; a means of trial. For software.
Compsci 06/101, Fall Compsci 6/101: Sept 6-10 l We've looked at the language Python  Names and types: int, float, long, String, file, …  Operations:
Compsci 101.2, Fall PFTT (plan for this Thursday) l What is a Python program?  In the context of what we do in Compsci 101  In a neuroscience.
Compsci 101.2, Fall Plan For the Week l Solve problems by programming in Python  Like to do "real-world" problems, but we're very new to the.
Compsci 06/101, Fall Compsci 6/101: Random debugging?!# l The joys and rewards of writing code to solve a problem  How do we know where to begin?
Compsci 101.2, Fall PFThursday l Review Organization and Problem-Solving  Defining functions, calling functions  Return types, print, None l.
Compsci 06/101, Fall What will you do in Compsci 6 Today? l Learn about selection and if/else statements  In context of solving problems  Understanding.
Compsci 06/101, Fall How to build a program l Create a module  Can be used by other modules via import  Collection of functions, later collection.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
Compsci 6/101, Spring PFTW: Sequences aka Strings&Lists l From Return values to Random-ness [aka two R's]  What power does random provide? 
Compsci 101.2, Fall PFTWeek l Introduce new Python concepts  Control: if, elif, else, for  Data: Strings, Lists Operators on data: slicing,
Compsci 101.2, Fall Plan for TDAFB (after fall break) l Hear via about plans to catch-up and stay-ahead  Profs. Rodger and Astrachan to.
Compsci 101, Fall Plan For The Day (PFTD) l Practice solving problems  Some solved with a computer, some with Python  Differences in solving.
CompSci 101 Introduction to Computer Science January 28, 2016 Prof. Rodger compsci101 spring161.
Compsci 6/101, Spring PFTW: Functions, Control, Python/Tools l How do functions work and why do we use them?  Functions we call (APIs), Functions.
Compsci 06/101, Spring Compsci 6: PFTW l Problem solving and (Python) programming  What are the steps in solving an APT?  How do you get better.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Python Programing: An Introduction to Computer Science
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
CPS 100, Spring Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CompSci 101 Introduction to Computer Science Sept 13, 2016 Prof. Rodger compsci101 fall161.
String and Lists Dr. José M. Reyes Álamo.
CSC 222: Object-Oriented Programming
Topic: Python Lists – Part 1
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Algorithmic complexity: Speed of algorithms
CSC 221: Computer Programming I Spring 2010
Tuples and Lists.
CompSci 101 Introduction to Computer Science
Containers and Lists CIS 40 – Introduction to Programming in Python
Engineering and Debugging an App Chapter 15
CompSci 101 Introduction to Computer Science
CMPT 120 Topic: Python strings.
CompSci 101 Introduction to Computer Science
Topic: Functions – Part 2
CompSci 101 Introduction to Computer Science
Introduction to Algorithms
Compsci 6/101: PFTW What is Python? What is a programming language?
CompSci 101 Introduction to Computer Science
Compsci 101, Intro to Python
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CompSci 101 Introduction to Computer Science
UNIT 3 CHAPTER 1 LESSON 4 Using Simple Commands.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Algorithmic complexity: Speed of algorithms
Compsci 6/101: PFTW Review how APTs and Python work, run
Training & Development
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Algorithmic complexity: Speed of algorithms
Introduction to Computer Science
“Everything Else”.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review
CompSci 101 Introduction to Computer Science
Introduction to Computer Science
Compsci 201, O-Notation and Maps (Interfaces too)
Presentation transcript:

Compsci 101, APTs and Sequences and Selection Owen Astrachan Kristin Stephens-Martinez January 25, 2018 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection

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

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

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

Compsci 101, Spring 2018, Sequences+Selection Organization Matters https://www.youtube.com/watch?v=1ve57l3c19g 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

Compsci 101, Spring 2018, Sequences+Selection WOTO Redux http://bit.ly/101spring18-jan23-2 Sneak Preview: lists and tuples Last time, do it and review the concepts 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection

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

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

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

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

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

Compsci 101, Spring 2018, Sequences+Selection WOTO: Pancakes http://bit.ly/101spring18-jan25-pancakes Correctness counts, score > 1/3 == 100% After discussion test student understanding 1/25/2018 Compsci 101, Spring 2018, Sequences+Selection

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

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

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

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

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 13-16 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

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

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

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

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

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

How to teach pancake flipping http://www.youtube.com/watch?v=W_gxLKSsSIE Is this computer science? http://bit.ly/zykOrh For longer, more complex robotic tasks http://www.youtube.com/watch?v=4usoE981e7I 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

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

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

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

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

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

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

Compsci 101, Spring 2018, Sequences+Selection One more APT Let’s have Brunch with the Digerati https://www2.cs.duke.edu/csed/pythonapt/portmanteau.html 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

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

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?