Last active
February 26, 2018 16:57
-
-
Save pr00thmatic/5ef363c2c431ed4a4e77c5828fe154a6 to your computer and use it in GitHub Desktop.
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
| module.exports = (() => { | |
| // each word must be a string | |
| function isClosedInSum (code, base = 2) { | |
| var i, j; | |
| var map = {}; | |
| var nonClosedCouples = []; | |
| var missingElements = []; | |
| var missingMap = {}; | |
| for (i=0; i<code.length; i++) { | |
| code[i] = code[i] + ''; | |
| map[code[i]] = true; | |
| } | |
| for (i=0; i<code.length; i++) { | |
| for (j=i; j<code.length; j++) { | |
| if (!map[modSum(code[i], code[j], base)]) { | |
| nonClosedCouples.push([code[i], code[j]]); | |
| missingMap[modSum(code[i], code[j], base)] = true; | |
| } | |
| } | |
| } | |
| return { | |
| nonClosedCouples, | |
| missing: Object.keys(missingMap) | |
| }; | |
| } | |
| // a and b are strings | |
| function modSum (as, bs, base = 2) { | |
| as = as + ''; bs = bs + ''; | |
| var i; | |
| var sum = as.length > bs.length? as.split(''): bs.split(''); | |
| for (i=0; i<Math.min(as.length, bs.length); i++) { | |
| sum[i] = (eval(as[i]) + eval(bs[i])) % base; | |
| } | |
| return sum.join(''); | |
| } | |
| examples = { | |
| 0: { | |
| code: [ | |
| '0000000', | |
| '0011101', | |
| '0111010', | |
| '1110101', | |
| '1101001', | |
| '1010011', | |
| '0100111', | |
| '1001110' | |
| ], | |
| base: 2 | |
| }, 1: { | |
| code: [ | |
| '00000', | |
| '11001', | |
| '22002', | |
| '20210', | |
| '01211', | |
| '12212', | |
| '10120', | |
| '21121', | |
| '02122' | |
| ], | |
| base: 3 | |
| } | |
| }; | |
| return { | |
| isClosedInSum, | |
| modSum, | |
| examples | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment