Skip to content

Instantly share code, notes, and snippets.

@ttarlov
Forked from ericweissman/roman_numeral_recursion.md
Last active July 30, 2020 16:23
Show Gist options
  • Save ttarlov/a42a4c2269b09442ac4bc78077ab1eb5 to your computer and use it in GitHub Desktop.
Save ttarlov/a42a4c2269b09442ac4bc78077ab1eb5 to your computer and use it in GitHub Desktop.

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:

   I need to write a recursive function that takes in a regular numeber and converts it to a Roman Numeral number
   in the most consise way. 

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

 I would a have to assume that we are starting with number 1 and not zero. 

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

I imagine i will need to store the "conversion" data in an object. Where roman numberal is a key and its 
equivalent in a Hindu–Arabic numeral system. 

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?

   Most likely will need to use an object to store the "conversion" data. Pros: there is no index position to 
   deal with as it is with an array. Cons: Its not as easy to iterate over an object as it is over an array. 

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

                1. declare a variable with a data type of object.
                2. assign key to the roman numeral and value to the equvalent in Hindu-Arabic system. I.E V:5.
                3. write a function named toRoman that takes a Hindu-Arabic number as an argument. 
                4. the function should have a base case that will be triggered if the argument passed 
                into the function is less than 1 , which in this case it should return something other than a 
                number. So possibly an empty string or simply null. I think null would be more intentional. 
                5. the function will need a recursive case as well. There will probably be more than one loop involved
                

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

I imagine this is going to be a nested loop which makes this a 0(n^2).

JS Starter Code

                   const romanNums = {
                         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,
                  }

                 // note above roman numberas need to be strings

                 const toRoman = number => {

                   // Base case here
                   if (number < 1 ) {
                     return null
                   }


                 }
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