Ameba Ownd

アプリで簡単、無料ホームページ作成

Java script roman numeral converter

2022.01.17 02:02




















I think that my algorithm is far the most easiest to understand and the bit of performance lost is not important even on a relatively large scale. I'm also obeying the standardized coding conventions as opposed to some of the users here. Average conversion time: 0. Then you can try to find the largest set roman numeral in a given integer and if found subtract it from the original number and add it to the result.


Repeat with the newly acquired number until you hit zero. Alternative solution based on the OP's own solution by utilizing an enum.


Additionally, a parser and round-trip tests are included. Just to keep up with the technology, here's a Java 8 version using streams and a custom Collector, eliminating the need for loops or if statements:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.


Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 9 years, 2 months ago. Active 11 months ago. Viewed k times. This is a homework assignment I am having trouble with. Is there a way to assign values to Strings and then add them together when I call the method? Update Oct 28 : I got it working.


Michael Currie 12k 8 8 gold badges 40 40 silver badges 56 56 bronze badges. Given the fact that this is a homework, you should concatenate the String s as in the provided code. Otherwise, if you're thinking on performance and best practices, you should use the StringBuilder as stated in Andrew Thompson's comment.


Because we know 'ixix' will be 'xviii' for instance. I think it 6 jmp instruction less in output and 18 6 times 3 char less in code — Et7f3XIV. Add a comment. Active Oldest Votes.


A compact implementation using Java TreeMap and recursion: import java. I am in awe with the simplicity and conciseness of this code. Hats off. Why the TreeMap? ElroyJetson TreeMap is sorting the keys by natural order.


The method TreeMap floorKey has a vital role in this solution as you can conveniently lookup the greatest key less than or equal to the given key. If there is an exact match you just return the associated roman symbol, else you just concatenate the roman symbol associated by the greatest key less than the given number to the one returned by recursively calling the function using the current number subtracting the previous greatest key found.


How would you go about creating method that turns roman back to an Int using this method? Feeling inspired so I am going to throw my hat in the ring too! You can use "I". Use these libraries: import java. LinkedHashMap; import java. Since it has an uppercase letter at the start, it gets formatted as if it were a Class Type — Alfredo Gallegos. Also why are you creating and initializing the map for every invocation? Just make it a private static final — RecursiveExceptionException.


I can't remember after 5 years. It is not easy to come up with a solution to convert roman numeral to integer but I would recommend that you should practice solving this problem by your own. Writing a program for this conversion is little difficult because you will have to first understand the pattern of how roman numeral works. Our conversion function is divided in two different formats, one which converts the value less than 9 because it follows different pattern and then value which is greater than 9 because it follows another pattern.


I would not go into to the details of how this program works, this is a exercise for you to figure it out because it will really increase your problem solving skills.


Now when the convert button is clicked or enter button is pressed we have to perform the conversion, for which we need to get the inputs and show the output. When the checkbox is checked we have to update the text to notify user which type of conversion is going to happen. After this when convert button is clicked or enter button is pressed we should perform the conversion, so it is good idea to create a function which will call the appropriate conversion function based on the checkbox value and we call this function to trigger the conversion.


By building this project you will learn many important things in problem solving. HTML:- Layout the elements. CSS:- Styling the layout. Starting at the largest number, continue subtracting from the lookup table and appending as long as the remainder is greater than the lookup value. Objects have no order! You should use an array and avoid for I'm not talking about performance.


I'm saying that the iteration order is not guaranteed, so the result may be completely wrong. I can't give an non-working example, because the order is implementation dependent. Instead, please link me which part of the spec ensures it will be iterated with the desired order. Oh, you can't. Furthermore even if you test it, it does not mean that it always works.


There might be cases where a browser changes the order for performance reasons or whatever reasons. You can only be sure, if you know the source code of every version of browser that you support and want to support in the future. Show 8 more comments. I don't understand why everyones solution is so long and uses multiple for loops.


August August 1, 4 4 gold badges 18 18 silver badges 28 28 bronze badges. I think I finally understand this solution. You can place it at the end of the loop and remove the 'return str'.


Yes kite, nicely spotted! It would make it loop less, however, considering that we are only looping through 13 items, it won't make much of a difference : — August. Piotr Berebecki Piotr Berebecki 7, 3 3 gold badges 30 30 silver badges 41 41 bronze badges. I really like your solution.


Easy to read, understand and very simple. There is too many repetitions that happens and that is unnecessary. Best solution so far. Number to Roman Numeral: Number. I personally think the neatest way not by any means the fastest is with recursion. Deftwun Deftwun 1, 11 11 silver badges 21 21 bronze badges.


Ah this is clever. I did pretty much same thing using switch statements, this would have been my next refactor after figuring out the pattern and making it more elegant. I have tested this code against a data set from and it works. TLDR; This also means this solution can handle numbers greater than the maximum roman scale could Shawn Shawn 3, 7 7 gold badges 43 43 silver badges 60 60 bronze badges. Why did this get a downvote? It is a working as-is example that answers the question and is very well documented?


From my perspective, these are probably the most interesting but least practical, if for no other reason than the added function call overhead of the implementations so far. If the idioms are foreign to you, check out the Chiron source to figure out how it works. Anyone wonder how the Romans actually read those numbers? I mean, we read every digit and say something about it. How did they do it? Since the regex-based validation can easily be added in, it looks like your deromanize is the one to beat.


The leading zero causes parseInt to treat it as an octal number in probably all browsers, despite octals being summarily deprecated in ES3. Of course, you can use parseInt "",10 to get around that. The plus sign is there to remove leading zeros if the number is received as a string. When validating the string, IMO, the function should throw an Error, not return false. Rafael, I agree that returning NaN or throwing an error would be better than returning false.


That saves four characters. Additionally, recreating the lookup object during each iteration is going to add a little bit of overhead. The other changes you made cause the function to return 0 when passed an empty string, and not handle lowercase Roman numeral characters.