Skip to content

Commit 8c9bafb

Browse files
authored
Merge pull request mouredev#3288 from hozlucas28/Solution-18-TypeScript
#18 - TypeScript
2 parents 6833dc1 + e0a1637 commit 8c9bafb

File tree

1 file changed

+260
-0
lines changed

1 file changed

+260
-0
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/*
2+
Data methods...
3+
*/
4+
5+
console.log('Data methods...')
6+
7+
class Data<T extends string> {
8+
private savedData: T[]
9+
10+
constructor(initialData: T[]) {
11+
this.savedData = this.sanitizeData(initialData)
12+
}
13+
14+
public getData(): T[] {
15+
return this.savedData
16+
}
17+
18+
public setData(newInitialData: T[]): this {
19+
this.savedData = this.sanitizeData(newInitialData)
20+
return this
21+
}
22+
23+
private sanitizeData(data: T[]): T[] {
24+
const uniqueData: Set<T> = new Set(data)
25+
const uniqueDataValues: IterableIterator<T> = uniqueData.values()
26+
27+
const sanitizedData: T[] = []
28+
for (let i = 0; i < uniqueData.size; i++) {
29+
sanitizedData.push(uniqueDataValues.next().value)
30+
}
31+
32+
return sanitizedData
33+
}
34+
35+
public appendEnd(newData: T[]): this {
36+
this.savedData.push(...newData)
37+
return this
38+
}
39+
40+
public appendStart(newData: T[]): this {
41+
this.savedData.unshift(...newData)
42+
return this
43+
}
44+
45+
public appendAt(index: number, newData: T | T[]): this {
46+
const head: T[] = this.savedData.slice(0, index)
47+
const tail: T[] = this.savedData.slice(index)
48+
49+
const newSavedData: T[] = [...head]
50+
51+
typeof newData === 'string'
52+
? newSavedData.push(newData)
53+
: newSavedData.push(...newData)
54+
55+
newSavedData.push(...tail)
56+
57+
this.setData(newSavedData)
58+
return this
59+
}
60+
61+
public clear(): this {
62+
this.setData([])
63+
return this
64+
}
65+
66+
public deleteAt(index: number): this {
67+
const head: T[] = this.savedData.slice(0, index)
68+
const tail: T[] = this.savedData.slice(index + 1)
69+
70+
const newSavedData: T[] = [...head, ...tail]
71+
72+
this.setData(newSavedData)
73+
return this
74+
}
75+
76+
public has(data: T): boolean {
77+
return this.savedData.some((value) => value === data)
78+
}
79+
80+
public updateAt(index: number, newValue: T): this {
81+
this.savedData[index] = newValue
82+
return this
83+
}
84+
}
85+
86+
const data: Data<string> = new Data([
87+
'Buenos Aires',
88+
'Texas',
89+
'Madrid',
90+
'Houston',
91+
'Buenos Aires',
92+
'California',
93+
'Texas',
94+
])
95+
96+
console.log(
97+
`\nconst data: Data<string> = new Data(['Buenos Aires', 'Texas', 'Madrid', 'Houston', 'Buenos Aires', 'California', 'Texas'])`
98+
)
99+
100+
console.table(data.getData())
101+
102+
console.log('\nAppend data at the end...')
103+
console.log(`\ndata.appendEnd(['Berlin'])`)
104+
105+
data.appendEnd(['Berlin'])
106+
console.table(data.getData())
107+
108+
console.log('\nAppend data at the start...')
109+
console.log(`\ndata.appendStart(['Chaco'])`)
110+
111+
data.appendStart(['Chaco'])
112+
console.table(data.getData())
113+
114+
console.log('\nAppend several data at the end...')
115+
console.log(`\ndata.appendEnd(['Paris', 'Montana'])`)
116+
117+
data.appendEnd(['Paris', 'Montana'])
118+
console.table(data.getData())
119+
120+
console.log('\nAppend several data at index 4...')
121+
console.log(`\ndata.appendAt(4, ['Jujuy', 'Formosa'])`)
122+
123+
data.appendAt(4, ['Jujuy', 'Formosa'])
124+
console.table(data.getData())
125+
126+
console.log('\nDelete data at index 3...')
127+
console.log(`\ndata.deleteAt(3)`)
128+
129+
data.deleteAt(3)
130+
console.table(data.getData())
131+
132+
console.log('\nUpdate data at index 6...')
133+
console.log(`\ndata.updateAt(6, 'Miami')`)
134+
135+
data.updateAt(6, 'Miami')
136+
console.table(data.getData())
137+
138+
console.log('\nCheck if a data is present...')
139+
console.log(`\ndata.has('Buenos Aires') => ${data.has('Buenos Aires')}`)
140+
141+
console.table(data.getData())
142+
143+
console.log('\nClear saved data...')
144+
console.log(`\ndata.clear()`)
145+
146+
data.clear()
147+
console.table(data.getData())
148+
149+
console.log(
150+
'\n# ---------------------------------------------------------------------------------- #\n'
151+
)
152+
153+
/*
154+
Additional challenge...
155+
*/
156+
157+
console.log('Additional challenge...')
158+
159+
function differenceSets<T>(firstSet: Set<T>, secondSet: Set<T>): Set<T> {
160+
const differenceSets: Set<T> = new Set()
161+
162+
const firstSetValues: IterableIterator<T> = firstSet.values()
163+
164+
for (let i = 0; i < firstSet.size; i++) {
165+
const firstSetValue = firstSetValues.next().value
166+
if (!secondSet.has(firstSetValue)) differenceSets.add(firstSetValue)
167+
}
168+
return differenceSets
169+
}
170+
171+
function intersectionSets<T>(firstSet: Set<T>, secondSet: Set<T>): Set<T> {
172+
const intersectedSets: Set<T> = new Set()
173+
174+
const [shortestSet, longestSet]: [Set<T>, Set<T>] =
175+
firstSet.size < secondSet.size
176+
? [firstSet, secondSet]
177+
: [secondSet, firstSet]
178+
179+
const shortestSetValues: IterableIterator<T> = shortestSet.values()
180+
181+
for (let i = 0; i < shortestSet.size; i++) {
182+
const shortestSetValue = shortestSetValues.next().value
183+
if (longestSet.has(shortestSetValue)) intersectedSets.add(shortestSetValue)
184+
}
185+
186+
return intersectedSets
187+
}
188+
189+
function joinSets<T>(firstSet: Set<T>, secondSet: Set<T>): Set<T> {
190+
const joinedSets: Set<T> = new Set()
191+
192+
const [shortestSet, longestSet]: [Set<T>, Set<T>] =
193+
firstSet.size < secondSet.size
194+
? [firstSet, secondSet]
195+
: [secondSet, firstSet]
196+
197+
const shortestSetValues: IterableIterator<T> = shortestSet.values()
198+
const longestSetValues: IterableIterator<T> = longestSet.values()
199+
200+
for (let i = 0; i < longestSet.size; i++) {
201+
if (i < shortestSet.size) joinedSets.add(shortestSetValues.next().value)
202+
joinedSets.add(longestSetValues.next().value)
203+
}
204+
205+
return joinedSets
206+
}
207+
208+
function symmetricDifferenceSets<T>(
209+
firstSet: Set<T>,
210+
secondSet: Set<T>
211+
): Set<T> {
212+
const symmetricDifferenceSets: Set<T> = new Set()
213+
214+
const headDifference = differenceSets(firstSet, secondSet)
215+
const tailDifference = differenceSets(secondSet, firstSet)
216+
217+
const headDifferenceValues = headDifference.values()
218+
const tailDifferenceValues = tailDifference.values()
219+
220+
for (let i = 0; i < headDifference.size + tailDifference.size; i++) {
221+
const headDifferenceValuesObj = headDifferenceValues.next()
222+
const tailDifferenceValuesObj = tailDifferenceValues.next()
223+
224+
if (!headDifferenceValuesObj.done)
225+
symmetricDifferenceSets.add(headDifferenceValuesObj.value)
226+
227+
if (!tailDifferenceValuesObj.done)
228+
symmetricDifferenceSets.add(tailDifferenceValuesObj.value)
229+
}
230+
231+
return symmetricDifferenceSets
232+
}
233+
234+
const firstSet: Set<string> = new Set(['Hello', 'TypeScript', 'World!'])
235+
const secondSet: Set<string> = new Set(['By', 'TypeScript'])
236+
237+
console.log('\nfirstSet =', firstSet)
238+
console.log('\nsecondSet =', secondSet)
239+
240+
const intersectedSets: Set<string> = intersectionSets(firstSet, secondSet)
241+
242+
console.log('\nIntersection (firstSet, and secondSet) =>', intersectedSets)
243+
244+
const joinedSets: Set<string> = joinSets(firstSet, secondSet)
245+
246+
console.log('\nJoin (firstSet, and secondSet) =>', joinedSets)
247+
248+
const differenceSetsRtn: Set<string> = differenceSets(firstSet, secondSet)
249+
250+
console.log('\nDifference (firstSet, and secondSet) =>', differenceSetsRtn)
251+
252+
const symmetricDifferenceSetsRtn: Set<string> = symmetricDifferenceSets(
253+
firstSet,
254+
secondSet
255+
)
256+
257+
console.log(
258+
'\nSymmetric difference (firstSet, and secondSet) =>',
259+
symmetricDifferenceSetsRtn
260+
)

0 commit comments

Comments
 (0)