-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-dictionary.ts
More file actions
125 lines (93 loc) · 3.21 KB
/
Copy pathcreate-dictionary.ts
File metadata and controls
125 lines (93 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//////////////////////////////////////////////////
// 1. Choose a data structure for your dictionary
//////////////////////////////////////////////////
type Entry = string;
type EntryLength = number;
type Definition = string;
type Dictionary = Record<Entry, Definition>;
type GroupedDictionaryEntriesByLength = Record<EntryLength, Entry[]>;
const dictionary: Dictionary = {};
//////////////////////////////////////////////////
// 2. Implement the addEntry function to store
// items in the dictionary.
//////////////////////////////////////////////////
const addEntry = (entry: Entry, definition: Definition): void => {
if (!entry || !definition) {
return;
}
dictionary[entry] = definition;
};
addEntry('boat', 'goes on the sea');
addEntry('cat', 'cute animal');
addEntry('Bruno', 'person name');
addEntry('pen', 'thing to write');
addEntry('bike', 'two-wheel vehicle');
addEntry('car', 'four-wheel vehicle');
console.log(dictionary);
//////////////////////////////////////////////////
// 3. Implement a new function getEntry to return
// and entry given a new word
// It should return both the word and
// definition.
//////////////////////////////////////////////////
const getEntry = (entry: Entry): [Entry, Definition] | undefined => {
if (!entry) {
return;
}
const definition = dictionary[entry];
if (!definition) {
console.error(`Entry "${entry}" not found in the dictionary.`);
return;
}
return [entry, definition];
};
console.log(getEntry('boat'));
//////////////////////////////////////////////////
// 4. Implement a new function startsWith to
// return a new dictionary containing all the
// entries with the word starting with that
// string. It must be case insensitive.
//////////////////////////////////////////////////
const startsWith = (str: string): Dictionary => {
if (!str) {
return {};
}
const dictionaryEntriesStartsWithStr: Dictionary = {};
const entries = Object.keys(dictionary);
for (let e = 0; e < entries.length; e++) {
const entry = entries[e];
const entryStartWithStr = entry.toLowerCase().startsWith(str.toLowerCase());
if (entryStartWithStr) {
dictionaryEntriesStartsWithStr[entry] = dictionary[entry];
}
}
return dictionaryEntriesStartsWithStr;
};
console.log(startsWith('b'));
//////////////////////////////////////////////////
// 5. Implement a new function groupByLength
// to return the words of the dictionary
// grouped by the length.
//
// i.e.:
//
// {
// "3": ["cat", "pen", "car"],
// "4": ["boat", "bike"],
// "5": ["Bruno"],
// }
//////////////////////////////////////////////////
const groupByLength = (): GroupedDictionaryEntriesByLength => {
const groupedDictionaryEntriesByLength: GroupedDictionaryEntriesByLength = {};
for (const entry in dictionary) {
const length = entry.length;
const lengthIsAlreadyInGroupedDictionaryEntriesByLength = groupedDictionaryEntriesByLength[length];
if (!lengthIsAlreadyInGroupedDictionaryEntriesByLength) {
groupedDictionaryEntriesByLength[length] = [entry];
continue;
}
groupedDictionaryEntriesByLength[length].push(entry);
}
return groupedDictionaryEntriesByLength;
};
console.log(groupByLength());