CompSci 101 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.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
An Introduction to Textual Programming
General Programming Introduction to Computing Science and Programming I.
By the end of this session you should be able to...
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 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
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 101 Introduction to Computer Science November 18, 2014 Prof. Rodger.
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.2, Fall Plan for FWON l Review current assignments and APTs  Review Dictionaries and how to use them  Code and APT walk-through.
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
CompSci 101 Introduction to Computer Science January 28, 2016 Prof. Rodger compsci101 spring161.
Algorithms and Pseudocode
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
Compsci 6/101, Spring Being Clever at Games, aka Cheating l When you play solitaire, does the computer cheat?  What would this mean? l When.
GCSE Computing: Programming GCSE Programming Remembering Python.
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.
Algorithms and Flowcharts
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
CompSci 101 Introduction to Computer Science March 31, 2016 Prof. Rodger.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
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.
CMSC201 Computer Science I for Majors Lecture 03 – Variables
BASIC PROGRAMMING C SCP1103 (02)
Introduction to Computing Science and Programming I
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
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
Formatting Output.
BASIC PROGRAMMING C SCP1103 (02)
IGCSE 6 Cambridge Effectiveness of algorithms 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
Introduction to Programmng in Python
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
UMBC CMSC 104 – Section 01, Fall 2016
CompSci 101 Introduction to Computer Science
Engineering Innovation Center
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Learning to Program in Python
Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Computational Thinking for KS3
Introduction to Decision Structures and Boolean Variables
CompSci 101 Introduction to Computer Science
Let’s all Repeat Together
Life is Full of Alternatives
Python Basics with Jupyter Notebook
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Hardware is… Software is…
CompSci 101 Introduction to Computer Science
Presentation transcript:

CompSci 101 Introduction to Computer Science Nov 2, 2017 Prof. Rodger compsci 101 fall 2017

Announcements No Reading/RQ until after Exam 2 Assignment 5 due, Assignment 6 due Nov 8 APT 6 due Tuesday APT Quiz 2 - Sunday-Wednesday Today: Debugging Which code is better? compsci 101 fall 2017

Assignment 7 – Demo Smarty, Evil, Frustrating Hangman Computer changes secret word every time player guesses to make it "hard" to guess Must be consistent with all previous guesses Idea: the more words there are, harder it is Not always true! Example of greedy algorithm Locally optimal decision leads to best solution More words to choose from means more likely to be hung compsci 101 fall 2017

Canonical Greedy Algorithm How do you give change with fewest number of coins? Pay $1.00 for something that costs $0.43 Pick the largest coin you need, repeat If you have 50 cent piece it also works 50cent is the guy in the right, a wrapper compsci 101 fall 2017

Greedy not always optimal What if you have no nickels? Give $0.31 in change Algorithms exist for this problem too, not greedy! compsci 101 fall 2017

Smarty Hangman When you guess a letter, you're really guessing a category (secret word "salty") _ _ _ _ _ and user guesses 'a' "gates", "cakes", "false" are all a the same, in 2cd position "flats", "aorta", "straw", "spoon" are all a in different places How can we help ensure player always has many words to distinguish between? Should we tell the user she has guessed an 'a' correctly? Or should we switch to a new secret word. IT DEPENDS! If we switch to "aorta", user might get word quickly, how many words fit A _ _ _ A? If we switch to SPOON? Run it with DEDUG OFF Run it with DEBUG ON compsci 101 fall 2017

Debugging Output number of misses left: 8 secret so far: _ _ _ _ _ _ _ _ (word is catalyst ) # possible words: 7070 guess a letter: a a__a___a 1 … _a______ 587 __aa____ 1 __a_____ 498 ________ 3475 ___a____ 406 ____a___ 396 # keys = 48 number of misses left: 7 letters guessed: a … (word is designed ) # possible words: 3475 guess a letter: This is debugging output, shows some of the 48 different categories when an 'a' is guessed for an 8 letter word. Notice That after one guess the computer switches to "designed" which has 3,475 possibilities to distinguish between, where as switching To _ A A _ _ _ _ wouldn't give so many words, turns out that' isaacson, not really a word anyway , but the secret word ANACONDA would be just as bad, only one word fitting that pattern too compsci 101 fall 2017

Debugging Output and Game Play Sometimes we want to see debugging output, and sometimes we don't While using microsoft word, don't want to see the programmer's debugging statements Release code and development code You'll approximate release/development using a global variable DEBUG Initialize to False, set to True when debugging Ship with DEBUG = False The program is in the code directory for assignment6 on the course site, I tested it there and it runs compsci 101 fall 2017

Look at howto and categorizing words Play a game with a list of possible words Initially this is all words List of possible words changes after each guess Given template "_ _ _ _", list of all words, and a letter, choose a secret word Choose all equivalent secret words, not just one Greedy algorithm, choose largest category words = categorize(words, guess, letter) This slide is all about explaining the code at the end of the slide, which is from the howto compsci 101 fall 2017

Computing the Categories Loop over every string in words, each of which is consistent with guess (template) This is important, also letter cannot be in guess Put letter in template according to word _ _ _ a _ t might become _ _ _ a n t Build a dictionary of templates with that letter to all words that fit in that template. How to create key in dictionary? compsci 101 fall 2017

Everytime guess a letter, build a dictionary based on that letter Example: Four letter word, guess o Key is string, value is list of strings that fit compsci 101 fall 2017

Keys can’t be lists [“O”,”_”,”O”,”_”] need to convert to a string to be the key representing this list: “O_O_” compsci 101 fall 2017

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 This is the moth that Grace Hopper found in the Harvard Mark 2, so she put it in the log book Verify the approach being taken, test small, test frequently How do you 'prove' your code works?

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. compsci 101 fall 2017

Debug 1 – Does it work? bit.ly/101f17-1102-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 compsci 101 fall 2017

Debug 2 – Does it work? bit.ly/101f17-1102-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 compsci 101 fall 2017

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’] compsci 101 fall 2017

Debug 3 – Does it work? bit.ly/101f17-1102-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 compsci 101 fall 2017

Debug 4 – Does it work? bit.ly/101f17-1102-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 compsci 101 fall 2017

Problem 5 – Does it work? bit.ly/101f17-1102-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)

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. compsci 101 fall 2017

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’] compsci 101 fall 2017

Consider two solutions, which is better? bit.ly/101f17-1102-6 compsci 101 fall 2017

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. compsci 101 fall 2017

Solution 1 compsci 101 fall 2017

Compare with Solution 2 bit.ly/101f17-1102-7 compsci 101 fall 2017