diff options
| author | Nuno Vieira <nunovieira220@gmail.com> | 2020-12-22 21:09:28 +0000 |
|---|---|---|
| committer | Nuno Vieira <nunovieira220@gmail.com> | 2020-12-22 21:09:28 +0000 |
| commit | 98d03e4ebc62104a96cc7d363f758bb1999f4e1c (patch) | |
| tree | aa9896419f5212c6692562883d2b08a1671dc9e5 | |
| parent | 84d8ec87a29d69909ac4c1a3376b4353d2dcce97 (diff) | |
| download | perlweeklychallenge-club-98d03e4ebc62104a96cc7d363f758bb1999f4e1c.tar.gz perlweeklychallenge-club-98d03e4ebc62104a96cc7d363f758bb1999f4e1c.tar.bz2 perlweeklychallenge-club-98d03e4ebc62104a96cc7d363f758bb1999f4e1c.zip | |
Add nunovieira220 js solution to challenge 092
| -rw-r--r-- | challenge-092/nunovieira220/js/ch-1.js | 23 | ||||
| -rw-r--r-- | challenge-092/nunovieira220/js/ch-2.js | 48 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-092/nunovieira220/js/ch-1.js b/challenge-092/nunovieira220/js/ch-1.js new file mode 100644 index 0000000000..2af6d41f67 --- /dev/null +++ b/challenge-092/nunovieira220/js/ch-1.js @@ -0,0 +1,23 @@ +// Input +const A = 'abc'; +const B = 'xyz'; + +// Isomorphic Strings +const mapping = {}; +let res = 1; + +if (A.length != B.length) return 0; + +for (let i = 0; i < A.length; i++) { + const cA = A.charAt(i); + const cB = B.charAt(i); + + if(!mapping[cA] && !Object.values(mapping).includes(cB)) mapping[cA] = cB; + if (mapping[cA] && mapping[cA] === cB) continue; + + res = 0; + break; +}; + +// Output +console.log(res);
\ No newline at end of file diff --git a/challenge-092/nunovieira220/js/ch-2.js b/challenge-092/nunovieira220/js/ch-2.js new file mode 100644 index 0000000000..925a4cf12e --- /dev/null +++ b/challenge-092/nunovieira220/js/ch-2.js @@ -0,0 +1,48 @@ +// Input +const S = [[1, 2], [3, 7], [8, 10]]; +const N = [5, 8]; + +// Insert Interval + +/// In the beginning +if(N[1] < S[0][0]) { + S.unshift(N); + console.log(S); + process.exit(1); +} + +/// In the end +if(N[0] > S[S.length - 1][1]) { + S.push(N); + console.log(S); + process.exit(1); +} + +/// In the middle +const res = []; +let one = undefined; +let added = false; + +const add = pair => { + res.push(pair); + one = undefined; + return true; +} + +for(let i = 0; i < S.length; i++) { + const fst = S[i][0]; + const snd = S[i][1]; + + if(one === undefined && N[0] < fst && N[1] < snd && !added) added = add([N[0], N[1]]); + if(one === undefined && N[0] < fst && N[1] >= fst) one = N[0]; + if(one === undefined) one = fst; + + if(N[0] <= snd && N[1] > snd) { + if(S[i+1] === undefined || S[i+1][0] > N[1]) added = add([one, N[1]]); + } else { + add([one, snd]); + } +} + +// Output +console.log(res);
\ No newline at end of file |
