Skip to content

Commit 4053822

Browse files
Map and Set
Map and Set by @gabifs
2 parents e4c7754 + 4b8cb19 commit 4053822

File tree

8 files changed

+156
-160
lines changed

8 files changed

+156
-160
lines changed

1-js/05-data-types/07-map-set/01-array-unique-map/_js.view/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
describe("unique", function() {
2-
it("removes non-unique elements", function() {
2+
it("remove elementos não únicos", function () {
33
let strings = ["Hare", "Krishna", "Hare", "Krishna",
44
"Krishna", "Krishna", "Hare", "Hare", ":-O"
55
];
66

77
assert.deepEqual(unique(strings), ["Hare", "Krishna", ":-O"]);
88
});
99

10-
it("does not change the source array", function() {
10+
it("não altera o array de origem", function () {
1111
let strings = ["Krishna", "Krishna", "Hare", "Hare"];
1212
unique(strings);
1313
assert.deepEqual(strings, ["Krishna", "Krishna", "Hare", "Hare"]);

1-js/05-data-types/07-map-set/01-array-unique-map/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ importance: 5
22

33
---
44

5-
# Filter unique array members
5+
# Filtrar elementos únicos de um array
66

7-
Let `arr` be an array.
7+
Seja `arr` um array.
88

9-
Create a function `unique(arr)` that should return an array with unique items of `arr`.
9+
Crie uma função `unique(arr)` que deve retornar um array com itens únicos de `arr`.
1010

11-
For instance:
11+
Por exemplo:
1212

1313
```js
1414
function unique(arr) {
@@ -22,6 +22,6 @@ let values = ["Hare", "Krishna", "Hare", "Krishna",
2222
alert( unique(values) ); // Hare, Krishna, :-O
2323
```
2424

25-
P.S. Here strings are used, but can be values of any type.
25+
Observação: Aqui estão sendo usadas strings, mas os valores podem ser de qualquer tipo.
2626

27-
P.P.S. Use `Set` to store unique values.
27+
Observação adicional: Use `Set` para armazenar valores únicos.

1-js/05-data-types/07-map-set/02-filter-anagrams/_js.view/test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ function intersection(arr1, arr2) {
33
}
44

55
describe("aclean", function() {
6-
7-
it("returns exactly 1 word from each anagram set", function() {
6+
it("retorna exatamente 1 palavra de cada conjunto de anagramas", function () {
87
let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
98

109
let result = aclean(arr);
@@ -16,9 +15,9 @@ describe("aclean", function() {
1615

1716
});
1817

19-
it("is case-insensitive", function() {
18+
it("é insensível a maiúsculas e minúsculas", function () {
2019
let arr = ["era", "EAR"];
2120
assert.equal(aclean(arr).length, 1);
2221
});
2322

24-
});
23+
});

1-js/05-data-types/07-map-set/02-filter-anagrams/solution.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
To find all anagrams, let's split every word to letters and sort them. When letter-sorted, all anagrams are same.
1+
Para encontrar todos os anagramas, vamos dividir cada palavra em letras e ordená-las. Quando ordenadas por letras, todos os anagramas são iguais.
22

3-
For instance:
3+
Por exemplo:
44

55
```
66
nap, pan -> anp
@@ -9,14 +9,14 @@ cheaters, hectares, teachers -> aceehrst
99
...
1010
```
1111

12-
We'll use the letter-sorted variants as map keys to store only one value per each key:
12+
Vamos usar as variantes ordenadas por letras como chaves de mapa para armazenar apenas um valor para cada chave:
1313

1414
```js run
1515
function aclean(arr) {
1616
let map = new Map();
1717

1818
for (let word of arr) {
19-
// split the word by letters, sort them and join back
19+
// Divida a palavra em letras, ordene-as e junte novamente.
2020
*!*
2121
let sorted = word.toLowerCase().split('').sort().join(''); // (*)
2222
*/!*
@@ -31,9 +31,9 @@ let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
3131
alert( aclean(arr) );
3232
```
3333

34-
Letter-sorting is done by the chain of calls in the line `(*)`.
34+
A ordenação por letras é feita pela cadeia de chamadas na linha `(*)`.
3535

36-
For convenience let's split it into multiple lines:
36+
Para maior clareza, vamos dividi-la em várias linhas:
3737

3838
```js
3939
let sorted = word // PAN
@@ -43,21 +43,21 @@ let sorted = word // PAN
4343
.join(''); // anp
4444
```
4545

46-
Two different words `'PAN'` and `'nap'` receive the same letter-sorted form `'anp'`.
46+
Duas palavras diferentes, `'PAN'` e `'nap'`, recebem a mesma forma ordenada por letras, `'anp'`.
4747

48-
The next line put the word into the map:
48+
A próxima linha coloca a palavra no mapa:
4949

5050
```js
5151
map.set(sorted, word);
5252
```
5353

54-
If we ever meet a word the same letter-sorted form again, then it would overwrite the previous value with the same key in the map. So we'll always have at maximum one word per letter-form.
54+
Se encontrarmos novamente uma palavra com a mesma forma ordenada por letras, ela irá sobrescrever o valor anterior com a mesma chave no mapa. Portanto, sempre teremos no máximo uma palavra por forma de letras.
5555

56-
At the end `Array.from(map.values())` takes an iterable over map values (we don't need keys in the result) and returns an array of them.
56+
No final, `Array.from(map.values())` cria um iterável sobre os valores do mapa (não precisamos das chaves no resultado) e retorna um array com eles.
5757

58-
Here we could also use a plain object instead of the `Map`, because keys are strings.
58+
Aqui também poderíamos usar um objeto simples em vez do `Map`, porque as chaves são strings.
5959

60-
That's how the solution can look:
60+
É assim que a solução pode ser implementada:
6161

6262
```js run demo
6363
function aclean(arr) {

1-js/05-data-types/07-map-set/02-filter-anagrams/task.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@ importance: 4
22

33
---
44

5-
# Filter anagrams
5+
# Filtrar anagramas
66

7-
[Anagrams](https://en.wikipedia.org/wiki/Anagram) are words that have the same number of same letters, but in different order.
7+
[Anagramas](https://en.wikipedia.org/wiki/Anagram) são palavras que têm o mesmo número das mesmas letras, mas em ordem diferente.
88

9-
For instance:
9+
Por exemplo:
1010

1111
```
1212
nap - pan
1313
ear - are - era
1414
cheaters - hectares - teachers
1515
```
1616

17-
Write a function `aclean(arr)` that returns an array cleaned from anagrams.
17+
Escreva uma função `aclean(arr)` que retorne um array limpo de anagramas.
1818

19-
For instance:
19+
Por exemplo:
2020

2121
```js
2222
let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
2323

24-
alert( aclean(arr) ); // "nap,teachers,ear" or "PAN,cheaters,era"
24+
alert( aclean(arr) ); // "nap,teachers,ear" ou "PAN,cheaters,era"
2525
```
2626

27-
From every anagram group should remain only one word, no matter which one.
28-
27+
De cada grupo de anagramas, deve permanecer apenas uma palavra, não importa qual.

1-js/05-data-types/07-map-set/03-iterable-keys/solution.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
That's because `map.keys()` returns an iterable, but not an array.
3-
4-
We can convert it into an array using `Array.from`:
2+
Isso ocorre porque `map.keys()` retorna um objeto iterável, mas não um array.
53

4+
Podemos convertê-lo em um array usando `Array.from`:
65

76
```js run
87
let map = new Map();

1-js/05-data-types/07-map-set/03-iterable-keys/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ importance: 5
44

55
# Iterable keys
66

7-
We'd like to get an array of `map.keys()` in a variable and then apply array-specific methods to it, e.g. `.push`.
7+
Gostaríamos de obter um array de `map.keys()` em uma variável e, em seguida, aplicar métodos específicos de array a ele, como `.push`.
88

9-
But that doesn't work:
9+
Mas isso não funciona:
1010

1111
```js run
1212
let map = new Map();
@@ -21,4 +21,4 @@ keys.push("more");
2121
*/!*
2222
```
2323

24-
Why? How can we fix the code to make `keys.push` work?
24+
Por quê? Como podemos corrigir o código para fazer com que `keys.push` funcione?

0 commit comments

Comments
 (0)