CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding

Slides:



Advertisements
Similar presentations
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
Advertisements

AnagramSolver Based on slides by Ethan Apter
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Chapter 3: Formal Translation Models
1 CSE 143 Lecture 1: ArrayList reading: Welcome to CSE 143! I'm Allison Obourn
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Languages and Grammars MSU CSE 260. Outline Introduction: E xample Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar,
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2; 15.3; 16.5 slides adapted from Marty Stepp
CS Collection and Input/Output Classes CS 3331 Fall 2009.
Building Java Programs Chapter 11 Java Collections Framework.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
CS324e - Elements of Graphics and Visualization Java Intro / Review.
Introduction Syntax: form of a sentence (is it valid) Semantics: meaning of a sentence Valid: the frog writes neatly Invalid: swims quickly mathematics.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
(c) University of Washington14-1 CSC 143 Java Collections.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Chapter 3 Describing Syntax and Semantics
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
CSC 212 – Data Structures Lecture 26: Hash Tables.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
Maps Nick Mouriski.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
CSE 143 Lecture 12: Sets and Maps reading:
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
Collections Dwight Deugo Nesa Matic
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
CSE 143 Lecture 11: Sets and Maps reading:
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Chapter 9 Introduction to Arrays Fundamentals of Java.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Building Java Programs
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
slides created by Marty Stepp and Hélène Martin
Road Map CS Concepts Data Structures Java Language Java Collections
Week 6 Discussion Word Cloud.
CSE 373: Data Structures and Algorithms
Maps Grammars Based on slides by Marty Stepp & Alyssa Harding
CSE 143 Lecture 14 More Recursive Programming; Grammars
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
slides created by Marty Stepp
CSE 1020: The Collection Framework
slides created by Alyssa Harding
slides created by Marty Stepp
slides adapted from Marty Stepp and Hélène Martin
Presentation transcript:

CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding

2 Example: studentGrades Let’s pretend it’s midterm time and all the TAs are tired of grading We decide to randomly generate grades for all our students! // generate random grade between 0 and 99 // so that no one aces the test Random r = new Random(); int grade = r.nextInt(100); …I promise this won’t really happen

3 Example: studentGrades But this gets tiring too We don’t want to hand generate a grade for each student If we have a list of all of our students, we could write a program to loop through them and assign them grades List students = new ArrayList (); students.add("Joe"); students.add("Sally"); students.add("Mike");

4 Example: studentGrades But we need a way to keep track of which grade goes with which student We could keep another list of all the grades –Student at index 0 has grade at index 0, and so on –But that’s tedious JoeSallyMike students grades 012

5 Maps Solution: maps allow us to associate key/value pairs –For example, a student name with a grade Also known as a dictionary, associative array, hash –Can think of it as an array where you can have indexes of any type of Object instead of just int s Joe Sally Mike student names grades Steve

6 Maps Java’s Map interface that uses generic key/value types // adds a mapping from the given key to the given value void put(K key, V value) // returns the value mapped to the given key (null if none) V get(K key) // returns true if the map contains a mapping for the given key boolean containsKey(K key) // returns a Set of all keys in the map Set keySet() // removes any existing mapping for the given key remove(K key)

7 Maps We will use two implementations of the Map interface: TreeMap : –provides O(log(n)) access to elements –stores keys in sorted order HashMap : –provides O(1) access to elements –stores keys in unpredictable order The SortedMap interface is also implemented by TreeMap

8 Example: studentGrades Using this, can solve our problem of grading by making a map: Map studentGrades = new HashMap (); And storing the grades in it: Random r = new Random(); for (String name : students) { int grade = r.nextInt(100); studentGrades.put(name, grade); }

9 Example: studentGrades How can we see the grades? We can get a Set of all the keys –we don’t know anything about a Set –but it’s Iterable so we can use a foreach loop for (String name : studentGrades.keySet() ) { System.out.println(name + " " + studentGrades.get(name)); }

10 Example: wordCount Let’s try a tougher problem now Given some text file, we want to count how many times each word occurs // open the file Scanner console = new Scanner(System.in); System.out.print("What is the name of the text file? "); String fileName = console.nextLine(); Scanner input = new Scanner(new File(fileName));

11 Example: wordCount Make a SortedMap to hold the words and their counts: SortedMap wordCounts = new TreeMap ();

12 Example: wordCount Put the words into the map: while (input.hasNext()) { String next = input.next().toLowerCase(); wordCounts.put(next, 1); } But what if the word is already in the map? This would always keep its count at 1.

13 Example: wordCount Instead, we test whether it was there, and if so, increment it: while (input.hasNext()) { String next = input.next().toLowerCase(); if (!wordCounts.containsKey(next)) { wordCounts.put(next, 1); } else { wordCounts.put(next, wordCounts.get(next) + 1); } Note that each key can only map to one value. When we put a key in multiple times, only the last value is recorded

14 Example: wordCount We can also print out all the word counts: for (String word : wordCounts.keySet()) { int count = wordCounts.get(word); System.out.println(count + "\t" + word); } Note that the keys (the words) occur in sorted order because we are using a SortedMap.

15 Grammars Grammar: A description of a language that describes which sequences of symbols are allowed in that language. Grammars describe syntax (rules), not semantics (meaning) We will use them to produce syntactically correct sentences

16 Grammars Use simplified Backus-Naur Form (BNF) for describing language: : | |... “:” means “is composed of” “|” means “or”

17 Grammars We can describe the basic structure of an English sentence as follows: : "A sentence ( ) is composed of a noun phrase ( ) followed by a verb phrase ( )."

18 Grammars We can break down the further into proper nouns: : :John|Jane|Sally|Spot|Fred|Elmo The vertical bar (“|”) means that the a can be “John” OR “Jane” OR “Sally” OR …

19 Grammars Nonterminals: –,,, and –we don’t expect them to appear in an actual English sentence –they are placeholders on the left side of rules Terminals: –“John”, “Jane”, and “Sally” –they can appear in sentences –they are final productions on the right side of rules

20 Grammars We also need a verb phrase rule, : : | :hit|honored|kissed|helped :died|collapsed|laughed|wept

21 Grammars We can expand the rule so that we can have more complex noun phrases: : | :John|Jane|Sally|Spot|Fred|Elmo :the|a :dog|cat|man|university|father|mother|child

22 Grammars We could just make an rule: :big|fat|green|wonderful|faulty But we want to have multiple adjectives: : | | … We can use recursion to generate any number of adjectives: : |

23 Grammars Similarly, we can add rules for adverbs and prepositional phrases : :quickly|drunkenly|stingily|shamelessly : | : :on|over|inside|by|under|around