6.1 KiB
6.1 KiB
id, title, localeTitle, challengeType
id | title | localeTitle | challengeType |
---|---|---|---|
a3f503de51cf954ede28891d | Find the Symmetric Difference | Encuentra la diferencia simétrica | 5 |
Description
△
o ⊕
) de las matrices proporcionadas.
Dado dos conjuntos (por ejemplo, conjunto A = {1, 2, 3}
y conjunto B = {2, 3, 4}
), el término matemático "diferencia simétrica" es el conjunto de elementos que se encuentran en cualquiera de los dos conjuntos, pero no en ambos ( A △ B = C = {1, 4}
). Por cada diferencia simétrica adicional que tome (por ejemplo, en un conjunto D = {2, 3}
), debe obtener el conjunto con elementos que están en cualquiera de los dos conjuntos pero no en ambos ( C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}
). La matriz resultante debe contener solo valores únicos ( no duplicados ).
Recuerda usar Read-Search-Ask si te atascas. Trate de emparejar el programa. Escribe tu propio código.
Instructions
Tests
tests:
- text: ' <code>sym([1, 2, 3], [5, 2, 1, 4])</code> debe devolver <code>[3, 4, 5]</code> .'
testString: 'assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5], "<code>sym([1, 2, 3], [5, 2, 1, 4])</code> should return <code>[3, 4, 5]</code>.");'
- text: ' <code>sym([1, 2, 3], [5, 2, 1, 4])</code> debe contener solo tres elementos.'
testString: 'assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3, "<code>sym([1, 2, 3], [5, 2, 1, 4])</code> should contain only three elements.");'
- text: ' <code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code> debe devolver <code>[3, 4, 5]</code> .'
testString: 'assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5], "<code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code> should return <code>[3, 4, 5]</code>.");'
- text: ' <code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code> debe contener solo tres elementos.'
testString: 'assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3, "<code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code> should contain only three elements.");'
- text: ' <code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code> debe devolver <code>[3, 4, 5]</code> .'
testString: 'assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5], "<code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code> should return <code>[3, 4, 5]</code>.");'
- text: ' <code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code> debe contener solo tres elementos.'
testString: 'assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3, "<code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code> should contain only three elements.");'
- text: ' <code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code> debe devolver <code>[1, 4, 5]</code> '
testString: 'assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5], "<code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code> should return <code>[1, 4, 5]</code>");'
- text: ' <code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code> debe contener solo tres elementos.'
testString: 'assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3, "<code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code> should contain only three elements.");'
- text: ' <code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code> debe devolver <code>[1, 4, 5]</code> .'
testString: 'assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], "<code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code> should return <code>[1, 4, 5]</code>.");'
- text: ' <code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code> debe contener solo tres elementos.'
testString: 'assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3, "<code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code> should contain only three elements.");'
- text: ' <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> debe devolver <code>[2, 3, 4, 6, 7]</code> .
testString: 'assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7], "<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> should return <code>[2, 3, 4, 6, 7]</code>.");'
- text: ' <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> debe contener solo cinco elementos.'
testString: 'assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5, "<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> should contain only five elements.");'
- text: ' <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code> debe devolver <code>[1, 2, 4, 5, 6, 7, 8, 9]</code> . '
testString: 'assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9], "<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code> should return <code>[1, 2, 4, 5, 6, 7, 8, 9]</code>.");'
- text: ' <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code> debe contener solo ocho elementos '.
testString: 'assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8, "<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code> should contain only eight elements.");'
Challenge Seed
function sym(args) {
return args;
}
sym([1, 2, 3], [5, 2, 1, 4]);
Solution
function sym() {
var arrays = [].slice.call(arguments);
return arrays.reduce(function (symDiff, arr) {
return symDiff.concat(arr).filter(function (val, idx, theArr) {
return theArr.indexOf(val) === idx
&& (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);
});
});
}
sym([1, 2, 3], [5, 2, 1, 4]);