Skip to content

Instantly share code, notes, and snippets.

@OlegKorn
Last active October 7, 2022 04:29
Show Gist options
  • Save OlegKorn/46b975200427df522df5f6599b7f3dd3 to your computer and use it in GitHub Desktop.
Save OlegKorn/46b975200427df522df5f6599b7f3dd3 to your computer and use it in GitHub Desktop.
leetcode - intToRoman.js
/*
author Oleg Kornilov
[email protected], https://github.com/OlegKorn
*/
var intToRoman = function(num) {
var equivalents = {
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M"
}
function isThereEquivalent(equivalentsArr, value) {
try {
var equivalent = Object.entries(equivalentsArr).find(([key]) => key === value.toString())[1]
return equivalent
}
catch (e) {
if (e instanceof TypeError) {
return false
}
}
}
var decimals = []
var convertedToRoman = ""
var nStr = num.toString()
for (var i = nStr.length; i > 0; i--) {
var decimalPosition = nStr.slice(-i)
if (decimalPosition[0] === "0") {
continue
}
var toSubstract = decimalPosition.substring(1, decimalPosition.length)
var clearDecimalPosition = Number(decimalPosition) - Number(toSubstract)
decimals.push(clearDecimalPosition)
}
for (var i = 0; i < decimals.length; i++) {
// if decimals[i] has its letter equivalent
if (isThereEquivalent(equivalents, decimals[i])) {
convertedToRoman += isThereEquivalent(equivalents, decimals[i]).toString()
// console.log(decimals[i], isThereEquivalent(equivalents, decimals[i]), convertedToRoman)
continue
}
// if decimals[i] is >= 1000
if (decimals[i].toString().length > 3) {
// now have to decompose this.decimalPositions[i] into its closestEquivalent and remainders
var closestEquivalent = Number(Object.keys(equivalents)[Object.keys(equivalents).length - 1])
var howManyEquivalentsInThis = decimals[i] / closestEquivalent
var closestEquivalentLetterValue = isThereEquivalent(equivalents, closestEquivalent)
var equivalent = closestEquivalentLetterValue.repeat(howManyEquivalentsInThis)
convertedToRoman += equivalent
}
for (var j = 0; j < Object.keys(equivalents).length; j++) {
// find closest eqiuvalents of decimals[i] so that it's between them
// e.g. 500 700 900
if ((decimals[i] < Object.keys(equivalents)[j+1]) && (decimals[i] > Object.keys(equivalents)[j])) {
// now have to decompose this.decimalPositions[i] into its closestEquivalent and remainders
var closestEquivalent = Number(Object.keys(equivalents)[j]) // 500
var closestEquivalentLetter = isThereEquivalent(equivalents, closestEquivalent) // D
convertedToRoman += closestEquivalentLetter
// 200
var remained = decimals[i] - closestEquivalent // 200
var remainedEquivalent = remained / Number(remained.toString().charAt(0)) // 100
var howManyEquivalentsInRemained = remained / Number(remainedEquivalent) // 2
var counter = howManyEquivalentsInRemained
while (counter !== 0) {
var remainedLetters = isThereEquivalent(equivalents, remainedEquivalent).repeat(1)
convertedToRoman += remainedLetters
counter -= 1
}
}
}
}
return convertedToRoman
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment