Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,3 +1,12 @@
|
|||
Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.
|
||||
{{omit from|GUISS}}
|
||||
|
||||
Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
|
||||
Create a function taking a positive integer as its parameter
|
||||
and returning a string containing the Roman Numeral representation
|
||||
of that integer.
|
||||
|
||||
Modern Roman numerals are written by expressing each digit separately,
|
||||
starting with the left most digit and skipping any digit with a value of zero.
|
||||
|
||||
In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. <br>
|
||||
2008 is written as 2000=MM, 8=VIII; or MMVIII. <br>
|
||||
1666 uses each Roman symbol in descending order: MDCLXVI.
|
||||
|
|
|
|||
3
Task/Roman-numerals-Encode/00META.yaml
Normal file
3
Task/Roman-numerals-Encode/00META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
category:
|
||||
- String_manipulation
|
||||
|
|
@ -1,31 +1,17 @@
|
|||
(def arabic-roman-map
|
||||
{1 "I", 5 "V",
|
||||
10 "X", 50 "L",
|
||||
100 "C", 500 "D",
|
||||
1000 "M",
|
||||
4 "IV", 9 "IX",
|
||||
40 "XL", 90 "XC",
|
||||
400 "CD", 900 "CM" })
|
||||
(def roman-map
|
||||
(sorted-map
|
||||
1 "I", 4 "IV", 5 "V", 9 "IX",
|
||||
10 "X", 40 "XL", 50 "L", 90 "XC",
|
||||
100 "C", 400 "CD", 500 "D", 900 "CM"
|
||||
1000 "M"))
|
||||
|
||||
(def arabic-roman-map-sorted-keys
|
||||
(sort (keys arabic-roman-map)))
|
||||
(defn int->roman [n]
|
||||
{:pre (integer? n)}
|
||||
(loop [res (StringBuilder.), n n]
|
||||
(if-let [v (roman-map n)]
|
||||
(str (.append res v))
|
||||
(let [[k v] (->> roman-map keys (filter #(> n %)) last (find roman-map))]
|
||||
(recur (.append res v) (- n k))))))
|
||||
|
||||
(defn find-value-in-coll
|
||||
[coll k]
|
||||
(let [aval (find coll k)]
|
||||
(if (nil? aval) "" (val aval))))
|
||||
|
||||
(defn to-roman
|
||||
[result n]
|
||||
(let
|
||||
[closest-key-for-n (last (filter #(> n %) arabic-roman-map-sorted-keys))
|
||||
roman-value-for-n (find-value-in-coll arabic-roman-map n)
|
||||
roman-value-for-closet-to-n (find-value-in-coll arabic-roman-map
|
||||
closest-key-for-n)]
|
||||
(if (or (<= n 0)(contains? arabic-roman-map n))
|
||||
(conj result roman-value-for-n)
|
||||
(recur (conj result roman-value-for-closet-to-n)
|
||||
(- n closest-key-for-n)))))
|
||||
|
||||
Usage: >(to-roman [] 1999)
|
||||
result: ["M" "CM" "XC" "IX"]
|
||||
(int->roman 1999)
|
||||
; "MCMXCIX"
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
(defn a2r
|
||||
[a]
|
||||
(let [rv [1000 500 100 50 10 5 1]
|
||||
(defn a2r [a]
|
||||
(let [rv '(1000 500 100 50 10 5 1)
|
||||
rm (zipmap rv "MDCLXVI")
|
||||
dv (->> rv (take-nth 2) next (#(interleave % %)))]
|
||||
dv (->> rv (take-nth 2) next #(interleave % %))]
|
||||
(loop [a a rv rv dv dv r nil]
|
||||
(if (<= a 0)
|
||||
r
|
||||
|
|
|
|||
59
Task/Roman-numerals-Encode/Eiffel/roman-numerals-encode.e
Normal file
59
Task/Roman-numerals-Encode/Eiffel/roman-numerals-encode.e
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
local
|
||||
numbers: ARRAY [INTEGER]
|
||||
do
|
||||
numbers := <<1990, 2008, 1666, 3159, 1977, 2010>>
|
||||
-- "MCMXC", "MMVIII", "MDCLXVI", "MMMCLIX", "MCMLXXVII", "MMX"
|
||||
across numbers as n loop
|
||||
print (n.item.out + " in decimal Arabic numerals is " +
|
||||
decimal_to_roman (n.item) + " in Roman numerals.%N")
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Roman numerals
|
||||
|
||||
decimal_to_roman (a_int: INTEGER): STRING
|
||||
-- Representation of integer `a_int' as Roman numeral
|
||||
require
|
||||
a_int > 0
|
||||
local
|
||||
dnums: ARRAY[INTEGER]
|
||||
rnums: ARRAY[STRING]
|
||||
|
||||
dnum: INTEGER
|
||||
rnum: STRING
|
||||
|
||||
i: INTEGER
|
||||
do
|
||||
dnums := <<1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1>>
|
||||
rnums := <<"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I">>
|
||||
|
||||
dnum := a_int
|
||||
rnum := ""
|
||||
|
||||
from
|
||||
i := 1
|
||||
until
|
||||
i > dnums.count or dnum <= 0
|
||||
loop
|
||||
from
|
||||
until
|
||||
dnum < dnums[i]
|
||||
loop
|
||||
dnum := dnum - dnums[i]
|
||||
rnum := rnum + rnums[i]
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
|
||||
Result := rnum
|
||||
end
|
||||
end
|
||||
13
Task/Roman-numerals-Encode/Haskell/roman-numerals-encode.hs
Normal file
13
Task/Roman-numerals-Encode/Haskell/roman-numerals-encode.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
digit x y z k =
|
||||
[[x],[x,x],[x,x,x],[x,y],[y],[y,x],[y,x,x],[y,x,x,x],[x,z]] !!
|
||||
(fromInteger k - 1)
|
||||
|
||||
toRoman :: Integer -> String
|
||||
toRoman 0 = ""
|
||||
toRoman x | x < 0 = error "Negative roman numeral"
|
||||
toRoman x | x >= 1000 = 'M' : toRoman (x - 1000)
|
||||
toRoman x | x >= 100 = digit 'C' 'D' 'M' q ++ toRoman r where
|
||||
(q,r) = x `divMod` 100
|
||||
toRoman x | x >= 10 = digit 'X' 'L' 'C' q ++ toRoman r where
|
||||
(q,r) = x `divMod` 10
|
||||
toRoman x = digit 'I' 'V' 'X' x
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
> for n in [ 1666, 1990, 2008 ] do printf( "%d\t%s\n", n, convert( n, 'roman' ) ) end:
|
||||
1666 MDCLXVI
|
||||
1990 MCMXC
|
||||
2008 MMVIII
|
||||
|
|
@ -1,3 +1,14 @@
|
|||
per quisque in I tum C conscribementum sic
|
||||
hoc tum duos multiplicamentum comementum egresso scribe.
|
||||
cis
|
||||
my @symbols = ( [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] );
|
||||
|
||||
sub roman {
|
||||
my($n, $r) = (shift, '');
|
||||
($r, $n) = ('-', -$n) if $n < 0; # Optional handling of negative input
|
||||
foreach my $s (@symbols) {
|
||||
my($arabic, $roman) = @$s;
|
||||
($r, $n) = ($r .= $roman x int($n/$arabic), $n % $arabic)
|
||||
if $n >= $arabic;
|
||||
}
|
||||
$r;
|
||||
}
|
||||
|
||||
say roman($_) for 1..2012;
|
||||
|
|
|
|||
|
|
@ -1,25 +1,2 @@
|
|||
use v5.12;
|
||||
use Sub::SmartMatch;
|
||||
use SmartMatch::Sugar qw(any);
|
||||
use List::MoreUtils qw( natatime );
|
||||
|
||||
my %symbols = (
|
||||
1 => "I", 5 => "V", 10 => "X", 50 => "L", 100 => "C",
|
||||
500 => "D", 1_000 => "M"
|
||||
);
|
||||
|
||||
my @subtractors = (
|
||||
1_000, 100, 500, 100, 100, 10, 50, 10, 10, 1, 5, 1, 1, 0
|
||||
);
|
||||
|
||||
multi roman => [0], sub { '' };
|
||||
multi roman => any, sub {
|
||||
my $n = shift;
|
||||
my $iter = natatime 2, @subtractors;
|
||||
while( my ($cut, $minus) = $iter->() ) {
|
||||
$n >= $cut
|
||||
and return $symbols{$cut} . roman($n - $cut);
|
||||
$n >= $cut - $minus
|
||||
and return $symbols{$minus} . roman($n + $minus);
|
||||
}
|
||||
};
|
||||
use Math::Roman qw/roman/;
|
||||
say roman($_) for 1..2012'
|
||||
|
|
|
|||
|
|
@ -1 +1,3 @@
|
|||
say roman($_) for 1..2_012;
|
||||
per quisque in I tum C conscribementum sic
|
||||
hoc tum duos multiplicamentum comementum egresso scribe.
|
||||
cis
|
||||
|
|
|
|||
27
Task/Roman-numerals-Encode/Perl/roman-numerals-encode-4.pl
Normal file
27
Task/Roman-numerals-Encode/Perl/roman-numerals-encode-4.pl
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use v5.12;
|
||||
use Sub::SmartMatch;
|
||||
use SmartMatch::Sugar qw(any);
|
||||
use List::MoreUtils qw( natatime );
|
||||
|
||||
my %symbols = (
|
||||
1 => "I", 5 => "V", 10 => "X", 50 => "L", 100 => "C",
|
||||
500 => "D", 1_000 => "M"
|
||||
);
|
||||
|
||||
my @subtractors = (
|
||||
1_000, 100, 500, 100, 100, 10, 50, 10, 10, 1, 5, 1, 1, 0
|
||||
);
|
||||
|
||||
multi roman => [0], sub { '' };
|
||||
multi roman => any, sub {
|
||||
my $n = shift;
|
||||
my $iter = natatime 2, @subtractors;
|
||||
while( my ($cut, $minus) = $iter->() ) {
|
||||
$n >= $cut
|
||||
and return $symbols{$cut} . roman($n - $cut);
|
||||
$n >= $cut - $minus
|
||||
and return $symbols{$minus} . roman($n + $minus);
|
||||
}
|
||||
};
|
||||
|
||||
say roman($_) for 1..2_012;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ]
|
||||
|
||||
def arabic_to_roman(arabic)
|
||||
return '' if arabic.zero?
|
||||
Symbols.each { |arabic_rep, roman_rep| return roman_rep + arabic_to_roman(arabic - arabic_rep) if arabic >= arabic_rep }
|
||||
end
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ]
|
||||
|
||||
def to_roman(num)
|
||||
Symbols.reduce "" do |memo, (divisor, letter)|
|
||||
div, num = num.divmod(divisor)
|
||||
memo + letter * div
|
||||
end
|
||||
end
|
||||
|
|
@ -19,14 +19,14 @@ static NUMERALS: [RomanNumeral, ..13] = [
|
|||
RomanNumeral {symbol: "I", value: 1}
|
||||
];
|
||||
|
||||
fn to_roman(num: uint) -> ~str {
|
||||
fn to_roman(num: uint) -> String {
|
||||
for numeral in NUMERALS.iter() {
|
||||
if num >= numeral.value {
|
||||
return numeral.symbol + to_roman(num - numeral.value);
|
||||
return numeral.symbol.to_string() + to_roman(num - numeral.value);
|
||||
}
|
||||
}
|
||||
|
||||
return ~"";
|
||||
return "".to_string();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
roman() {
|
||||
local values=( 1000 900 500 400 100 90 50 40 10 5 4 1 )
|
||||
local roman=(
|
||||
[1000]=M [900]=CM [500]=D [400]=CD
|
||||
[100]=C [90]=XC [50]=L [40]=XL
|
||||
[10]=X [9]=IX [5]=V [4]=IV
|
||||
[1]=I
|
||||
)
|
||||
local nvmber=""
|
||||
local num=$1
|
||||
for value in ${values[@]}; do
|
||||
while (( num >= value )); do
|
||||
nvmber+=${roman[value]}
|
||||
((num -= value))
|
||||
done
|
||||
done
|
||||
echo $nvmber
|
||||
}
|
||||
|
||||
for test in 1999 24 944 1666 2008; do
|
||||
printf "%d = %s\n" $test $(roman $test)
|
||||
done
|
||||
Loading…
Add table
Add a link
Reference in a new issue