Introduction to Computer Science

Slides:



Advertisements
Similar presentations
CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd.
Advertisements

CompSci 101 Introduction to Computer Science Feb 26, 2015 Prof. Rodger.
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
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?
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Compsci 101.2, Fall PFThursday l Review Organization and Problem-Solving  Defining functions, calling functions  Return types, print, None l.
CompSci 6 Introduction to Computer Science October 20, 2011 Prof. Rodger.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
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 November 1, 2011 Prof. Rodger.
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 November 8, 2011 Prof. Rodger.
CompSci 101 Introduction to Computer Science November 18, 2014 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 3, 2015 Prof. Rodger compsci101 spring151.
CompSci 6 Introduction to Computer Science September 13, 2011 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
Introduction to Programming Lecture 4. Key Words of C main main if if else else while while do do for for.
CompSci 101 Introduction to Computer Science February 10, 2015 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
CompSci 101 Introduction to Computer Science January 28, 2016 Prof. Rodger compsci101 spring161.
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
CompSci 6 Introduction to Computer Science Sept. 20, 2011 Prof. Rodger CompSci 6 Fall
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.
CompSci 101 Introduction to Computer Science March 17, 2015 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science February 25, 2016 Prof. Rodger compsci101 spring20161.
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 April 7, 2015 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 24, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science April 14, 2016 Prof. Rodger Lecture today by Sriram Vepuri.
CompSci 101 Introduction to Computer Science March 1, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science April 14, 2016 Prof. Rodger compsci101 spring161.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
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.
CMSC201 Computer Science I for Majors Lecture 03 – Variables
CMPT 120 Topic: Python’s building blocks -> More Statements
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Input and Output Upsorn Praphamontripong CS 1110
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 6 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 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
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
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 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Python Basics with Jupyter Notebook
Functions Divide and Conquer
CompSci 101 Introduction to Computer Science
Presentation transcript:

Introduction to Computer Science CompSci 101 Introduction to Computer Science March 9, 2017 Prof. Rodger Lecture by Lesia Semenova cps101 spring2017 1

cps101 spring2017 2

Rule Lists Go for a walk? If it’s rainy then no Else if it’s sunny then yes Else if it’s warm then yes …. Default no cps101 spring2017 3 3

Announcements Reading and RQ due next time Assignment 5 due tonight APT 6 due Thursday, March 23 Today: Debugging Which code is better? cps101 spring2017 4

Bug and Debug software 'bug' Start small Judicious 'print' Easier to cope Simplest input? Judicious 'print' Debugger too Python tutor Visualizes data step through Rarely do you use a large data file to test your program, how do you know it works? Use a small data file Verify the approach being taken, test small, test frequently How do you 'prove' your code works? 5 5

Debugging Problems Today the main focus is on debugging. There are several problems. Trace by hand to see if you can figure out if they are correct or not, or what to do to correct them. Enter your answers on the google form cps101 spring2017 6

Debug 1 – Does it work? bit.ly/101s17-0309-1 The function sizes has a parameter named words that is a list of strings. This function returns a list of the sizes of each string. For example, sizes(['This', 'is', 'a', 'test']) should return the list [4, 2, 1, 4] def sizes(words): nums = [ ] for w in words: nums = len(w) return nums 7

Debug 2 – Does it work? Bit.ly/101s17-0309-2 The function buildword has a parameter words that is a list of strings. This function returns a string that is made up of the first character from each word in the list. For example, buildword(['This', 'is', 'a', 'test']) returns 'Tiat' def buildword(words): answer = '' for w in words: answer += w[:1] return answer 8

Debug 3 – Does it work? The function middle has a parameter names that is a list of strings, which each string is in the format "firstname:middlename:lastname". This function returns a list of strings of the middlenames. For example, the call middle( "Jo:Mo:Tree", "Mary:Sue:Perez", "Stephen:Lucas:Zhang") returns [ ‘Mo’, ‘Sue’, ‘Lucas’] 9

Debug 3 – Does it work? Bit.ly/101s17-0309-3 The function middle has a parameter names that is a list of strings, which each string is in the format "firstname:middlename:lastname". This function returns a list of strings of the middlenames. def middle(names): middlelist = [] for name in names: name.split(":") middlelist.append(name[1]) return middlelist 10

Debug 4 – Does it work? Bit.ly/101s17-0309-4 The function removeOs has one string parameter named names. This function returns a string equal to names but with all the lowercase o's removed. For example, removeOs(‘Mo Moo Move Over’) returns ‘M M Mve Over’ def removeOs(word): position = word.find("o") while position != -1: word = word[:position] + word[position+1:] return word 11

Problem 5 – Does it work? Bit.ly/101s17-0309-5 The function uniqueDigits has one int parameter number. This function returns the number of unique digits in number. For example, the call uniqueDigits(456655) should return 3. def uniqueDigits(number) digits = [ ] while number > 0: digits.append(number % 10) number = number / 10 return len(digits) 12

Which code is better? For the next two problems, we will look at two examples of code that both work in solving the problem, and think about which code is better. cps101 spring2017 13

Problem 6: Which code is better? Problem: Given a string parameter named phrase and string named letter, the function findWords returns a list of all the words from phrase that have letter in them. Example: findWords("the circus is coming to town with elephants and clowns", "o") would return [‘coming’, ‘to’, ‘town’, ‘clowns’] cps101 spring2017 14

Consider two solutions, which is better? Bit.ly/101s17-0309-6 cps101 spring2017 15

Problem 7 – Which number appears the most times? The function most has one parameter nums, a list of integers. This function returns the number that appears the most in the list. For example, the call most([3,4,2,2,3,2]) returns 2, as 2 appears more than any other number. cps101 spring2017 16

Solution 1 cps101 spring2017 17

Compare with Solution 2 bit.ly/101s17-0309-7 cps101 spring2017 18