Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MeganHuggins/458f74ddec24e683dbdef0ff56ee1ead to your computer and use it in GitHub Desktop.

Select an option

Save MeganHuggins/458f74ddec24e683dbdef0ff56ee1ead 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:

Using the roman numeral key provided, check a specific integer that is coming through against the key and return a string of roman numerals that corresponds with the number found in the integer.

What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?

That I should set up a 'key' of sorts (object key value pairs) for what each roman numeral equals out to. This way I can I check through this 'key' with each of the numbers in the integer and from there be able to match up/ return what I need.

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

My itital thoughts for this problem is that it states that it wants me to use a recursive function, meaning one that calls upon itself untill a specified solution is reached. By knowing this I can then begin to set up my function in which a series of conditionals, regarding the num coming in, will be put in action. In these conditionals it will convert parts of the num to a string, then add that string to the output and then call the function again. To stop this process I will need to add a conditional for if no number is inputted (the end of the integer).

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?

Since that is the learning goal for this challenge is to use a recursive fucntion, that is the data structure I will be using. The pros of using this choice is that it will help keep my code more compact and efficient. The con being I'm new to recursive fucntions so there is a bit of a learning curve going on with it.

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

  • // Create a function to_roman
  • // Inside the function create the variable 'romanKey' and assign it to object with the key value pairs
  • // Within this romanKey object have the keys be the string roman nuermals and the values be their corresponding values {'M':1000, 'CM':900, etc)
  • // Create the variable romanNumeral and assign it to an empty string, this is where we will push up our found roman strings into
  • // Write the first conditional for the recursive function that when it gets a number that is 0 it will return and end the process
  • // Next write a conditional that takes in the whole integer and checks to see if it is less or equal to 4000
  • // If the integer is greater than 4000 return and end the process
  • // If the integer does pass the first conditional take the integer, and pass it through a conditional that takes each number from the integer and checks it against the key given the placement it is in the interger as whole
  • // Once this check is complete, push the found corresponding string into the romanNumeral variable
  • // As this conditional returns the out put needed for one letter it then calls itself to return the next letter
  • // Complete this process untill the first conditional is met and the recursive function is stopped and return the joined romanNumeral string

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

Because I am only using one for loop over and voer again I believe the Big 0 complexity would be (O)n

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