Skip to content

Instantly share code, notes, and snippets.

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

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.

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

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.

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?

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
}
}

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

Quadratic time complexity

JS Starter Code

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)

@Corbinj22
Copy link
Author

`
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)`

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