My coding education journey has been a bit, disjointed to say the least. I've stop started on freeCodeCamp, CS50, other tutorials, books, and just hanging out with smart people. I made it a goal to go back and get some of the certifications I bounced around on this year, and today solved the "Roman Numeral Converter" on freeCodeCamp. I'd love to know how others solved this. Because this was just the first idea I came up with and I'm sure there are many other ways to get it done!
What are the pros and cons of your solution?
Top comments (5)
The last code test I took for an interview involved writing a calculator in Java:
pro:
con:
Whew man, I donβt know Java at all, so this looks really complicated to me.
The way it works is by subtracting until you can add based only on the one-to-one mapping in descending order. So if you're trying to convert 1990, it goes like this:
1000 - (1000 / 10)
), so we set thelastEntry
pointer to M and proceedlastEntry
is set. 990 > 900 which is CM, giving us MCM so far and leaving 90. I think this is where the bug is but it works in this case :)100 - (100 / 10)
) solastEntry
is now ClastEntry
is set, but the calculation to test whether L can be subtracted fails, so we proceedNice thatβs such a cool way of thinking about it!
Mine works... but I every time I tried doing something clever to not have to define 900, 400, 90, 40, and 9, it wouldn't work on 4, so I gave up on that. I really liked the .repeat so I didn't have to define multiples, though.