March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,9 +1,8 @@
|
|||
import std.regex, std.algorithm, std.functional;
|
||||
|
||||
alias sum = curry!(reduce!q{a + b}, 0);
|
||||
import std.regex, std.algorithm;
|
||||
|
||||
immutable int[string] w2s;
|
||||
nothrow static this() {
|
||||
|
||||
pure nothrow static this() {
|
||||
w2s = ["IX": 9, "C": 100, "D": 500, "CM": 900, "I": 1,
|
||||
"XC": 90, "M": 1000, "L": 50, "CD": 400, "XL": 40,
|
||||
"V": 5, "X": 10, "IV": 4];
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
:- module test_roman.
|
||||
|
||||
:- interface.
|
||||
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
|
||||
:- import_module char.
|
||||
:- import_module exception.
|
||||
:- import_module int.
|
||||
:- import_module list.
|
||||
:- import_module string.
|
||||
|
||||
:- type conversion_error ---> not_a_roman_number.
|
||||
|
||||
:- func build_int(list(char), int, int) = int.
|
||||
:- func from_roman(string) = int.
|
||||
:- pred roman_to_int(char::in, int::out) is semidet.
|
||||
|
||||
from_roman(Roman) = Decimal :-
|
||||
List = reverse(to_char_list(Roman)),
|
||||
Decimal = build_int(List, 0, 0).
|
||||
|
||||
build_int([], LastValue, Accumulator) = LastValue + Accumulator.
|
||||
build_int([Digit|Rest], LastValue, Accumulator) = Sum :-
|
||||
( roman_to_int(Digit, Value) ->
|
||||
( Value < LastValue ->
|
||||
Sum = build_int(Rest, Value, Accumulator - LastValue)
|
||||
; Sum = build_int(Rest, Value, Accumulator + LastValue) )
|
||||
; throw(not_a_roman_number) ).
|
||||
|
||||
roman_to_int('I', 1).
|
||||
roman_to_int('V', 5).
|
||||
roman_to_int('X', 10).
|
||||
roman_to_int('L', 50).
|
||||
roman_to_int('C', 100).
|
||||
roman_to_int('D', 500).
|
||||
roman_to_int('M', 1000).
|
||||
|
||||
main(!IO) :-
|
||||
command_line_arguments(Args, !IO),
|
||||
foldl((pred(Arg::in, !.IO::di, !:IO::uo) is det :-
|
||||
format("%s => %d\n", [s(Arg), i(from_roman(Arg))], !IO)),
|
||||
Args, !IO).
|
||||
|
||||
:- end_module test_roman.
|
||||
37
Task/Roman-numerals-Decode/Rust/roman-numerals-decode.rust
Normal file
37
Task/Roman-numerals-Decode/Rust/roman-numerals-decode.rust
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
struct RomanNumeral {
|
||||
symbol: &'static str,
|
||||
value: uint
|
||||
}
|
||||
|
||||
static NUMERALS: [RomanNumeral, ..13] = [
|
||||
RomanNumeral {symbol: "M", value: 1000},
|
||||
RomanNumeral {symbol: "CM", value: 900},
|
||||
RomanNumeral {symbol: "D", value: 500},
|
||||
RomanNumeral {symbol: "CD", value: 400},
|
||||
RomanNumeral {symbol: "C", value: 100},
|
||||
RomanNumeral {symbol: "XC", value: 90},
|
||||
RomanNumeral {symbol: "L", value: 50},
|
||||
RomanNumeral {symbol: "XL", value: 40},
|
||||
RomanNumeral {symbol: "X", value: 10},
|
||||
RomanNumeral {symbol: "IX", value: 9},
|
||||
RomanNumeral {symbol: "V", value: 5},
|
||||
RomanNumeral {symbol: "IV", value: 4},
|
||||
RomanNumeral {symbol: "I", value: 1}
|
||||
];
|
||||
|
||||
fn to_hindu(roman: &str) -> uint {
|
||||
for numeral in NUMERALS.iter() {
|
||||
if roman.starts_with(numeral.symbol) {
|
||||
return numeral.value + to_hindu(roman.slice_from(numeral.symbol.len()));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let roms = ["MMXIV", "MCMXCIX", "XXV", "MDCLXVI", "MMMDCCCLXXXVIII"];
|
||||
for r in roms.iter() {
|
||||
println!("{:s} = {:u}", *r, to_hindu(*r));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue