Skip to content

Instantly share code, notes, and snippets.

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

Write a reccursive function that converts numbers into their associated roman numeric values. When displaying the roman numerals, provide the most efficient value (4 = IV, 4 != IIII). You will also put a cap on the conversions of 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 am assuming we will be displaying only the provided argument roman numeral

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

I am thinking I will need to create a blueprint object that gives the recursive function a guide on what base values to display. Once I have the base values, I will then narrow it down to the more precise numbers.

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?

I will use an Object/Hash. The pros of this will be the ability to assign certain values to certain roman nummerals for easy base numeral values. The cons will be trying to find the more precise values hidden within the object.

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

 - create an object where the keys are roman numerals and the values are the assigned numeric values
 - add a accumulator as a global variable that will be keeping track of the current value, reset after the base case is met.
 - write a base case where if the provided number is greater than 4000, return an error message
 - also write a base case where if number passed in is or equal to 0 return the joined current value
 - write a recursive case where if the the number provided is greater than or equal to any value in the object, push that letter into the current value array.
 - I want to use loop iteration it would allow me to iterate through the objects values. I cant do this though
 - How can I cycle through the values of an object without using iteration?
 - How can I do this without adding a condition for every single case?
 - If I start this problem I will have no direction on how to access values

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

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment