Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimalajoy/c8bbea59ec722a5a1689748a62764466 to your computer and use it in GitHub Desktop.
Save kimalajoy/c8bbea59ec722a5a1689748a62764466 to your computer and use it in GitHub Desktop.
function toRoman(num) {
let arr = Array.from(String(num), Number);
let roman = []
//base case
if (roman.length === arr.length) {
return roman
}
console.log(arr)
for (let i = 0; i < arr.length; i++) {
if(arr.length === 4) {
// this would need to return the number of Ms that the digit at [0] is
// then push those ms into the roman array
// Ugh, crap my base case wont work now because you're returning a different amount roman numerals than digits in the num argument!!
//
} else if (arr.length === 3) {
} else if (arr.length === 2) {
} else if (arr.length === 1) {
}
}
}

Instructions

  1. Fork this gist, then "edit" the gist
  2. Fill out the questions below
  3. Click the "Add file" button and add your source code to the gist
  4. Submit by the due time as instructed in Zoom

Do not publish your code on a public repl.it or repo or other public means.

Prompt

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. eg, the number 4 could be written as 'IIII' but it's more efficient to use 'IV' since 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

Rewrite the question in your own words:

We will need to take an input of normal numbers and translate those numbers into roman numerals. However, we must use the most 'efficient' 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?

No negatives, case insensitive on the roman numerals.

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

Well, I know I am going to need to use recursion, and so I know I would need a base case and a recursion case. I think that checking different cases for the length numbers is something that I could do, so write some conditionals about the length, so a 4 digit input would mean you would at least need an M in there and then you could check the number in the [0] index and output that many Ms and then move on down the line maybe

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 guess I think I will need a for loop maybe? Def some if statements

Write out a few lines of initial pseudocode: (mid-level design, this should be short, and not be real code!)

have an empty array (or string) to push numbers into, probably will need to toString the numbers Start checking the length of the entire number (ie - .length === 4? you know you need an m) check the digit in [0] position - output that many Ms

base case - if the empty array.length === input.length return

write out your pseudocode here

What do you think the Big O complexity of your algorithm is? (time complexity and space complexity)

I know I am probably going to need a loop of some kind so probably O(n^2)

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"

Ruby Starter Code

def to_roman(num)
  # your code goes here
end

puts to_roman(128)   # should return "CXXVIII"
puts to_roman(2000)  # should return "MM"
puts to_roman(2017)  # should return "MMXVII"
puts to_roman(1999)  # should return "MCMXCIX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment