C
C#β€’2y ago
scxft

βœ… Why does this Roman numeral to denary converter not work?

I can see that the problem is if an X is inputed after a CM, it makes it 1000 rather than 900. https://paste.mod.gg/wehmvqpimjrw/0 https://leetcode.com/problems/roman-to-integer/
BlazeBin - wehmvqpimjrw
A tool for sharing your source code with the world!
LeetCode
Roman to Integer - LeetCode
Can you solve this real interview question? Roman to Integer - Roman numerals are represented by seven different symbols:Β I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example,Β 2 is written as IIΒ in Roman numeral, just two...
20 Replies
ero
eroβ€’2y ago
Rather than 910 you mean?
scxft
scxftOPβ€’2y ago
no i meant it outputs 1010 rather than 910 so cm is registered as 1000 when it shouold be 900
ero
eroβ€’2y ago
But cm on its own is correct? It's only an issue when it's cmx?
scxft
scxftOPβ€’2y ago
yes
ZacharyPatten
ZacharyPattenβ€’2y ago
I'm still reading your code and haven't quite found the bug yet, but I can link my implementation if you wanted to see it
scxft
scxftOPβ€’2y ago
scxft
scxftOPβ€’2y ago
yes please
scxft
scxftOPβ€’2y ago
I've been trying to figure it out for like 4 hours lmao finally given up thanks you definitely had a lot more efficient approach im still frustrated I cant figure out the bug tho
ZacharyPatten
ZacharyPattenβ€’2y ago
I mean... the most efficient code is just to have a single switch for every case πŸ˜„ there are only ~4000 numbers that roman numerals can represent but I felt too dirty having that large of a switch for such a stupid thing... so I didn't do it in my code
scxft
scxftOPβ€’2y ago
fair enough did you figure out the bug/
ZacharyPatten
ZacharyPattenβ€’2y ago
I believe you are missing quite a few cases in your code in my mind you have a bit of a fundamental logic flaw. you shouldn't have both forward looking and reverse looking logic pick one and stick with it but probably one thing that was tripping you up was this:
else if (i > 0)
ZacharyPatten
ZacharyPattenβ€’2y ago
ZacharyPatten
ZacharyPattenβ€’2y ago
why do you have an "else" there? forward and back are independent variables. both should be "if" checks not one "if" and one "if else"
scxft
scxftOPβ€’2y ago
thanks that fixed it
ero
eroβ€’2y ago
Surely this isn't true Pretty sure there's a sign for 5k and 10k
scxft
scxftOPβ€’2y ago
it wasnt even my idea lol, someone in this server told me to use it - @Pobiega
ZacharyPatten
ZacharyPattenβ€’2y ago
the standard roman numerals only go to 3999
Pobiega
Pobiegaβ€’2y ago
I also told you this was a very fragile approach to Roman numerals :p
Unknown User
Unknown Userβ€’2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server