Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,151 +0,0 @@
|
|||
Pragma Ada_2012;
|
||||
Pragma Assertion_Policy( Check );
|
||||
|
||||
With
|
||||
Unchecked_Conversion,
|
||||
Ada.Text_IO;
|
||||
|
||||
Procedure Test_Roman_Numerals is
|
||||
|
||||
-- We create an enumeration of valid characters, note that they are
|
||||
-- character-literals, this is so that we can use literal-strings,
|
||||
-- and that their size is that of Integer.
|
||||
Type Roman_Digits is ('I', 'V', 'X', 'L', 'C', 'D', 'M' )
|
||||
with Size => Integer'Size;
|
||||
|
||||
-- We use a representation-clause ensure the proper integral-value
|
||||
-- of each individual character.
|
||||
For Roman_Digits use
|
||||
(
|
||||
'I' => 1,
|
||||
'V' => 5,
|
||||
'X' => 10,
|
||||
'L' => 50,
|
||||
'C' => 100,
|
||||
'D' => 500,
|
||||
'M' => 1000
|
||||
);
|
||||
|
||||
-- To convert a Roman_Digit to an integer, we now only need to
|
||||
-- read its value as an integer.
|
||||
Function Convert is new Unchecked_Conversion
|
||||
( Source => Roman_Digits, Target => Integer );
|
||||
|
||||
-- Romena_Numeral is a string of Roman_Digit.
|
||||
Type Roman_Numeral is array (Positive range <>) of Roman_Digits;
|
||||
|
||||
-- The Numeral_List type is used herein only for testing
|
||||
-- and verification-data.
|
||||
Type Numeral_List is array (Positive range <>) of
|
||||
not null access Roman_Numeral;
|
||||
|
||||
-- The Test_Cases subtype ensures that Test_Data and Validation_Data
|
||||
-- both contain the same number of elements, and that the indecies
|
||||
-- are the same; essentially the same as:
|
||||
--
|
||||
-- pragma Assert( Test_Data'Length = Validation_Data'Length
|
||||
-- AND Test_Data'First = Validation_Data'First);
|
||||
|
||||
subtype Test_Cases is Positive range 1..14;
|
||||
|
||||
Test_Data : constant Numeral_List(Test_Cases):=
|
||||
(
|
||||
New Roman_Numeral'("III"), -- 3
|
||||
New Roman_Numeral'("XXX"), -- 30
|
||||
New Roman_Numeral'("CCC"), -- 300
|
||||
New Roman_Numeral'("MMM"), -- 3000
|
||||
|
||||
New Roman_Numeral'("VII"), -- 7
|
||||
New Roman_Numeral'("LXVI"), -- 66
|
||||
New Roman_Numeral'("CL"), -- 150
|
||||
New Roman_Numeral'("MCC"), -- 1200
|
||||
|
||||
New Roman_Numeral'("IV"), -- 4
|
||||
New Roman_Numeral'("IX"), -- 9
|
||||
New Roman_Numeral'("XC"), -- 90
|
||||
|
||||
New Roman_Numeral'("ICM"), -- 901
|
||||
New Roman_Numeral'("CIM"), -- 899
|
||||
|
||||
New Roman_Numeral'("MDCLXVI") -- 1666
|
||||
);
|
||||
|
||||
Validation_Data : constant array(Test_Cases) of Natural:=
|
||||
( 3, 30, 300, 3000,
|
||||
7, 66, 150, 1200,
|
||||
4, 9, 90,
|
||||
901, 899,
|
||||
1666
|
||||
);
|
||||
|
||||
|
||||
-- In Roman numerals, the subtractive form [IV = 4] was used
|
||||
-- very infrequently, the most common form was the addidive
|
||||
-- form [IV = 6]. (Consider military logistics and squads.)
|
||||
|
||||
-- SUM returns the Number, read in the additive form.
|
||||
Function Sum( Number : Roman_Numeral ) return Natural is
|
||||
begin
|
||||
Return Result : Natural:= 0 do
|
||||
For Item of Number loop
|
||||
Result:= Result + Convert( Item );
|
||||
end loop;
|
||||
End Return;
|
||||
end Sum;
|
||||
|
||||
-- EVAL returns Number read in the subtractive form.
|
||||
Function Eval( Number : Roman_Numeral ) return Natural is
|
||||
Current : Roman_Digits:= 'I';
|
||||
begin
|
||||
Return Result : Natural:= 0 do
|
||||
For Item of Number loop
|
||||
if Current < Item then
|
||||
Result:= Convert(Item) - Result;
|
||||
Current:= Item;
|
||||
else
|
||||
Result:= Result + Convert(Item);
|
||||
end if;
|
||||
end loop;
|
||||
End Return;
|
||||
end Eval;
|
||||
|
||||
-- Display the given Roman_Numeral via Text_IO.
|
||||
Procedure Put( S: Roman_Numeral ) is
|
||||
begin
|
||||
For Ch of S loop
|
||||
declare
|
||||
-- The 'Image attribute returns the character inside
|
||||
-- single-quotes; so we select the character itself.
|
||||
C : Character renames Roman_Digits'Image(Ch)(2);
|
||||
begin
|
||||
Ada.Text_IO.Put( C );
|
||||
end;
|
||||
end loop;
|
||||
end;
|
||||
|
||||
-- This displays pass/fail dependant on the parameter.
|
||||
Function PF ( Value : Boolean ) Return String is
|
||||
begin
|
||||
Return Result : String(1..4):= ( if Value then"pass"else"fail" );
|
||||
End PF;
|
||||
|
||||
Begin
|
||||
Ada.Text_IO.Put_Line("Starting Test:");
|
||||
|
||||
for Index in Test_Data'Range loop
|
||||
declare
|
||||
Item : Roman_Numeral renames Test_Data(Index).all;
|
||||
Value : constant Natural := Eval(Item);
|
||||
begin
|
||||
Put( Item );
|
||||
|
||||
Ada.Text_IO.Put( ASCII.HT & "= ");
|
||||
Ada.Text_IO.Put( Value'Img );
|
||||
Ada.Text_IO.Put_Line( ASCII.HT & '[' &
|
||||
PF( Value = Validation_Data(Index) )& ']');
|
||||
end;
|
||||
end loop;
|
||||
|
||||
|
||||
Ada.Text_IO.Put_Line("Testing complete.");
|
||||
End Test_Roman_Numerals;
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
syms: #[ M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 ]
|
||||
|
||||
fromRoman: function [roman][
|
||||
ret: 0
|
||||
loop 0..(size roman)-2 'ch [
|
||||
fst: roman\[ch]
|
||||
snd: roman\[ch+1]
|
||||
valueA: syms\[fst]
|
||||
valueB: syms\[snd]
|
||||
ret: 0
|
||||
loop 0..(size roman)-2 'ch [
|
||||
fst: roman\[ch]
|
||||
snd: roman\[ch+1]
|
||||
valueA: syms\[fst]
|
||||
valueB: syms\[snd]
|
||||
|
||||
if? valueA < valueB -> ret: ret - valueA
|
||||
else -> ret: ret + valueA
|
||||
]
|
||||
return ret + syms\[last roman]
|
||||
switch valueA < valueB -> ret: ret - valueA
|
||||
-> ret: ret + valueA
|
||||
]
|
||||
return ret + syms\[last roman]
|
||||
]
|
||||
|
||||
loop ["MCMXC" "MMVIII" "MDCLXVI"] 'r -> print [r "->" fromRoman r]
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace Roman
|
|||
case 'D': return 500;
|
||||
case 'M': return 1000;
|
||||
}
|
||||
throw exception("Invalid character");
|
||||
throw invalid_argument("Invalid character");
|
||||
}
|
||||
|
||||
int ToInt(const string& s)
|
||||
|
|
|
|||
|
|
@ -1,75 +0,0 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. UNROMAN.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 filler.
|
||||
03 i pic 9(02) comp.
|
||||
03 j pic 9(02) comp.
|
||||
03 k pic 9(02) comp.
|
||||
03 l pic 9(02) comp.
|
||||
01 inp-roman.
|
||||
03 inp-rom-ch pic x(01) occurs 20 times.
|
||||
01 inp-roman-digits.
|
||||
03 inp-rom-digit pic 9(01) occurs 20 times.
|
||||
01 ws-search-idx pic 9(02) comp.
|
||||
01 ws-tbl-table-def.
|
||||
03 filler pic x(05) value '1000M'.
|
||||
03 filler pic x(05) value '0500D'.
|
||||
03 filler pic x(05) value '0100C'.
|
||||
03 filler pic x(05) value '0050L'.
|
||||
03 filler pic x(05) value '0010X'.
|
||||
03 filler pic x(05) value '0005V'.
|
||||
03 filler pic x(05) value '0001I'.
|
||||
01 filler redefines ws-tbl-table-def.
|
||||
03 ws-tbl-roman occurs 07 times indexed by rx.
|
||||
05 ws-tbl-rom-val pic 9(04).
|
||||
05 ws-tbl-rom-ch pic x(01).
|
||||
01 ws-number pic s9(05) value 0.
|
||||
01 ws-number-pic pic zzzz9-.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
accept inp-roman
|
||||
perform
|
||||
until inp-roman = ' '
|
||||
move zeroes to inp-roman-digits
|
||||
perform
|
||||
varying i from 1 by +1 until inp-rom-ch (i) = ' '
|
||||
set rx to 1
|
||||
search ws-tbl-roman
|
||||
at end
|
||||
move 0 to inp-rom-digit (i)
|
||||
when ws-tbl-rom-ch (rx) = inp-rom-ch (i)
|
||||
set inp-rom-digit (i) to rx
|
||||
end-search
|
||||
end-perform
|
||||
compute l = i - 1
|
||||
move 0 to ws-number
|
||||
perform
|
||||
varying i from 1 by +1
|
||||
until i > l or inp-rom-digit (i) = 0
|
||||
compute j = inp-rom-digit (i)
|
||||
compute k = inp-rom-digit (i + 1)
|
||||
if ws-tbl-rom-val (k)
|
||||
> ws-tbl-rom-val (j)
|
||||
compute ws-number
|
||||
= ws-number
|
||||
- ws-tbl-rom-val (j)
|
||||
else
|
||||
compute ws-number
|
||||
= ws-number
|
||||
+ ws-tbl-rom-val (j)
|
||||
end-if
|
||||
end-perform
|
||||
move ws-number to ws-number-pic
|
||||
display '----------'
|
||||
display 'roman=' inp-roman
|
||||
display 'arabic=' ws-number-pic
|
||||
if i < l or ws-number = 0
|
||||
display 'invalid/incomplete roman numeral at pos 'i
|
||||
' found ' inp-rom-ch (i)
|
||||
end-if
|
||||
accept inp-roman
|
||||
end-perform
|
||||
stop run
|
||||
.
|
||||
END PROGRAM UNROMAN.
|
||||
|
|
@ -38,9 +38,9 @@ extension op : String
|
|||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
public Program()
|
||||
{
|
||||
console.printLine("MCMXC: ", "MCMXC".toRomanInt());
|
||||
console.printLine("MMVIII: ", "MMVIII".toRomanInt());
|
||||
console.printLine("MDCLXVI:", "MDCLXVI".toRomanInt())
|
||||
Console.printLine("MCMXC: ", "MCMXC".toRomanInt());
|
||||
Console.printLine("MMVIII: ", "MMVIII".toRomanInt());
|
||||
Console.printLine("MDCLXVI:", "MDCLXVI".toRomanInt())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,6 @@ defmodule Roman_numeral do
|
|||
defp to_value(?I), do: 1
|
||||
end
|
||||
|
||||
Enum.each(['MCMXC', 'MMVIII', 'MDCLXVI', 'IIIID'], fn clist ->
|
||||
IO.puts "#{clist}\t: #{Roman_numeral.decode(clist)}"
|
||||
Enum.each(["MCMXC", "MMVIII", "MDCLXVI", "IIIID"], fn clist ->
|
||||
IO.puts "#{clist}\t: #{Roman_numeral.decode(to_charlist(clist))}"
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
(defun ro2ar (RN)
|
||||
"Translate a roman number RN into arabic number.
|
||||
Its argument RN is wether a symbol, wether a list.
|
||||
Returns the arabic number. (ro2ar 'C) gives 100,
|
||||
(ro2ar '(X X I V)) gives 24"
|
||||
(cond
|
||||
((eq RN 'M) 1000)
|
||||
((eq RN 'D) 500)
|
||||
((eq RN 'C) 100)
|
||||
((eq RN 'L) 50)
|
||||
((eq RN 'X) 10)
|
||||
((eq RN 'V) 5)
|
||||
((eq RN 'I) 1)
|
||||
((null (cdr RN)) (ro2ar (car RN))) ;; stop recursion
|
||||
((< (ro2ar (car RN)) (ro2ar (car (cdr RN)))) (- (ro2ar (cdr RN)) (ro2ar (car RN)))) ;; "IV" -> 5-1=4
|
||||
(t (+ (ro2ar (car RN)) (ro2ar (cdr RN)))))) ;; "VI" -> 5+1=6
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
constant symbols = "MDCLXVI", weights = {1000,500,100,50,10,5,1}
|
||||
function romanDec(sequence roman)
|
||||
integer n, lastval, arabic
|
||||
lastval = 0
|
||||
arabic = 0
|
||||
for i = length(roman) to 1 by -1 do
|
||||
n = find(roman[i],symbols)
|
||||
if n then
|
||||
n = weights[n]
|
||||
end if
|
||||
if n < lastval then
|
||||
arabic -= n
|
||||
else
|
||||
arabic += n
|
||||
end if
|
||||
lastval = n
|
||||
end for
|
||||
return arabic
|
||||
end function
|
||||
|
||||
? romanDec("MCMXCIX")
|
||||
? romanDec("MDCLXVI")
|
||||
? romanDec("XXV")
|
||||
? romanDec("CMLIV")
|
||||
? romanDec("MMXI")
|
||||
|
|
@ -4,5 +4,5 @@ test = ["I", "III", "IX", "IVI", "IIM",
|
|||
"CMMDXL", "icv", "cDxLiV", "MCMLD", "ccccccd",
|
||||
"iiiiiv", "MMXV", "MCMLXXXIV", "ivxmm", "SPQR"]
|
||||
for rnum in test
|
||||
@printf("%15s → %s\n", rnum, try parseroman(rnum) catch "not valid" end)
|
||||
@printf("%15s → %s\n", rnum, try parseroman(rnum) catch y "not valid" end)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
Filter FromRoman {
|
||||
$output = 0
|
||||
|
||||
if ($_ -notmatch '^(M{1,3}|)(CM|CD|D?C{0,3}|)(XC|XL|L?X{0,3}|)(IX|IV|V?I{0,3}|)$') {
|
||||
throw 'Incorrect format'
|
||||
}
|
||||
|
||||
$current = 1000
|
||||
$subtractor = 'M'
|
||||
$whole = $False
|
||||
$roman = $_
|
||||
'C','D','X','L','I','V',' ' `
|
||||
| %{
|
||||
if ($whole = !$whole) {
|
||||
$current /= 10
|
||||
$subtractor = $_ + $subtractor[0]
|
||||
$_ = $subtractor[1]
|
||||
}
|
||||
else {
|
||||
$subtractor = $subtractor[0] + $_
|
||||
}
|
||||
|
||||
if ($roman -match $subtractor) {
|
||||
$output += $current * (4,9)[$whole]
|
||||
$roman = $roman -replace $subtractor,''
|
||||
}
|
||||
if ($roman -match ($_ + '{1,3}')) {
|
||||
$output += $current * (5,10)[$whole] * $Matches[0].Length
|
||||
}
|
||||
}
|
||||
|
||||
$output
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
'XIX','IV','','MMCDLXXIX','MMMI' | FromRoman
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
from functools import reduce
|
||||
numerals = { 'M' : 1000, 'D' : 500, 'C' : 100, 'L' : 50, 'X' : 10, 'V' : 5, 'I' : 1 }
|
||||
def romannumeral2number(s):
|
||||
return reduce(lambda x, y: -x + y if x < y else x + y, map(lambda x: numerals.get(x, 0), s.upper()))
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
proc fromRoman rnum {
|
||||
set map {M 1000+ CM 900+ D 500+ CD 400+ C 100+ XC 90+ L 50+ XL 40+ X 10+ IX 9+ V 5+ IV 4+ I 1+}
|
||||
expr [string map $map $rnum]0}
|
||||
expr [string map $map $rnum]0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
' Roman numerals Encode - Visual Basic - 18/04/2019
|
||||
|
||||
Function toRoman(ByVal value)
|
||||
Dim arabic
|
||||
Dim roman
|
||||
Dim i, result
|
||||
arabic = Array(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
|
||||
roman = Array("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I")
|
||||
For i = 0 To 12
|
||||
Do While value >= arabic(i)
|
||||
result = result + roman(i)
|
||||
value = value - arabic(i)
|
||||
Loop
|
||||
Next 'i
|
||||
toRoman = result
|
||||
End Function 'toRoman
|
||||
|
||||
n=InputBox("Number, please","Roman numerals/Encode")
|
||||
code=MsgBox(n & vbCrlf & toRoman(n),vbOKOnly+vbExclamation,"Roman numerals/Encode")
|
||||
If code=vbOK Then ok=1
|
||||
Loading…
Add table
Add a link
Reference in a new issue