Skip to content

Commit 675b6e3

Browse files
committed
adding chapter 7 Dictionaries
1 parent 723e0b6 commit 675b6e3

File tree

3 files changed

+164
-3
lines changed

3 files changed

+164
-3
lines changed

07-chapter-Dictionaries.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
class Dictionary {
2+
constructor(dataStore = []) {
3+
this.dataStore = dataStore;
4+
}
5+
6+
add(key, value) {
7+
this.dataStore[key] = value;
8+
}
9+
10+
find(key) {
11+
return this.dataStore[key];
12+
}
13+
14+
remove(key) {
15+
delete this.dataStore[key];
16+
}
17+
18+
showOne() {
19+
if(this.count() > 0) {
20+
const key = Object.keys(this.dataStore)[0];
21+
console.log(key, ' -> ', this.dataStore[key]);
22+
}
23+
}
24+
25+
showAll() {
26+
// Display in ascending order
27+
Object.keys(this.dataStore).forEach(key => {
28+
console.log(key, ' -> ', this.dataStore[key]);
29+
});
30+
}
31+
32+
showAllSorted() {
33+
// Display in ascending order
34+
Object.keys(this.dataStore).sort().forEach(key => {
35+
console.log(key, ' -> ', this.dataStore[key]);
36+
});
37+
}
38+
39+
count() {
40+
// or this.dataStore.length since dataStore is an array, but this aproach is only for arrays
41+
// Object.keys(obj).length returns how many elements has the Dictionary
42+
return Object.keys(this.dataStore).length;
43+
}
44+
45+
clear() {
46+
this.dataStore = [];
47+
}
48+
49+
}
50+
51+
// Implementation
52+
// #####################################
53+
console.log('CHAPTER 7');
54+
const pbook = new Dictionary();
55+
pbook.add("Raymond","123");
56+
pbook.add("David", "345");
57+
pbook.add("Cynthia", "456");
58+
pbook.add("Mike", "723");
59+
pbook.add("Jennifer", "987");
60+
pbook.add("Danny", "012");
61+
pbook.add("Jonathan", "666");
62+
console.log("Number of entries: " + pbook.count());
63+
console.log("David's extension: " + pbook.find("David"));
64+
pbook.showAll();
65+
console.log('\n Cleat Dictionary');
66+
pbook.clear();
67+
console.log("Number of entries: " + pbook.count());
68+
69+
// #################################################
70+
/*
71+
1. Write a program that takes a set of names and phone numbers from a text file
72+
and stores them in a Dictionary object. Include in your program the ability to
73+
display one phone number, display all phone numbers, add new phone numbers,
74+
remove phone numbers, and clear out the list of numbers.
75+
*/
76+
console.log('\n\n### Exercise 1');
77+
const readline = require('readline');
78+
const fs = require('fs');
79+
80+
const dictionary = new Dictionary();
81+
82+
const rl = readline.createInterface({
83+
input: fs.createReadStream('phoneDictionary.txt')
84+
});
85+
86+
rl.on('line', (line) => {
87+
const split = line.split(' ');
88+
dictionary.add(split[0], split[1]);
89+
});
90+
91+
rl.on('close', () => {
92+
console.log('\nDisplay 1 phone number');
93+
dictionary.showOne();
94+
95+
console.log('\nDisplay All');
96+
dictionary.showAll();
97+
98+
dictionary.add('Cristian', '111-111-111-111');
99+
dictionary.add('Kalesei', '222-111-222-111');
100+
dictionary.add('Kal', '111-333-111-333');
101+
console.log('\nDisplay All');
102+
dictionary.showAll();
103+
104+
console.log('\nRemoving elements');
105+
dictionary.remove('Mike');
106+
dictionary.remove('Raymond');
107+
dictionary.remove('Kal');
108+
109+
console.log('\nDisplay All');
110+
dictionary.showAll();
111+
console.log(dictionary.find('Jane'));
112+
113+
/*
114+
2. Using the Dictionary class, write a program that stores the number of occurrences
115+
of words in a text. Your program should display each word in a text just once as well
116+
as the number of times the word occurs in the text.
117+
*/
118+
console.log('\n\n### Exercise 2');
119+
const d = new Dictionary();
120+
let string = 'the brown fox jumped over the blue fox';
121+
122+
string = string.split(' ');
123+
string.forEach(item => {
124+
if(item !== ' ') {
125+
let w = d.find(item);
126+
if(w !== undefined) {
127+
const value = w + 1;
128+
d.remove(item);
129+
d.add(item, value);
130+
} else {
131+
d.add(item, 1);
132+
}
133+
}
134+
});
135+
console.log('showing unsorted');
136+
d.showAll();
137+
138+
/*
139+
3. Rewrite exercise 2 so that it displays the words in sorted order.
140+
*/
141+
console.log('\n\n### Exercise 3');
142+
console.log('showing sorted');
143+
d.showAllSorted();
144+
});

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ The purpose of this repo is to update the exercises to ES6 standards and to corr
1515
| 2.- | [Lists](./03-chapter-List.js) | 5 | A `List` is an ordered sequence of data, where elements are not meant to be ordered.
1616
| 3.- | [Stacks](./04-chapter-Stacks.js) | 3 | A `Stack` is an example of Last-in, First-Out (LIFO)
1717
| 4.- | [Queues](./05-chapter-Queues.js) | 3 | A `Queue` is an example of First-in, First-Out (FIFO)
18-
| 5.- | [Linked List](./06-chapter-1-Linked-List.js) | 4 | A linked list is a collection of objects called nodes. Each node is linked to a successor node in the list using an object reference.
19-
| 6.- | [Double Linked List](./06-chapter-2-Double-Linked-List.js) | 4 | Traversing a Double linked list are more efficient, since we no longer have to search for the previous node.
20-
| 7.- | [Circular Linked List](./) | 4 | The reason you might want to create a circularly linked list is if you want the ability to go backward through a list but don’t want the extra overhead of creating a doubly linked list.
18+
| 5.- | [Linked List](./06-chapter-1-Linked-List.js) | 4 | A `Linked list` is a collection of objects called nodes. Each node is linked to a successor node in the list using an object reference.
19+
| 6.- | [Double Linked List](./06-chapter-2-Double-Linked-List.js) | 4 | Traversing a `Double linked list` are more efficient, since we no longer have to search for the previous node.
20+
| 7.- | [Circular Linked List](./06-chapter-3-Circular-Linked-List.js) | 4 | The reason you might want to create a `Circular linked list` is if you want the ability to go backward through a list but don’t want the extra overhead of creating a doubly linked list.
21+
| 8.- | [Dictionaries](./07-chapter-Dictionaries.js) | 3 | A `Dictionary` is a data structure that stores data as key-value pairs
22+
23+
24+
25+
### To run the examples we need the following:
26+
27+
- NodeJS Install
28+
- Open Any Terminal and position to the folder where the files are located
29+
- execute the command: `$ node [file-name.js]`

phoneDictionary.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Jane 202-555-0161
2+
Raymond 202-555-0122
3+
David 202-555-0147
4+
Cynthia 202-555-0165
5+
Mike 202-555-0116
6+
Jennifer 202-555-0196
7+
Danny 202-555-0165
8+
Jonathan 202-555-0122

0 commit comments

Comments
 (0)