Roman numeral conversion problem
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm writing a program that converts between Roman numerals and Arabic ones. I get an error when trying to convert 'C' to 100.
Prompt: Write a function called roman2 that takes a string input representing an integer between 1 and 399 inclusive using Roman numerals and returns the Arabic equivalent as a uint16. If the input is illegal, or its value is larger than 399, roman2 returns 0 instead. The rules for Roman numerals can be found here: http://en.wikipedia.org/wiki/Roman_numerals. Use the definition at the beginning of the page under the “Reading Roman Numerals” heading. In order to have unambiguous one-to-one mapping from Roman to Arabic numbers, consider only the shortest possible roman representation as legal. Therefore, only three consecutive symbols can be the same (IIII or VIIII are illegal, but IV and IX are fine). Also, a subtractive notation cannot be followed by an additive one using the same symbols making strange combinations, such as IXI for 10 or IXX for 19, illegal also.
My code:
function num = roman2(r)
romans = { 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX' 'X' ...
'XI' 'XII' 'XIII' 'XIV' 'XV' 'XVI' 'XVII' 'XVIII' 'XIX' 'XX' 'XXX'...
'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC' 'C' 'CX' 'CXX' 'CXX' 'CXL' 'CL' 'CLX' ...
'CLXX' 'CLXXX' 'CXC' 'CC' 'CCX' 'CCXX' 'CCXXX' 'CCXL' 'CCL' 'CCLX' ...
'CCLXX' 'CCLXXX' 'CCXC' 'CCC' 'CCCX' 'CCCXX' 'CCCXXX' 'CCCXL' 'CCCL' ...
'CCCLX' 'CCCLXX' 'CCCLXXX' 'CCCXC' 'CCCXCIX' 'CD' };
num = uint16(0);
for ii = 1:399
if strcmp(r,romans{ii})
num = uint16(ii);
break
end
end
end
The error message reads:
Feedback: Your function performed correctly for argument(s) 'I'
Feedback: Your function performed correctly for argument(s) 'V'
Feedback: Your function performed correctly for argument(s) 'X'
Feedback: Your function made an error for argument(s) 'C'
Your solution is _not_ correct.
Any help would be much appreciated. Thanks!
2 Kommentare
Cedric
am 1 Sep. 2015
Bearbeitet: Cedric
am 1 Sep. 2015
When r is 'C', what happens in your code? What is the index of the 'C' element in the cell array romans, is it 100?
This is a good example that illustrates that a test failing or a crash is a lucky event (unless we think well about a sound test phase and take time to implement it), because it allows to detect a faulty behavior. In your case, the fact that it works for the first three tests is only due to the fact that you were lucky (or unlucky) enough so the order of the roman elements and their indices in the cell array do match.
This tells you something about the approach: if you want to limit the logic to simple string comparison, you will have to store 399 roman numbers (all valid cases) in the cell array. But as there is a logic (described in the wiki), you can avoid this full enumeration by implementing a more elaborate logic.
Stephen23
am 1 Sep. 2015
Bearbeitet: Stephen23
am 1 Sep. 2015
The whole idea of scripting and programming is that you do not need to do every little task explicitly, because computers are basically very fast repeaters of simple tasks. So while your concept (incorrectly implemented in your code) could work in practice it will be long, liable to bugs, have to be implemented entirely by hand, be larger than required and require very thorough testing.
What you should do, and what is really the aim of your task, is to read and understand the rules of roman numbers (they even gave you a link for this) and think about how these rules can be implemented by an algorithm. In fact understanding what you are trying to code is the most important step, more important than sitting at your computer and typing code.
Only when you do this yourself will you actually learn anything about computer programming and algorithms. We could solve it for you, but what would you learn then?
Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!