Skip to content

Instantly share code, notes, and snippets.

@amykarnaze
Last active November 5, 2020 17:25
Show Gist options
  • Save amykarnaze/d4dce268b48c6b1f2d96bc9eea666a6c to your computer and use it in GitHub Desktop.
Save amykarnaze/d4dce268b48c6b1f2d96bc9eea666a6c to your computer and use it in GitHub Desktop.
Mod 4 code challenge

Problem - Roman Numerals

Roman Numerals

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.

For example, the number 4 could be written as IIII but it's more efficient to use IVsince 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

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"

Instructions

  1. Copy this markdown and paste in your own, privte gist
  2. In your private gist, fill out the questions below
  3. 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.

Rewrite the question in your own words:

Convert a number to roman numerals

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

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

convert number to a sting, get its length

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?

stings for to array for iterating

Write out a few lines of initial pseudocode: (mid-level design, NOT REAL CODE)

recursion

Write out any implementation code OR link to repl

function toRoman(num) { if(num < 1){ return "";} if(num >= 50){ return "L" + toRoman(num - 50);} if(num >= 40){ return "XL" + toRoman(num - 40);} if(num >= 10){ return "X" + toRoman(num - 10);} if(num >= 9){ return "IX" + toRoman(num - 9);} if(num >= 5){ return "V" + toRoman(num - 5);} if(num >= 4){ return "IV" + toRoman(num - 4);} if(num >= 1){ return "I" + toRoman(num - 1);}
} console.log(toRoman(52));

What is the Big O complexity of your solution?

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