Top Banner
Collections, Part Three
117

Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Aug 13, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Collections, Part Three

Page 2: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Lexicon

Page 3: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Lexicon

● A Lexicon is a container that stores a collection of words.

● No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.”

● Contains operations for● Checking whether a word exists.● Checking whether a string is a prefix of a

given word.

Page 4: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Tautonyms

● A tautonym is a word formed by repeating the same string twice.● For example: murmur, couscous, papa, etc.

● What English words are tautonyms?

Page 5: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Some Aa

http://upload.wikimedia.org/wikipedia/commons/f/f1/Aa_large.jpg

Page 6: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

One Bulbul

Page 7: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

More than One Caracara

http://www.greglasley.net/images/CO/Crested-Caracara-F3.jpg

Page 8: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Introducing the Dikdik

Page 9: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

And a Music Recommendation

Page 10: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Set

Page 11: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Set

● The Set represents an unordered collection of distinct elements.

● Elements can be added and removed, and you can check whether or not an element exists.

Page 12: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Set

● The Set represents an unordered collection of distinct elements.

● Elements can be added and removed, and you can check whether or not an element exists.

137

Page 13: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Set

● The Set represents an unordered collection of distinct elements.

● Elements can be added and removed, and you can check whether or not an element exists.

137

42

Page 14: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Set

● The Set represents an unordered collection of distinct elements.

● Elements can be added and removed, and you can check whether or not an element exists.

137

42

2718

Page 15: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Set

● The Set represents an unordered collection of distinct elements.

● Elements can be added and removed, and you can check whether or not an element exists.

42

2718

Page 16: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Operations on Sets

● You can add a value to a set by writing

set += value;

● You can remove a value from a set by writing

set -= value;● You can check if a value exists by writing

set.contains(value)● Many more operations are available (union,

intersection, difference, subset, etc.), so be sure to check the documentation.

Page 17: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map

Page 18: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map

● The Map class represents a set of key/value pairs.

● Each key is associated with a unique value.

● Given a key, can look up the associated value.

Page 19: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map

● The Map class represents a set of key/value pairs.

● Each key is associated with a unique value.

● Given a key, can look up the associated value.

CS106B Awesome!

Page 20: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map

● The Map class represents a set of key/value pairs.

● Each key is associated with a unique value.

● Given a key, can look up the associated value.

CS106B Awesome!

Dikdik Cute!

Page 21: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map

● The Map class represents a set of key/value pairs.

● Each key is associated with a unique value.

● Given a key, can look up the associated value.

CS106B Awesome!

Dikdik Cute!

ThisSlide

SelfReferential

Page 22: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map

● The Map class represents a set of key/value pairs.

● Each key is associated with a unique value.

● Given a key, can look up the associated value.

CS106B Awesome!

Dikdik Very Cute!

ThisSlide

SelfReferential

Page 23: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Using the Map

● You can create a map by writing

Map<KeyType, ValueType> map;

● You can add or change a key/value pair by writing

map[key] = value;

If the key doesn't already exist, it is added.

● You can read the value associated with a key by writing

map[key]

If the key doesn't exist, it is added and associated with a default value.

● You can check whether a key exists by calling

map.containsKey(key)

Page 24: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Page 25: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Page 26: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap

Page 27: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap

Page 28: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap

Page 29: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text

Page 30: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text

Page 31: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text

Page 32: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text

Oh no! I don’tknow what that is!

Page 33: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text

Let’s pretendI already had that

key here.

"Hello"

Page 34: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text

The values areall ints, so I’ll pick

zero.

"Hello" 0

Page 35: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text

Phew! Crisisaverted!

"Hello" 0

Page 36: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text"Hello" 0

Page 37: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text"Hello" 0

Page 38: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text"Hello" 0

Cool as a cucumber.

⊂(▀¯▀⊂)

Page 39: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text"Hello" 1

Cool as a cucumber.

⊂(▀¯▀⊂)

Page 40: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap "Hello"text"Hello" 1

Page 41: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

Page 42: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

Page 43: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text

Page 44: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text

Page 45: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text

Page 46: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text

Oh man, not again!

Page 47: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text

I’ll pretendI already had that

key.

"Goodbye" 0

Page 48: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text"Goodbye" 0

Page 49: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text"Goodbye" 0

Page 50: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text"Goodbye" 0

Chillin’ like a villain.

⊂(▀¯▀⊂)

Page 51: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text"Goodbye" 1

Chillin’ like a villain.

⊂(▀¯▀⊂)

Page 52: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye"text"Goodbye" 1

Page 53: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

Map<string, int> freqMap; while (true) { string text = getLine("Enter some text: "); cout << "Times seen: " << freqMap[text] << endl; freqMap[text]++; }

freqMap"Hello" 1

"Goodbye" 1

Page 54: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Sorting by First Letters

Page 55: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Page 56: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Page 57: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

Page 58: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

Page 59: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"word

Page 60: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"word

Page 61: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"word

Page 62: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"word

Oops, no f’s here.

Page 63: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"wordLet’s insertthat key.

'f'

Page 64: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"wordI’ll give you ablank Lexicon.

'f' { }

Page 65: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"word

'f' { }

Page 66: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter

"first"word

'f' { "first" }

Page 67: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

"first"word

Page 68: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

Page 69: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

Page 70: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

"foremost"word

Page 71: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

"foremost"word

Page 72: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

"foremost"word

Page 73: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

"foremost"wordEasy peasy.

⊂(▀¯▀⊂)

Page 74: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first" }

"foremost"word

Page 75: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

"foremost"word

Page 76: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

"foremost"word

Page 77: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

Page 78: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

Page 79: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

"initial"word

Page 80: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

"initial"word

Page 81: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

"initial"word

'i' { }

Page 82: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

"initial"word

'i' { }

Page 83: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Map Autoinsertion

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

Lexicon english("EnglishWords.dat");

Map<char, Lexicon> wordsByFirstLetter; for (string word: english) { wordsByFirstLetter[word[0]].add(word); }

wordsByFirstLetter'f' { "first", "foremost" }

"initial"word

'i' { "initial" }

Page 84: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Anagrams

● Two words are anagrams of one another if the letters in one can be rearranged into the other.

● Some examples:● “Senator” and “treason.”● “Praising” and “aspiring.”● “Arrogant” and “tarragon.”

● Question for you: does this concept exist in other languages? If so, please send me examples!

Page 85: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Anagrams

● Nifty fact: two words are anagrams if you get the same string when you write the letters in those words in sorted order.

● For example, “praising” and “aspiring” are anagrams because, in both cases, you get the string “aiignprs” if you sort the letters.

Page 86: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Anagram Clusters

● Let’s group all words in English into “clusters” of words that are all anagrams of one another.

● We’ll use a Map<string, Lexicon>.● Each key is a string of letters in sorted order.● Each value is the collection of English words

that have those letters in that order.

Page 87: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Assignment 2 Demo

Page 88: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Assignment 2

● Assignment 2 (Word Play) goes out today. It’s due a week from today at the start of class.● Play around with properties of words and discover some

new things along the way!● Solidify your understanding of container types and

procedural decomposition.

● Start this one early. You’ll want to have some time to let things percolate and to ask for help when you need it.

● You must complete this assignment individually. Working in pairs is not permitted yet.

Page 89: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Assignment 2

● Our illustrious and industrious head TA Anton will be holding an assignment review session (YEAH Hours) tonight from 7PM in room 420-041.

● Highly recommended!

Page 90: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Next Time

● Thinking Recursively● How can you best solve problems using

recursion?● What techniques are necessary to do so?● And what problems yield easily to a

recursive solution?

Page 91: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Extra Content: How to Sort a String

Page 92: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

Page 93: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

Page 94: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

Page 95: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

for (char ch: input) { letterFreq[ch]++;}

Page 96: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

for (char ch: input) { letterFreq[ch]++;}

Page 97: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1

for (char ch: input) { letterFreq[ch]++;}

Page 98: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1

for (char ch: input) { letterFreq[ch]++;}

Page 99: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 1 for (char ch: input) {

letterFreq[ch]++;}

Page 100: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 1 for (char ch: input) {

letterFreq[ch]++;}

Page 101: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 1

n 1

for (char ch: input) { letterFreq[ch]++;}

Page 102: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 1

n 1

for (char ch: input) { letterFreq[ch]++;}

Page 103: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 2

n 1

for (char ch: input) { letterFreq[ch]++;}

Page 104: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 2

n 1

for (char ch: input) { letterFreq[ch]++;}

Page 105: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 2

n 2

for (char ch: input) { letterFreq[ch]++;}

Page 106: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 2

n 2

for (char ch: input) { letterFreq[ch]++;}

Page 107: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2

for (char ch: input) { letterFreq[ch]++;}

Page 108: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2

Page 109: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Order in Range-Based for Loops

● When using the range-based for loop to iterate over a collection:● In a Vector, string, or array, the elements are

retrieved in order.● In a Map, the keys are returned in sorted

order.● In a Set or Lexicon, the values are returned in

sorted order.

Page 110: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2

for (char ch: letterFreq) { for (int i = 0; i < letterFreq[ch]; i++) { result += ch; }}

Page 111: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2

for (char ch: letterFreq) { for (int i = 0; i < letterFreq[ch]; i++) { result += ch; }}

Page 112: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2a a a

for (char ch: letterFreq) { for (int i = 0; i < letterFreq[ch]; i++) { result += ch; }}

Page 113: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2a a a

for (char ch: letterFreq) { for (int i = 0; i < letterFreq[ch]; i++) { result += ch; }}

Page 114: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2a a a b

for (char ch: letterFreq) { for (int i = 0; i < letterFreq[ch]; i++) { result += ch; }}

Page 115: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2a a a b

for (char ch: letterFreq) { for (int i = 0; i < letterFreq[ch]; i++) { result += ch; }}

Page 116: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2a a a b n n

for (char ch: letterFreq) { for (int i = 0; i < letterFreq[ch]; i++) { result += ch; }}

Page 117: Collections, Part Three · collection of words. No definitions are associated with the words; it is a “lexicon” rather than a “dictionary.” Contains operations for Checking

Counting Sort

b a n a n a

letterFreq

b 1a 3

n 2a a a b n n