Compsci 101.2, Fall 2015 19.1 Plan for FWON l Review current assignments and APTs  Review Dictionaries and how to use them  Code and APT walk-through.

Slides:



Advertisements
Similar presentations
Lecture 19. Reduction: More Undecidable problems
Advertisements

Max Cut Problem Daniel Natapov.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Lower bound: Decision tree and adversary argument
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Introducing While loops (and random numbers too) Alice.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
CSE1301 Computer Programming: Lecture 29 Group Project: “Quabble”
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Arrays for Random Storage Another way to use an array is to create an array with dozens, hundreds or thousands of items and then randomly select from them.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Lists in Python.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
By the end of this session you should be able to...
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Compsci 06/101, Spring Cheating at Games l When you play solitaire, does the computer cheat?  What would this mean? l When you play chess, who.
Built-in Data Structures in Python An Introduction.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Compsci 101.2, Fall Plan for FFWOO l Programming in the small and in the large  Making one function work correctly, especially in the context.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
ALGORITHMS.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Compsci 101.2, Fall Plan for October l Review Catchup and Midterm and Future  Make sure everyone understand options l Review Assignment.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
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.
Python Let’s get started!.
Compsci 101.2, Fall Plan for 11/9 and 11/11 l Reviewing Concepts for test  Loops: index and by element  Strings, Lists, Sets, Dictionaries.
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
Compsci 101.2, Fall Plan for WBTB l APT Quiz 3 – due tonight l Solving problems in the wild  How can you change how things are sorted Other.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?
LISTS. LEARNING OBJECTIVES Create a block that accepts a parameter Create a block that returns a value Create scripts that manipulates lists Incorporate.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Algorithms and Pseudocode
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.
Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Compsci 6/101, Spring Why is sorting data important? l Guess a number in [1,100] --- I'll tell you yes or no  How many guesses needed worst-case?
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 March 8, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 29, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 31, 2016 Prof. Rodger.
Trace Tables In today’s lesson we will look at:
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
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Iteration – While Loops
Presentation transcript:

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  Algorithms, searching, sorting? l Toward understanding sorting  What are the algorithms for sorting?  What are the libraries for sorting?

Compsci 101.2, Fall ACM MidAtlantic Programming Contest l Saturday, Nov 7 l 185 teams! l Each team: 3 students, one computer l 5 hours to solve 6-8 problems l Need volunteers to help!  Tshirt, meals, snacks! Fun!  Deliver printouts to teams l Signup here:

Compsci 101.2, Fall Answer Questions

Compsci 101.2, Fall Clever, Snarky, Evil, Frustrating Hangman l 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! l Example of greedy algorithm  Locally optimal decision leads to best solution  More words to choose from means more likely to be hung

Compsci 101.2, Fall Canonical Greedy Algorithm l 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

Compsci 101.2, Fall Greedy not always optimal l What if you have no nickels?  Give $0.31 in change  Algorithms exist for this problem too, not greedy!

Compsci 101.2, Fall Clever Hangman l When you guess a letter, you're really guessing a category (secret word "salty") _ _ _ _ _ and user guesses 'a'  "gates", "cakes", "false" are all the same  "flats", "aorta", "straw", "spoon" are all different l How can we help ensure player always has many words to distinguish between?

Compsci 101.2, Fall 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 not yet guessed: bcdefghijklmnopqrstuvwxyz … (word is designed ) # possible words: 3475 guess a letter: Debugging Output

Compsci 101.2, Fall Debugging Output and Game Play l 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 l You'll approximate release/development using a global variable DEBUG  Initialize to False, set to True when debugging  Ship with DEBUG = False

Compsci 101.2, Fall Look at howto and function categorize l Play a game with a list of possible words  Initially this is all words  List of possible words changes after each guess l 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)

Compsci 101.2, Fall Completing function categorize l 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 l How to re-use guess (template) make copy? l How to create key in dictionary  Why can't key be a list?

Compsci 101.2, Fall Voterigging APT l l For example: [5, 10, 7, 3, 8] answer is 4, why?  If you steal a vote, who do you steal from? Why?  Why is this like coin problem? Clever Hang? l How do you find who to steal from?  At least two approaches, functions or loop Use max and index, or write a loop to find max  When are you done stealing? This governs writing the APT

Compsci 101.2, Fall Answer Questions

Compsci 101.2, Fall Python shortcuts you can ignore l The zip function, tuples from two lists l Does something right if lists have different sizes. Look it up words = ['dog', 'cat', 'fish', 'guava'] counts = [3, 2, 1, 5] cc = zip(word,counts) [('dog', 3), ('cat', 2), ('fish', 1), ('guava', 5)]

Compsci 101.2, Fall Python shortcuts you can ignore l enumerate – the iterable  Sometimes you need an index, sometimes elt  for elt in lst: or  for dex in range(len(lst)): for dex,elt in enumerate(['a', 'b', 'c]): print dex,elt 0 'a' 1 'b' 2 'c'

Compsci 101.2, Fall Python shortcuts you can ignore l Default dictionaries  Typically we see if key in D before modifying  If initialization always same for new keys … import collections dd = collections.defaultdict(int) dd['apple'] 0 ee = {} ee['apple'] Key error

Compsci 101.2, Fall Python functions you CANNOT ignore l We know how to sort, we call sorted  Example from lab and class, sorting tuples  Function sorted returns a new list, original not changed xx = [('dog', 3), ('cat', 2), ('fish', 1), ('guava', 2)] yy = sorted(xx) [('cat', 2), ('dog', 3), ('fish', 1), ('guava', 2)]

Compsci 101.2, Fall First use what you know l You can re-organize data to sort it as you'd like, list comprehensions are your friend xx = [('dog', 3), ('cat', 2), ('fish', 1), ('guava', 2)]... nlist = [(t[1],t[0]) for t in xx] [(3, 'dog'), (2, 'cat'), (1, 'fish'), (2, 'guava')] yy = sorted(nlist) [(1, 'fish'), (2, 'cat'), (2, 'guava'), (3, 'dog')]

Compsci 101.2, Fall Many algorithms for sorting l In some classes knowing these algorithms matters  Not in Compsci 101  Bogo, Bubble, Selection, Insertion, Quick, Merge, …  We'll use built-in, library sorts, all languages have some l We will concentrate on calling or using these  How does API work?  What are characteristics of the sort?  How to use in a Pythonic way?

Compsci 101.2, Fall How do we sort? Ask Tim Peters! l Sorting API  Sort lists (or arrays, …)  Backwards, forwards, …  Change comparison First, Last, combo, … l Sorting Algorithms  We'll use what's standard! Best quote: import this I've even been known to get Marmite *near* my mouth -- but never actually in it yet. Vegamite is right out