Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
42
Task/Roman-numerals-Decode/CLU/roman-numerals-decode.clu
Normal file
42
Task/Roman-numerals-Decode/CLU/roman-numerals-decode.clu
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
roman = cluster is decode
|
||||
rep = null
|
||||
|
||||
digit_value = proc (c: char) returns (int) signals (invalid)
|
||||
if c < 'a' then c := char$i2c(char$c2i(c) + 32) end
|
||||
if c = 'm' then return(1000)
|
||||
elseif c = 'd' then return(500)
|
||||
elseif c = 'c' then return(100)
|
||||
elseif c = 'l' then return(50)
|
||||
elseif c = 'x' then return(10)
|
||||
elseif c = 'v' then return(5)
|
||||
elseif c = 'i' then return(1)
|
||||
else signal invalid
|
||||
end
|
||||
end digit_value
|
||||
|
||||
decode = proc (s: string) returns (int) signals (invalid)
|
||||
acc: int := 0
|
||||
for i: int in int$from_to(1, string$size(s)) do
|
||||
d: int := digit_value(s[i])
|
||||
if i < string$size(s) cand d < digit_value(s[i+1]) then
|
||||
acc := acc - d
|
||||
else
|
||||
acc := acc + d
|
||||
end
|
||||
end resignal invalid
|
||||
return(acc)
|
||||
end decode
|
||||
end roman
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
tests: array[string] := array[string]$
|
||||
["MCMXC", "mdclxvi", "MmViI", "mmXXi", "INVALID"]
|
||||
|
||||
for test: string in array[string]$elements(tests) do
|
||||
stream$puts(po, test || ": ")
|
||||
stream$putl(po, int$unparse(roman$decode(test))) except when invalid:
|
||||
stream$putl(po, "not a valid Roman numeral!")
|
||||
end
|
||||
end
|
||||
end start_up
|
||||
Loading…
Add table
Add a link
Reference in a new issue