Goal:
Write a function that takes a number as input and returns a short-hand roman numeral for that number as output.
Roman Numeral Symbols:
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 For an example of what I mean when I say "short-hand roman numerals", let's consider finding a roman numeral to represent 1983, because that is the year I was born. One option is to do this the normal way (10 letters):
1983 = MCMLXXXIII = (1000 - 100 + 1000 + 50 + 30 + 3)
The other option is to do it the short-hand way (6 characters):
1983 = MXVIIM = (1000 - (10 + 10) + 1000 + 3)
Do you know what this means?!?!!?? If I was roman I could have saved 4 characters every single time I wrote my birth date! Woot Woot!!
However, before I get ahead of myself in excitement, I have a question to write, so I should probably define the short-hand roman numeral rules so we are all on the same page:
Short-Hand Roman Numeral Rules:
- Always consider symbols from left to right until there are no more characters to consider.
- If there are no higher-valued symbols to the right of the current symbol:
- Add the value of the current symbol to the running total of this roman numeral.
- If there are higher-valued symbols to the right of the symbol you are considering:
- Locate the rightmost highest-valued symbol to the right of the current symbol
- Consider all the characters up to that symbol as one roman numeral
- Calculate the value of that roman numeral using these steps
- Subtract the value of that roman numeral from the running total of this roman numeral.
- Move to the next symbol after the group you just considered
- Each roman numeral must have at least 1 symbol in it.
- That's it! Anything following these rules will be accepted!
Examples:
IIIIV = (-(1+1+1+1)+5) = 1 //Don't ask me why you'd want to do this! VVX = (-(5+5) + 10) = 0 //Who said you couldn't represent 0 with roman numerals?!!? VVXM = (-(-(5+5) + 10) + 1000) = 1000 //Again...don't ask me why you'd want to do this! MXIIXMI = (1000-(10-(1+1)+10)+1000+1) = 1983 //Ahhh...such a great year :) Question Rules:
Make a function that takes a single number as input and returns a roman numeral for that number as output using the above rules. Calculate the codeGolfScore of this function.
example input: 2011 example possible output: MMXI another possible output: MMVVIVV //(2000 + 10 - 4 + 5)Using your function from rule 1, generate the roman numerals between -1000 (that's right, NEGATIVE one-thousand) and 3000. Then sum up the character length of these roman numerals to get your totalCharacterCount. Here's some pseudocode to clarify:
totalCharacterCount = 0; for(currentNumber = -1000; currentNumber <= 3000; currentNumber++){ totalCharacterCount += getRomanNumeral(currentNumber).length; } return totalCharacterCount;finalScore = codeGolfScore + totalCharacterCount
- Lowest finalScore wins!
Note: As the totalCharacter count will be in the ten-thousands+, the character-length algorithm should be top priority. Code-golf scores are just the tie-breaker in case multiple users find the optimal algorithm or algorithms that are close to each other.
Good luck, and have fun at your MMXII celebrations tomorrow night!!!
DDDDMstand for-1000? \$\endgroup\$""allowed for zero or do we have to useVVXor something equivalent? \$\endgroup\$IXV = -(-1 + 10) + 5 = -4(rightmost wins), orIXV = -1 + 10 + 5 = 14(highest-valued wins)? \$\endgroup\$