- 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
Given a value (number) take it and pass it through a function that returns the number in a roman numberal as efficiently as possible. For instance 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
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 will need to use the values I have been given for the roman numerals as a key.
I will use the key to compare the number I am given, find that number. Subtract it, then keep going until I have no value left.
- Searching of Data
- Sorting of Data
- Pattern Recognition
- Build/Navigate a Grid
- Math
- Language API knowledge
- Optimization
Object
Write out a few lines of initial pseudocode: (mid-level design, this should be short, and not be real code!)
function(Value given) {
if(Value < first roman number in list) {
subtract the number
run funciton again
} else {
if(Value < second roman number in list) {
subtract number
run function again
} else {
if(Value > roman number in list {
return number
}
}
Quadratic time complexity
DID NOT COMPLETE ALL CODE BUT BELIEVE THIS WILL/IS WORKING
var numeral = []
const romNum = (number) => {
if(number >= 1000) { number = number - 1000 numeral.push("M") romNum(number)
} else if (number >= 900) { number = number - 900 numeral.unshift("CM") romNum(number)
} else if (number >= 500) { number = number - 500 numeral.push("D") romNum(number)
} else if (number >= 400) { number = number - 400 numeral.unshift("CD") romNum(number)
} else { numeral = numeral.join('') console.log(numeral) } }
romNum(1900)
`
var numeral = []
const romNum = (number) => {
if(number >= 1000) {
number = number - 1000
numeral.push("M")
romNum(number)
} else if (number >= 900) {
number = number - 900
numeral.unshift("CM")
romNum(number)
} else if (number >= 500) {
number = number - 500
numeral.push("D")
romNum(number)
} else if (number >= 400) {
number = number - 400
numeral.unshift("CD")
romNum(number)
} else {
numeral = numeral.join('')
console.log(numeral)
}
}
romNum(1900)`