Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Map and Set#157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Map and Set#157
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletions1-js/05-data-types/07-map-set/01-array-unique-map/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
functionunikát(pole) { | ||
return Array.from(new Set(pole)); | ||
} |
18 changes: 9 additions & 9 deletions1-js/05-data-types/07-map-set/01-array-unique-map/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
describe("unikát", function() { | ||
it("odstraní neunikátní prvky", function() { | ||
letřetězce = ["Haré", "Kršna", "Haré", "Kršna", | ||
"Kršna", "Kršna", "Haré", "Haré", ":-O" | ||
]; | ||
assert.deepEqual(unikát(řetězce), ["Haré", "Kršna", ":-O"]); | ||
}); | ||
it("nemění zdrojové pole", function() { | ||
letřetězce = ["Kršna", "Kršna", "Haré", "Haré"]; | ||
unikát(řetězce); | ||
assert.deepEqual(řetězce, ["Kršna", "Kršna", "Haré", "Haré"]); | ||
}); | ||
}); |
22 changes: 11 additions & 11 deletions1-js/05-data-types/07-map-set/01-array-unique-map/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions1-js/05-data-types/07-map-set/02-filter-anagrams/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
functionodstraňAnagramy(pole) { | ||
letmapa = new Map(); | ||
for(letslovo ofpole) { | ||
letseřazené =slovo.toLowerCase().split("").sort().join(""); | ||
mapa.set(seřazené, slovo); | ||
} | ||
return Array.from(mapa.values()); | ||
} |
26 changes: 13 additions & 13 deletions1-js/05-data-types/07-map-set/02-filter-anagrams/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
functionprůnik(arr1, arr2) { | ||
return arr1.filter(prvek => arr2.includes(prvek)); | ||
} | ||
describe("odstraňAnagramy", function() { | ||
it("z každé sady anagramů vrátí právě 1 slovo", function() { | ||
letpole = ["rak", "reklama", "makrela", "KRA", "kostel", "stolek", "karamel"]; | ||
letvýsledek =odstraňAnagramy(pole); | ||
assert.equal(výsledek.length, 3); | ||
assert.equal(intersection(výsledek, ["rak", "KRA"]).length, 1); | ||
assert.equal(intersection(výsledek, ["reklama", "makrela", "karamel"]).length, 1); | ||
assert.equal(intersection(výsledek, ["kostel", "stolek"]).length, 1); | ||
}); | ||
it("nerozlišuje malá a velká písmena", function() { | ||
letpole = ["rak", "KRA"]; | ||
assert.equal(odstraňAnagramy(pole).length, 1); | ||
}); | ||
}); |
70 changes: 35 additions & 35 deletions1-js/05-data-types/07-map-set/02-filter-anagrams/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,77 @@ | ||
Pro nalezení anagramů rozdělíme každé slovo na písmena a ta seřadíme podle abecedy. Po seřazení písmen budou všechny anagramy stejné. | ||
Příklad: | ||
``` | ||
rak, kra ->akr | ||
kostel, stolek->eklost | ||
reklama, makrela, karamel ->aaeklmr | ||
... | ||
``` | ||
Varianty slov se seřazenými písmeny použijeme jako klíče mapy, abychom uložili pro každý klíč jen jednu hodnotu: | ||
```js run | ||
functionodstraňAnagramy(pole) { | ||
letmapa = new Map(); | ||
for (letslovo ofpole) { | ||
//rozdělíme slovo na písmena, seřadíme je a znovu spojíme | ||
*!* | ||
letseřazené =slovo.toLowerCase().split('').sort().join(''); // (*) | ||
*/!* | ||
mapa.set(seřazené, slovo); | ||
} | ||
return Array.from(mapa.values()); | ||
} | ||
letpole = ["rak", "reklama", "makrela", "KRA", "kostel", "stolek", "karamel"]; | ||
alert(odstraňAnagramy(pole) ); | ||
``` | ||
Seřazení písmen se děje ve zřetězeném volání na řádku `(*)`. | ||
Pro přehlednost jej rozdělme na několik řádků: | ||
```js | ||
letseřazené =slovo //KRA | ||
.toLowerCase() //kra | ||
.split('') // ['k','r','a'] | ||
.sort() // ['a','k','r'] | ||
.join(''); //akr | ||
``` | ||
Dvě různá slova `'KRA'`a `'rak'`budou seřazena stejně na `'akr'`. | ||
Další řádek vloží slovo do mapy: | ||
```js | ||
mapa.set(seřazené, slovo); | ||
``` | ||
Jestliže příště přijde slovo se stejným seřazením písmen, přepíše v mapě předchozí hodnotu se stejným klíčem. Vždy tedy budeme mít pro každou seřazenou skupinu písmen nejvýše jedno slovo. | ||
Nakonec`Array.from(mapa.values())`vezme iterovatelný objekt nad hodnotami mapy (klíče ve výsledku nepotřebujeme), vytvoří z těchto hodnot pole a vrátí je. | ||
Zde bychom mohli místo `Map` použít i planý objekt, neboť klíče jsou řetězce. | ||
Řešení by pak mohlo vypadat následovně: | ||
```js run demo | ||
functionodstraňAnagramy(pole) { | ||
let obj = {}; | ||
for (let i = 0; i <pole.length; i++) { | ||
letseřazené =pole[i].toLowerCase().split("").sort().join(""); | ||
obj[seřazené] =pole[i]; | ||
} | ||
return Object.values(obj); | ||
} | ||
letpole = ["rak", "reklama", "makrela", "KRA", "kostel", "stolek", "karamel"]; | ||
alert(odstraňAnagramy(pole) ); | ||
``` |
23 changes: 11 additions & 12 deletions1-js/05-data-types/07-map-set/02-filter-anagrams/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions1-js/05-data-types/07-map-set/03-iterable-keys/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
Je to proto, že `mapa.keys()`vrací iterovatelný objekt, ale ne pole. | ||
Můžeme jej převést na pole pomocí `Array.from`: | ||
```js run | ||
letmapa = new Map(); | ||
mapa.set("jméno", "Jan"); | ||
*!* | ||
letklíče = Array.from(mapa.keys()); | ||
*/!* | ||
klíče.push("další"); | ||
alert(klíče); //jméno,další | ||
``` |
18 changes: 9 additions & 9 deletions1-js/05-data-types/07-map-set/03-iterable-keys/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.