Created
October 11, 2022 19:47
-
-
Save irradev/81d7ae0821b95e85b2ae1717a8168bf9 to your computer and use it in GitHub Desktop.
Exercise 25 advent.js
This file contains 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
import { describe, expect, it } from 'vitest' | |
const canReconfigure = (from, to) => { | |
if (typeof from !== 'string') throw new Error('from is not a string') | |
if (typeof to !== 'string') throw new Error('from is not a string') | |
const hasSameLength = from.length === to.length | |
if (!hasSameLength) return false | |
const fromLetters = new Set(from) | |
const toLetters = new Set(to) | |
if (fromLetters === toLetters) return false | |
const hasSameUniqueLetters = fromLetters.size === toLetters.size | |
if (!hasSameUniqueLetters) return false | |
let correctTransformation = true | |
// const transformations = {} | |
// for (let i = 0; i < from.length; i++) { | |
// const fromLetter = from[i] | |
// const toLetter = to[i] | |
// const storedLetter = transformations[fromLetter] | |
// if (storedLetter && storedLetter !== toLetter) { | |
// correctTransformation = false | |
// } | |
// transformations[fromLetter] = toLetter | |
// } | |
const fromLettersArray = Array.from(fromLetters) | |
const toLettersArray = Array.from(toLetters) | |
let beforeLetterFrom = '' | |
let beforeLetterTo = '' | |
const beforeLettersListFrom = [] | |
const beforeLettersListTo = [] | |
for (let i = 0; i < fromLetters.size; i++) { | |
if (!beforeLetterFrom && !beforeLetterTo) { | |
beforeLetterFrom = fromLettersArray[i] | |
beforeLetterTo = toLettersArray[i] | |
continue | |
} | |
beforeLettersListFrom.push(beforeLetterFrom + fromLettersArray[i]) | |
beforeLettersListTo.push(beforeLetterTo + toLettersArray[i]) | |
beforeLettersListFrom.forEach((letters) => { | |
if (beforeLettersListTo.includes(letters)) { | |
correctTransformation = false | |
i = fromLetters.size + 1 | |
} | |
}) | |
beforeLetterFrom = fromLettersArray[i] | |
beforeLetterTo = toLettersArray[i] | |
} | |
return correctTransformation | |
} | |
describe('canReconfigure', () => { | |
// it('should be a function ', () => { | |
// expect(canReconfigure).toBeTypeOf('function') | |
// }) | |
it('should throw if first parameter is missing', () => { | |
expect(() => canReconfigure()).toThrow() | |
}) | |
it('should throw if first parameter is not a string', () => { | |
expect(() => canReconfigure(2)).toThrow() | |
}) | |
it('should throw if second parameter is not a string', () => { | |
expect(() => canReconfigure('a')).toThrow() | |
}) | |
it('should return a boolean', () => { | |
expect(canReconfigure('a', 'b')).toBeTypeOf('boolean') | |
}) | |
it('should return false if strings provided have different length', () => { | |
expect(canReconfigure('abc', 'abcde')).toBe(false) | |
}) | |
it('should return false if strings provided have different number of unique letters', () => { | |
expect(canReconfigure('abc', 'abb')).toBe(false) | |
}) | |
it('should return false if strings has same order of transformation', () => { | |
expect(canReconfigure('XBOX', 'XXBO')).toBe(false) | |
}) | |
it('should return true if strings has different order of transformation', () => { | |
expect(canReconfigure('REGALO', 'RILAGE')).toBe(true) | |
}) | |
it('should return false if strings has SAME order of transformation even in a portion of string', () => { | |
expect(canReconfigure('regALo', 'riALge')).toBe(false) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment