Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nickstaylor/2b42dec6b0d005a3948035ec7ee7711c to your computer and use it in GitHub Desktop.
Save nickstaylor/2b42dec6b0d005a3948035ec7ee7711c 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:

This problem is asking us to create a function that takes in a number (ex: 4) and converts it to the corresponding and most efficient Roman Numeral form ( ex: IV for '4').

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 am assuming that the inputs will alays be a number greater than 0 and less than 4001.

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

Initially, because the concept of recursion is still fuzzy, I will try to reason through this problem not thinking about recursion. I am thinking that there will have to be some sort of comparison of the number and the roman numeral. Just thinking first, I may just try to turn the number into an 'inefficient' (ie: 4 would be IIII) roman numeral first and then work from there.

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?

Optimization comes to mind first because we are going for the most 'efficient' outcome. There is also a conversion being done according to some math.

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

write out your pseudocode here:
1. start by setting up the conversion chart in the function
2. deconstruct each number coming in, possibly by place value. Ex: 128 = 100, 20, 8
3. convert those deonstructed numbers to the correspoding roman numeral values.  maybe with a map array prototype
4. (this is the not sure part):  Figure out some way to check for optimization
5. join and return the array.

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

welp, this one was a bit a humbler. I'm guessing so far my Big O completxity is O(n^16) or something along those lines.

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"
@nickstaylor
Copy link
Author

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