Skip to content

Instantly share code, notes, and snippets.

@DrewBradley
Last active April 15, 2021 15:20
Show Gist options
  • Select an option

  • Save DrewBradley/62d47f4186d6914d925c675832d28dab to your computer and use it in GitHub Desktop.

Select an option

Save DrewBradley/62d47f4186d6914d925c675832d28dab to your computer and use it in GitHub Desktop.

Problem - Roman Numerals

Roman Numerals

Write a recursive function that converts an integer into a string such that the number is represented in Roman Numerals in the most efficient way.

For example, the number 4 could be written as IIII but it's more efficient to use IVsince that's a shorter string.

Assume no number is greater than 4,000.

Here are the Roman Numeral equivalents you'll need to know:

  • M=1000
  • CM=900
  • D=500
  • CD=400
  • C=100
  • XC=90
  • L=50
  • XL=40
  • X=10
  • IX=9
  • V=5
  • IV=4
  • I=1

JS Starter Code

function toRoman(num) {
  // your code goes here
}

console.log(toRoman(128));  // should return "CXXVIII"
console.log(toRoman(2000)); // should return "MM"
console.log(toRoman(2017)); // should return "MMXVII"
console.log(toRoman(1999)); // should return "MCMXCIX"

Rewrite the question in your own words:

Given a an integer , return a string that represents the integer as a Roman Numeral..

What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?

  • Input will always be an integer.
  • Output should be a string.

What are your initial thoughts about this problem? (high level design, 2-3 sentences)

  • Most 'decoding' functions need some kind of object with key value pairs that correspond to the input and output.
  • Will it be better to have numbers or letters as key, or vice versa?
  • I want to ADD the letter to the string, and because the input is a number, the output will be interpolating the "keys" that are added to the string.

How would you identify the elements of this problem?

  • Searching of Data
  • Sorting of Data
  • Pattern Recognition
  • Build/Navigate a Grid
  • Math
  • Language API knowledge
  • Optimization

Which data structure(s) do you think you'll use? What pros/cons do you see with that choice?

  • I believe I will be using an object, which means I will probably need to use either Object.key() of a for - in loop.
  • The pros are that it will be creating a key to translate the number, but it will have to be iterated over repeatedly because of the size of the input.

Write out a few lines of initial pseudocode: (mid-level design, NOT REAL CODE)

  • Create an "map" object for the roman numerals - similar to what is written in the prompt above - that has key: value pairs with the letter as the key and the corresponding number as it's value.
  • Iterate over the object, and add the letter to a string, but only while the number is bigger than the value of the corresponding key
  • Final subtract that number value from the input and iterate over the object again.
  • Continue until the number is zero, then reture the string of letters.

Write out any implementation code OR link to repl

https://replit.com/@DrewBradley/RomanNumerals#index.js

What is the Big O complexity of your solution?

O(n)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment