Compsci 101.2, Fall 2015 20.1 Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?

Slides:



Advertisements
Similar presentations
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Advertisements

Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Analysis of Algorithms intro.  What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching Arrays Linear search Binary search small arrays
Elementary Data Structures and Algorithms
Main task -write me a program
COMP s1 Computing 2 Complexity
 1 String and Data Processing. Sequential Processing Processing each element in a sequence for e in [1,2,3,4]: print e for c in “hello”: print c for.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Computer Science 111 Fundamentals of Programming I Search Algorithms.
Searching and Sorting Topics Sequential Search on an Unordered File
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
A Computer Science Tapestry 11.1 From practice to theory and back again In theory there is no difference between theory and practice, but not in practice.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
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.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSC 211 Data Structures Lecture 13
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
Compsci 06/101, Spring Compsci 6/101: PFTW, Feb 28-March 4 l Algorithms and Data Structures  Sets and how they are used in Python (data structure)
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
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.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
Compsci 6/101, Spring WBFSB l Plan for this week  Get test back, review, look for problems/weaknesses  How to address these?  Assignments.
Searching Topics Sequential Search Binary Search.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Compsci 101.2, Fall Plan for October l Structuring Data and Code for Efficiency  Computer time, how expensive is it?  Data storage, how.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
Compsci 06/101, Spring This week in Compsci 6/101 l From sets to dictionaries  Incredibly powerful in solving problems  Power in Perl, Python,
Genome Revolution: COMPSCI 004G 5.1 Searching l Why does SSAHA compute locations of all n-mers?  agttc occurs at (1,3) (1,18) (2,6) (3,13), …  What is.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
CompSci 101 Introduction to Computer Science March 29, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 31, 2016 Prof. Rodger.
Compsci 201, Compare+Analysis
CompSci 101 Introduction to Computer Science
Week 9 - Monday CS 113.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Algorithmic complexity: Speed of algorithms
CSC 222: Object-Oriented Programming
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Computer Science 112 Fundamentals of Programming II
CompSci 101 Introduction to Computer Science
Compsci 201, Analysis+Markov
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
And now for something completely different . . .
Last Class We Covered Data representation Binary numbers ASCII values
CSc 110, Spring 2017 Lecture 39: searching.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Searching and Sorting Topics Sequential Search on an Unordered File
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
UMBC CMSC 104 – Section 01, Fall 2016
Algorithmic complexity: Speed of algorithms
Data Structures Sorted Arrays
Lecture 11 Algorithm Design
Algorithmic complexity: Speed of algorithms
Compsci 201, Collections, Hashing, Objects
Presentation transcript:

Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience? How do you get that?  Most APTs and test problems share structure: There's a loop, there's a selection/decision, update l You can often do this with a list comprehension, but you don't have to!  Write code you can understand, but you must be able to read code with list comprehensions and with dictionaries

Compsci 101.2, Fall SortedFreqs l l What do you return? How many elements does it contain? Can you categorize them?  Read problem, understand what to return  Then think about how to calculate/create values l Is efficiency an issue with APTs?  Computers do millions of operations a second  Your time is important!  Always possible to get time-limit exceeded

Compsci 101.2, Fall Customer Statistics l l What's returned? How many elements does it contain? Can you categorize them?  Read problem, understand what to return  Then think about how to calculate/create values l How can you find names that occur more than once? Can you filter names/elements?  Filtering is a great use of list comprehensions!  Creating return values in correct order, issues?

Compsci 101.2, Fall Questions

Compsci 101.2, Fall Shafi Goldwasser l 2012 Turing Award Winner l RCS professor of computer science at MIT  Twice Godel Prize winner  Grace Murray Hopper Award  National Academy  Co-inventor of zero-knowledge proof protocols How do you convince someone that you know [a secret] without revealing the knowledge? l Honesty and Privacy Honesty and Privacy Work on what you like, what feels right, I know of no other way to end up doing creative work

Compsci 101.2, Fall DictionaryTimings.py l Updating (key,value) pairs in structures  Search through unordered list  Search through ordered list  Use dictionary l Why is searching through ordered list fast?  Guess a number from 1 to 1000, first guess?  What is 2 10 ? Why is this relevant? 2 20 ?  Dictionary is faster! But not ordered

Compsci 101.2, Fall Linear search through list o' lists l Maintain list of [string,count] pairs  List of lists, why can't we have list of tuples?  If we read string 'cat', search and update  If we read string 'frog', search and update [ ['dog', 2], ['cat', 1], ['bug', 4], ['ant', 5] ] [ ['dog', 2], ['cat', 2], ['bug', 4], ['ant', 5] ] [ ['dog', 2],['cat', 2],['bug', 4],['ant', 5],['frog',1] ]

Compsci 101.2, Fall See DictionaryTimings.py def linear(words): data = [] for w in words: found = False for elt in data: if elt[0] == w: elt[1] += 1 found = True break if not found: data.append([w,1]) return data N new words?

Compsci 101.2, Fall Binary Search l Before the first guess, there are 1024 numbers How many times can we divide list in half? log 2 (N) for N element list, why? What must be true to use binary search? How is this done in Python?

Compsci 101.2, Fall See DictionaryTimings.py def binary(words): data = [] for w in words: elt = [w,1] index = bisect.bisect_left(data, elt) if index == len(data): data.append(elt) elif data[index][0] != w: data.insert(index,elt) else: data[index][1] += 1 return data

Compsci 101.2, Fall Search via Dictionary l In linear search we looked through all pairs l In binary search we looked at log pairs  But have to shift lots if new element!! l In dictionary search we look at one pair  one billion, 30, 1, for example  Note that 2 10 = 1024, 2 20 = million, 2 30 =billion l Dictionary converts key to number, finds it  Need far more locations than keys  Lots of details to get good performance

Compsci 101.2, Fall See DictionaryTimings.py l Finding value associated with key w:  Takes time independent of number of keys! def dictionary(words): d = {} for w in words: if w not in d: d[w] = 1 else: d[w] += 1 return [[w,d[w]] for w in d]

Compsci 101.2, Fall Running 10 9 instructions/sec This is a real focus in Compsci 201 linear is N 2, binary is N log N, dictionary N NO(log N)O(N)O(N log N)O(N 2 ) min years secs16.7 min11.07 hr31.7 million years

Compsci 101.2, Fall What's the best and worst case? l If every word is the same ….  Does linear differ from dictionary? Why? l Every word is different in alphabetical order  Does binary differ from linear? Why? l When would dictionary be bad?  In practice, never, in theory, kind of the same

Compsci 101.2, Fall Practice Test Question l Read, think, read, think, plan, think, write  If you're not sure, come back to question  We won't ask you to write too much  It's ok to write a lot if you can't write a little