- Fork this gist, then "edit" the gist
- Fill out the questions below
- Click the "Add file" button and add your source code to the gist
- 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.
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
Write a recursive function that will take in an integer and return the Roman numeral associated with that integer. Assume the integers are not greated than 1000.
What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?
- 4000 is MMMM, not sure what 5000 is, so I can't subtract it for that case.
- Integers will be greater than 0
- Keep numbers as integers until reporting the final string of characters
I need to keep track of how many digits long each number is and the leading digit. Each of the digits and locations will tell me which Roman numeral to add to the string. As I add to the string, I'll move from the left to right sides of the input integer.
- Searching of Data
- Sorting of Data
- Pattern Recognition
- Build/Navigate a Grid
- Math
- Language API knowledge
- Optimization
I will be using strings and integers in this problem. The strings will be added onto recursively. I don't believe I will need any other data types at this time.
Write out a few lines of initial pseudocode: (mid-level design, this should be short, and not be real code!)
- Make a function that takes in an integer as an argument.
- Check if that integer is zero, if so, kick out of recursion.
- Check if integer is 1000 or above, find out first digit and add that many M's to the string. Subtract the 1000s from the integer and make the subtracted integer the input to the function recursively.
- Check if integer is 900 or above, add CM to string and subtract 900 from integer. Make subtracted integer the input to the function recursively.
- Check if integer is 500 or above, add D to string and subtract 500 from integer. Make subtracted integer the input to the function recursively.
- Check if integer is 100 or above, check leading digit, add that many C's to the string. Call function.
- Repeat for rest of digits in a similar way to above.
- String should be reported once step 2 triggers as true.
I would think the BigO notation for time complexity is O(n). This is because checking the size of a integer is relatively simple with a O(1), but then the function is called over and over again, making the function be called in proportion to n. I believe the space complexity is O(n).
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"
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"