Created
May 4, 2017 20:37
-
-
Save jgierer12/3ed2e7da8fc6e95590a86d20a907b6b4 to your computer and use it in GitHub Desktop.
Solution to r/DailyProgrammer challenge #277 (https://www.reddit.com/r/dailyprogrammer/comments/4uhqdb/20160725_challenge_277_easy_simplifying_fractions/)
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
const gcd = (x, y) => y ? gcd(y, x % y) : x; | |
const simplify = xs => xs.map(x => x / gcd(...xs)); | |
export default str => simplify(str.split(' ')).join(' '); |
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 m from '.'; | |
test('4 8 -> 1 2', () => { | |
expect(m('4 8')).toBe('1 2'); | |
}); | |
test('1536 78360 -> 64 3265', () => { | |
expect(m('1536 78360')).toBe('64 3265'); | |
}); | |
test('51478 5536 -> 25739 2768', () => { | |
expect(m('51478 5536')).toBe('25739 2768'); | |
}); | |
test('46410 119340 -> 7 18', () => { | |
expect(m('46410 119340')).toBe('7 18'); | |
}); | |
test('7673 4729 -> 7673 4729', () => { | |
expect(m('7673 4729')).toBe('7673 4729'); | |
}); | |
test('4096 1024 -> 4 1', () => { | |
expect(m('4096 1024')).toBe('4 1'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment