Last active
July 11, 2018 19:28
-
-
Save ambergkim/1077f3a16c7a9d90fbb69fa430be2800 to your computer and use it in GitHub Desktop.
Converting Integers to Roman Numerals
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
// Roman Numerals | |
// I, II, III, IV, V, VI, VII, VIII, IX, X | |
// X, XX, XX, XL, L, LX, LXX, LXXX, XC, C | |
// C, CC, CCC, CD, D, DC, DCC, DCCC, CM, M | |
// EXAMPLES: | |
// 2018: MM X VIII | |
// 199: C XC IX | |
// Input: Integer | |
// Output: String | |
// hash of above roman numerals | |
// zero will be empty string. | |
// Int / 1000 = Number of thousands | |
// truncate to whole int | |
// whole int * 1000 = index in hash table. | |
// Int - wholeThousands | |
// divide by 100 = number of hundreds | |
// truncate to wholeHundreds | |
// wholeHundreds * 100 = index in hash table. | |
// Int - wholeHundreds | |
// divide by 10's | |
// truncate to wholeTens | |
// wholeTens * 10 = index in hash table. | |
// Int - whole Tens | |
// left is index in hash table. | |
// Output: thousands + hundreds + tens + ones; | |
let romanHash = []; | |
romanHash[0] = ''; | |
romanHash[1] = 'I'; | |
romanHash[2] = 'II'; | |
romanHash[3] = 'III'; | |
romanHash[4] = 'IV'; | |
romanHash[5] = 'V'; | |
romanHash[6] = 'VI'; | |
romanHash[7] = 'VII'; | |
romanHash[8] = 'VIII'; | |
romanHash[9] = 'IX'; | |
romanHash[10] = 'X'; | |
romanHash[20] = 'XX'; | |
romanHash[30] = 'XXX'; | |
romanHash[40] = 'XL'; | |
romanHash[50] = 'L'; | |
romanHash[60] = 'LX'; | |
romanHash[70] = 'LXX'; | |
romanHash[80] = 'LXXX'; | |
romanHash[90] = 'XC'; | |
romanHash[100] = 'C'; | |
romanHash[200] = 'CC'; | |
romanHash[300] = 'CCC'; | |
romanHash[400] = 'CD'; | |
romanHash[500] = 'D'; | |
romanHash[600] = 'DC'; | |
romanHash[700] = 'DCC'; | |
romanHash[800] = 'DCCC'; | |
romanHash[900] = 'CM'; | |
romanHash[1000] = 'M'; | |
romanHash[2000] = 'MM'; | |
romanHash[3000] = 'MMM'; | |
romanHash[4000] = 'MMMM'; | |
function intToRoman(int) { | |
let wholeThousands = (Math.trunc(int / 1000)) * 1000; | |
let wholeHundreds = (Math.trunc((int - wholeThousands) / 100)) * 100; | |
let wholeTens = (Math.trunc((int - wholeThousands - wholeHundreds) / 10)) * 10; | |
let wholeOnes = int - wholeThousands - wholeHundreds - wholeTens; | |
return romanHash[wholeThousands] + romanHash[wholeHundreds] + romanHash[wholeTens] + romanHash[wholeOnes]; | |
} | |
let test2018 = intToRoman(2018); | |
// wholeThousands = 2000 | |
// 2018/1000 = 2.018 | |
// trunc -> 2 | |
// wholeHundreds = 0 | |
// 2018 - 2000 = 18 | |
// 18 / 100 = 0.18 | |
// trunc -> 0 | |
// wholeTens = 10 | |
// 2018 - 2000 -> 18 - 0 -> 18 | |
// 18 / 10 = 1.8 | |
// trunc -> 1 | |
// wholeOnes 8 | |
// 2018 - 2000 > 18 - 0 -> 18 - 10 -> 8 | |
// return 'MM' + '' + 'X' + 'VIII' -> MMXVIII | |
let test199 = intToRoman(199); | |
// wholeThousands = 0 | |
// wholeHundreds = 100 | |
// wholeTens = 90 | |
// wholeOnes = 9 | |
// return '' + 'C' + 'XC' + 'IX' => CXCIX | |
// BIG O Runtime: O(N) | |
// Space Complexity: O(N) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment