roman-numerals

How to sort an array of Roman numerals?

£可爱£侵袭症+ 提交于 2019-12-03 06:38:03
问题 I have an array containing Roman numerals (as strings of course). Like this: $a = array('XIX', 'LII', 'V', 'MCCXCIV', 'III', 'XIII'); I'd like to sort them according to the numeric values of these numerals, so the results should be something like: $sorted_a = array('III', 'V', 'XIII', 'XIX', 'LII', 'MCCXCIV'); So my question is: what is the best way to sort an array of Roman numerals? I know how to use the array sorting functions of PHP, I'm interested in the logic that goes on inside the

How to sort an array of Roman numerals?

让人想犯罪 __ 提交于 2019-12-02 20:14:24
I have an array containing Roman numerals (as strings of course). Like this: $a = array('XIX', 'LII', 'V', 'MCCXCIV', 'III', 'XIII'); I'd like to sort them according to the numeric values of these numerals, so the results should be something like: $sorted_a = array('III', 'V', 'XIII', 'XIX', 'LII', 'MCCXCIV'); So my question is: what is the best way to sort an array of Roman numerals? I know how to use the array sorting functions of PHP, I'm interested in the logic that goes on inside the comparison function. EDIT : For simplicity, I'm only looking for a way that deals with strings constructed

prolog convert numbers into roman numerals

限于喜欢 提交于 2019-11-30 09:13:41
问题 i have this code that converts integers into roman numerals i need to add a function that compares an integer with a roman numeral input and show if it's try or false, for example: roman(v,5). true toroman(0). toroman(N) :- N < 4, put("I"), M is N - 1, toroman(M). toroman(N) :- N = 4, put("I"), put("V"). toroman(N) :- N = 5, put("V"). toroman(N) :- N < 9, put("V"), M is N - 5, toroman(M). toroman(N) :- N = 9, put("I"), put("X"). toroman(N) :- N < 40, put("X"), M is N - 10, toroman(M). toroman

Convert integers to roman numerals using a syntax-directed translation scheme?

元气小坏坏 提交于 2019-11-30 07:13:16
问题 The Dragon Book includes an exercise on converting integers to roman numerals using a syntax-directed translation scheme. How can this be completed? 回答1: I would consider parsing from right-to-left. First, I would map the units column: 0 -> '' 1 -> 'I' 2 -> 'II' 3 -> 'III' 4 -> 'IV' ... 9 -> 'IX' Then, if there was a second column (e.g. second from the right = tens column), I would use that to map to 0 -> '' 1 -> 'X' 2 -> 'XX' ... 9 -> 'XC' That would need to be prepended to the initial

Numbers to Roman Numbers with php

依然范特西╮ 提交于 2019-11-30 03:43:40
I need to transform ordinary numbers to Roman numerals with php and I have this code: <?php function roman2number($roman){ $conv = array( array("letter" => 'I', "number" => 1), array("letter" => 'V', "number" => 5), array("letter" => 'X', "number" => 10), array("letter" => 'L', "number" => 50), array("letter" => 'C', "number" => 100), array("letter" => 'D', "number" => 500), array("letter" => 'M', "number" => 1000), array("letter" => 0, "number" => 0) ); $arabic = 0; $state = 0; $sidx = 0; $len = strlen($roman); while ($len >= 0) { $i = 0; $sidx = $len; while ($conv[$i]['number'] > 0) { if

prolog convert numbers into roman numerals

杀马特。学长 韩版系。学妹 提交于 2019-11-29 14:00:19
i have this code that converts integers into roman numerals i need to add a function that compares an integer with a roman numeral input and show if it's try or false, for example: roman(v,5). true toroman(0). toroman(N) :- N < 4, put("I"), M is N - 1, toroman(M). toroman(N) :- N = 4, put("I"), put("V"). toroman(N) :- N = 5, put("V"). toroman(N) :- N < 9, put("V"), M is N - 5, toroman(M). toroman(N) :- N = 9, put("I"), put("X"). toroman(N) :- N < 40, put("X"), M is N - 10, toroman(M). toroman(N) :- N < 50, put("X"), put("L"), M is N - 40, toroman(M). toroman(N) :- N < 90, put("L"), M is N - 50

Convert integers to roman numerals using a syntax-directed translation scheme?

混江龙づ霸主 提交于 2019-11-29 02:13:05
The Dragon Book includes an exercise on converting integers to roman numerals using a syntax-directed translation scheme. How can this be completed? I would consider parsing from right-to-left. First, I would map the units column: 0 -> '' 1 -> 'I' 2 -> 'II' 3 -> 'III' 4 -> 'IV' ... 9 -> 'IX' Then, if there was a second column (e.g. second from the right = tens column), I would use that to map to 0 -> '' 1 -> 'X' 2 -> 'XX' ... 9 -> 'XC' That would need to be prepended to the initial output. Repeat for next columns (hundreds, thousands) until you run out of letters. Double-check the number isn't

How to convert a Roman numeral to integer in PHP?

故事扮演 提交于 2019-11-28 18:18:05
Using PHP, I'd like to convert a string containing a Roman number into its integer representation. I need this because I need to make calculations on them. Wikipedia on Roman numerals It would suffice to only recognize the basic Roman numeral characters, like: $roman_values=array( 'I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000, ); That means the highest possible number is 3999 (MMMCMXCIX). I will use N to represent zero, other than that only positive integers are supported. I cannot use the PEAR library for Roman numbers. I found this great question on SO on how

Convert roman numerals to numbers in R

你离开我真会死。 提交于 2019-11-28 10:46:10
In R, there is a great function as.roman in the very base setup: as.roman(79) # [1] LXXIX Is there an inverse function that would convert roman numerals to numbers? (I know I can write it myself but I prefer to use already prepared or preferably standard functions, unfortunatelly cannot find one. Standard library or package function is a prefered solution) as.roman() returns an object of class roman , so R recognizes it as such. You can directly turn it back into an Arabic numeral with as.numeric() . If you have a string that meets the criteria such that it could be a valid roman numeral, you

Code Golf New Year Edition - Integer to Roman Numeral

心已入冬 提交于 2019-11-28 09:05:12
Write a program that take a single command line argument N and prints out the corresponding Roman Numeral . Eg N = 2009 should print MMIX. Let's say this should work for 0 < N < 3000. (Had fun playing my first ever round of code golf with the Christmas edition , and thought this could fit for New Year. Googled to see if this has come up before elsewhere and it looks like it hasn't, but let me know if this is too hard or too easy or if the rules need changing. ) Happy MMIX! A. Rex Perl: 69 strokes (count 'em!) Sixty-nine strokes including calling perl in the first place: $ perl -ple's!.!($#.=5x