CompSci 101 Introduction to Computer Science

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd.
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Compsci 101.2, Fall PFThursday l Review Organization and Problem-Solving  Defining functions, calling functions  Return types, print, None l.
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
CompSci 101 Introduction to Computer Science Sept. 9, 2014 Prof. Rodger President Brodhead speech graduation 2010 CompSci 101 Fall
CompSci 6 Introduction to Computer Science Sept 29, 2011 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not.
CompSci 6 Introduction to Computer Science September 13, 2011 Prof. Rodger.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
CompSci 101 Introduction to Computer Science February 11, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science November 11, 2014 Prof. Rodger CompSci 101 Fall Review for exam.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CompSci 6 Introduction to Computer Science September 27, 2011 Prof. Rodger CompSci 6 Fall
CompSci 101 Introduction to Computer Science February 16, 2016 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
CompSci 101 Introduction to Computer Science March 24, 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.
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review Prof. Katherine Gibson.
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.
CompSci 4 Java 1 Apr 2, 2009 Prof. Susan Rodger. Announcements Assignment 7 questions? –Beware having two events that kick in at the same time! –Beware.
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
CompSci 6 Introduction to Computer Science
Containers and Lists CIS 40 – Introduction to Programming in Python
CompSci 101 Introduction to Computer Science
Python: Control Structures
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Compsci 6/101: PFTW What is Python? What is a programming language?
CompSci 101 Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 27 – Final Exam Review
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Loops CIS 40 – Introduction to Programming in Python
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review
Algorithmic complexity: Speed of algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Final Exam Information
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review
CompSci 101 Introduction to Computer Science
Introduction to Computer Science
Presentation transcript:

CompSci 101 Introduction to Computer Science September 22, 2016 Prof. Rodger score = [10,8,10,9] compsci 101, fall 2016

Announcements Reading and RQ8 due next time Assignment 3 due tonight Assignment 4 out, due Sept 29 APT 3 is due on Tuesday APT Quiz 1 take Sunday-Tuesday midnight Friday – practice APT quiz available Today - EOWF: Solving problems with lists, ifs. End of Week Four compsci 101, fall 2016

Getting help Consider a peer tutor – one hour of one on one help a week. Many take advantage of this contact peer tutoring center Are you getting too much help? After solving APT Can you solve again with a blank sheet of paper or blank file and no help? Are you using 7 step process to solve? I will recruit more tutors if they do not have enough . Bring form by and I will sign it. This class is different than others, you may have never needed a tutor before.

Are you Learning How to Debug? Print is your friend! Create variables! Isolate the problem Comment out sections until you can isolate where the problem is Python Tutor – trace Doesn’t work with files but comment out file and create variable with sample input Works with APTS, you just need to call the function compsci 101, fall 2016

Assignment 3 - Earthquakes Write QuarryBlastQuakes – return the list of earthquakes that are something from a Quarry such as a Quarry Blast Quakes – Description starts with “Quarry” quakes is a list of earthquake strings in correct format def QuarryBlastQuakes(quakes): compsci 101, fall 2016

Assignment 3 - Earthquakes Write QuarryBlastQuakes – return the list of earthquakes that are something from a Quarry such as a Quarry Blast Quakes – Description starts with “Quarry” quakes is a list of earthquake strings in correct format def QuarryBlastQuakes(quakes): compsci 101, fall 2016

Pattern to build and return new list initialize newlist for item in oldlist: if item fits criteria put item in newlist return newlist compsci 101, fall 2016

def quarryQuakes(quakes): answer = [] for q in quakes: pos = q def quarryQuakes(quakes): answer = [] for q in quakes: pos = q.find(" : ") rest = q[pos+3:] if rest.startswith("Quarry"): answer.append(q) return answer compsci 101, fall 2016

def quarryQuakes(quakes): answer = [] for q in quakes: pos = q def quarryQuakes(quakes): answer = [] for q in quakes: pos = q.find(" : ") rest = q[pos+3:] if rest.startswith("Quarry"): answer.append(q) return answer compsci 101, fall 2016

How do you use quarryQuakes? In main: print "Quarry quakes" quarryQ = quarryQuakes(eqList) printQuakes(quarryQ, -1) compsci 101, fall 2016

How do you use quarryQuakes? In main: print "Quarry quakes" quarryQ = quarryQuakes(eqList) printQuakes(quarryQ, -1) compsci 101, fall 2016

String Functions – What is output? bit.ly/101f16-0922-1 compsci 101, fall 2016

Making Decisions Question? True if block False compsci 101, fall 2016

Making Decisions in Python if condition1: Block of code to do if condition is true elif condition2: Block of code to do if condition1 false, condition2 is true else: Block of code to do if other conditions false Can have many elifs, leave out elif, leave out else compsci 101, fall 2016

Making Decisions tools Boolean values: True, False Boolean operators: and, or, not Relational operators: <, <=, >, >= Equality operators: ==, != X Y X and Y X or Y True False compsci 101, fall 2016

bit.ly/101f16-0922-2

Lists A list is a collection of objects scores = [99, 78, 91, 84] allAboutMe = [“Mo”,25, “934-1234”] club=[‘Mo’,‘Jo’,‘Po’, ‘Flo’, ‘Bo’] Lists are mutable – use [num] to change a value Lists are indexed starting at 0, or -1 from the end Functions: max, min, len, sum Slice lists [:] compsci 101, fall 2016

List Examples scores = [10, 8, 10, 9] print scores scores[2] = 5 print max(scores), len(scores), print sum(scores) print scores[1:] print scores[1], scores[-1] scores.append(4) scores += [5] Show them also Scores += 5 compsci 101, fall 2016

List before/after modification 0 1 2 3 score = [10,8,10,9] 10 8 9 10 0 1 2 3 score [2] = 5 10 8 9 5 10 compsci 101, fall 2016

Design pattern of accumulation for item in something Summing to tally a count value += 1 Building a new string by concatenating str += ch Building a new list by appending lst.append(element) OR lst += [element] compsci 101, fall 2016

Processing List Items Process all the items in a list, one item at a time Format: for variable in list: process variable Example: sum = 0 nums = [6, 7, 3, 1, 2] for value in nums: sum = sum + value print sum compsci 101, fall 2016

Learn list functions nums = [6, 7, 3, 1, 2] print sum(nums) compsci 101, fall 2016

Problem: Sum up even numbers in list of numbers Could do it similar to two slides back OR Build a list of the correct numbers, then sum You could do it the way two slides back, but you could also build compsci 101, fall 2016

How to build list of evens and sum? bit.ly/101f16-0922-3 def sumUpEven(nums): answer = question1 for item in nums: if question2: question3 return question4 compsci 101, fall 2016

Problem: What is length of longest string in list of strings? We develop this further in the next lecture compsci 101, fall 2016

From APT 3 - TxMsg http://www.cs.duke.edu/csed/pythonapt/txmsg.html compsci 101, fall 2016

Examples Do one by hand? Explain to partner? Identify Pythonic/programming challenges? compsci 101, fall 2016

Debugging APTs: Going green TxMsg 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 getMessage(original): ret = "" for word in : ret = ret + " " + transform(word) return ret #initial space? Same idiom Didn’t use join so you have an extra space, Use join or strip, slide later also original.split() compsci 101, fall 2016

Debugging APTs: Going green TxMsg 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 getMessage(original): ret = "" for word in : ret = ret + " " + transform(word) return ret #initial space? Same idiom Didn’t use join so you have an extra space, Use join or strip, slide later also original.split() compsci 101, fall 2016

Why use helper function 'transform'? Structure of code is easier to reason about 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 getMessage? Make it look like something you know Now solve txtmsg with code!!!! compsci 101, fall 2016

Python via Problem Solving In the loop for TxMsg we saw: ret = ret + " " + transform(word) - Why does this leave "extra" space at front? - Eliminate with ret.strip() Alternate: collect transform words in list, use join to return Rather than construct string via accumulation and concatenation, construct list with append Use join here compsci 101, fall 2016