Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,32 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
set cnt=0&for %%A in (1000,900,500,400,100,90,50,40,10,9,5,4,1) do (set arab!cnt!=%%A&set /a cnt+=1)
|
||||
set cnt=0&for %%R in (M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I) do (set rom!cnt!=%%R&set /a cnt+=1)
|
||||
|
||||
::Testing
|
||||
call :toRoman 2009
|
||||
echo 2009 = !result!
|
||||
call :toRoman 1666
|
||||
echo 1666 = !result!
|
||||
call :toRoman 3888
|
||||
echo 3888 = !result!
|
||||
pause>nul
|
||||
exit/b 0
|
||||
|
||||
::The "function"...
|
||||
:toRoman
|
||||
set value=%1
|
||||
set result=
|
||||
|
||||
for /l %%i in (0,1,12) do (
|
||||
set a=%%i
|
||||
call :add_val
|
||||
)
|
||||
goto :EOF
|
||||
|
||||
:add_val
|
||||
if !value! lss !arab%a%! goto :EOF
|
||||
set result=!result!!rom%a%!
|
||||
set /a value-=!arab%a%!
|
||||
goto add_val
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
&>0\0>00p:#v_$ >:#,_ $ @
|
||||
4-v >5+#:/#<\55+%:5/\5%:
|
||||
vv_$9+00g+5g\00g8+>5g\00
|
||||
g>\20p>:10p00g \#v _20gv
|
||||
> 2+ v^-1g01\g5+8<^ +9 _
|
||||
IVXLCDM
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
defmodule Roman_numeral do
|
||||
def encode(0), do: ''
|
||||
def encode(x) when x >= 1000, do: [?M | encode(x - 1000)]
|
||||
def encode(x) when x >= 100, do: digit(div(x,100), ?C, ?D, ?M) ++ encode(rem(x,100))
|
||||
def encode(x) when x >= 10, do: digit(div(x,10), ?X, ?L, ?C) ++ encode(rem(x,10))
|
||||
def encode(x) when x >= 1, do: digit(x, ?I, ?V, ?X)
|
||||
|
||||
defp digit(1, x, _, _), do: [x]
|
||||
defp digit(2, x, _, _), do: [x, x]
|
||||
defp digit(3, x, _, _), do: [x, x, x]
|
||||
defp digit(4, x, y, _), do: [x, y]
|
||||
defp digit(5, _, y, _), do: [y]
|
||||
defp digit(6, x, y, _), do: [y, x]
|
||||
defp digit(7, x, y, _), do: [y, x, x]
|
||||
defp digit(8, x, y, _), do: [y, x, x, x]
|
||||
defp digit(9, x, _, z), do: [x, z]
|
||||
end
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
defmodule Roman_numeral do
|
||||
@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 encode(num) do
|
||||
{roman,_} = Enum.reduce(@symbols, {[], num}, fn {divisor, letter}, {memo, n} ->
|
||||
{memo ++ List.duplicate(letter, div(n, divisor)), rem(n, divisor)}
|
||||
end)
|
||||
Enum.join(roman)
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Enum.each([1990, 2008, 1666], fn n ->
|
||||
IO.puts "#{n}: #{Roman_numeral.encode(n)}"
|
||||
end)
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
function roman(strIntegers) {
|
||||
'use strict';
|
||||
// DICTIONARY OF GLYPH:VALUE MAPPINGS
|
||||
var dctGlyphs = {
|
||||
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
|
||||
};
|
||||
|
||||
// LIST OF INTEGER STRINGS, WITH ANY SEPARATOR
|
||||
var strNums = typeof strIntegers === 'string' ? strIntegers : strIntegers.toString(),
|
||||
lstParts = strNums.split(/\d+/),
|
||||
strSeparator = lstParts.length > 1 ? lstParts[1] : '',
|
||||
lstDecimal = strSeparator ? strIntegers.split(strSeparator) : [strNums];
|
||||
|
||||
|
||||
// REWRITE OF DECIMAL INTEGER AS ROMAN
|
||||
function rewrite(strN) {
|
||||
var n = Number(strN);
|
||||
|
||||
/* Starting with the highest-valued glyph:
|
||||
take as many bites as we can with it
|
||||
(decrementing residual value with each bite,
|
||||
and appending a corresponding glyph copy to the string)
|
||||
before moving down to the next most expensive glyph */
|
||||
|
||||
// return Object.keys(dctGlyphs).reduce(
|
||||
// OR:
|
||||
return 'M CM D CD C XC L XL X IX V IV I'.split(' ').reduce(
|
||||
function (s, k) {
|
||||
var v = dctGlyphs[k];
|
||||
return n >= v ? (n -= v, s + k) : s;
|
||||
}, ''
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
// ALL REWRITTEN, WITH SEPARATOR RESTORED
|
||||
return lstDecimal.map(rewrite).join(strSeparator);
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
roman(1999);
|
||||
// --> "MCMXCIX"
|
||||
|
||||
[1990, 2008, "14.09.2015", 2000, 1666].map(roman);
|
||||
// --> ["MCMXC", "MCMCVI", "XIV.IX.MCMCXV", "MCMC", "MDCLXVI"]
|
||||
35
Task/Roman-numerals-Encode/Julia/roman-numerals-encode.julia
Normal file
35
Task/Roman-numerals-Encode/Julia/roman-numerals-encode.julia
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
function romanencode(n::Integer)
|
||||
const DR = [["I", "X", "C", "M"] ["V", "L", "D", "MMM"]]
|
||||
rnum = ""
|
||||
if n > 4999 || n < 1
|
||||
throw(DomainError())
|
||||
end
|
||||
for (omag, d) in enumerate(digits(n))
|
||||
if d == 0
|
||||
omr = ""
|
||||
elseif d < 4
|
||||
omr = DR[omag, 1]^d
|
||||
elseif d == 4
|
||||
omr = DR[omag, 1]*DR[omag, 2]
|
||||
elseif d == 5
|
||||
omr = DR[omag, 2]
|
||||
elseif d < 9
|
||||
omr = DR[omag, 2]*(DR[omag, 1]^(d - 5))
|
||||
else
|
||||
omr = DR[omag, 1]*DR[(omag +1), 1]
|
||||
end
|
||||
rnum = omr*rnum
|
||||
end
|
||||
return rnum
|
||||
end
|
||||
|
||||
testcases = Int64[1990, 2008, 1668]
|
||||
for i in 1:12
|
||||
push!(testcases, rand(1:4999))
|
||||
end
|
||||
testcases = unique(testcases)
|
||||
|
||||
println("Test romanencode, arabic => roman:")
|
||||
for i in testcases
|
||||
println(i, " => ", romanencode(i))
|
||||
end
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
function ConvertTo-RomanNumeral
|
||||
{
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Converts a number to a Roman numeral.
|
||||
.DESCRIPTION
|
||||
Converts a number - in the range of 1 to 3,999 - to a Roman numeral.
|
||||
.PARAMETER Number
|
||||
An integer in the range 1 to 3,999.
|
||||
.INPUTS
|
||||
System.Int32
|
||||
.OUTPUTS
|
||||
System.String
|
||||
.EXAMPLE
|
||||
ConvertTo-RomanNumeral -Number (Get-Date).Year
|
||||
.EXAMPLE
|
||||
(Get-Date).Year | ConvertTo-RomanNumeral
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
[OutputType([string])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
HelpMessage="Enter an integer in the range 1 to 3,999",
|
||||
ValueFromPipeline=$true,
|
||||
Position=0)]
|
||||
[ValidateRange(1,3999)]
|
||||
[int]
|
||||
$Number
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$DecimalToRoman = @{
|
||||
Thousands = "","M","MM","MMM"
|
||||
Hundreds = "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"
|
||||
Tens = "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"
|
||||
Ones = "","I","II","III","IV","V","VI","VII","VIII","IX"
|
||||
}
|
||||
|
||||
$column = @{
|
||||
Thousands = 0
|
||||
Hundreds = 1
|
||||
Tens = 2
|
||||
Ones = 3
|
||||
}
|
||||
}
|
||||
Process
|
||||
{
|
||||
[int[]]$digits = $Number.ToString().PadLeft(4,"0").ToCharArray() |
|
||||
ForEach-Object { [Char]::GetNumericValue($_) }
|
||||
|
||||
$RomanNumeral = ""
|
||||
$RomanNumeral += $DecimalToRoman.Thousands[$digits[$column.Thousands]]
|
||||
$RomanNumeral += $DecimalToRoman.Hundreds[$digits[$column.Hundreds]]
|
||||
$RomanNumeral += $DecimalToRoman.Tens[$digits[$column.Tens]]
|
||||
$RomanNumeral += $DecimalToRoman.Ones[$digits[$column.Ones]]
|
||||
|
||||
$RomanNumeral
|
||||
}
|
||||
End
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +1,39 @@
|
|||
struct RomanNumeral {
|
||||
symbol: &'static str,
|
||||
value: uint
|
||||
symbol: &'static str,
|
||||
value: u32
|
||||
}
|
||||
|
||||
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}
|
||||
const 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_roman(num: uint) -> String {
|
||||
for numeral in NUMERALS.iter() {
|
||||
if num >= numeral.value {
|
||||
return numeral.symbol.to_string() + to_roman(num - numeral.value);
|
||||
fn to_roman(mut number: u32) -> String {
|
||||
let mut min_numeral = String::new();
|
||||
for numeral in NUMERALS.iter() {
|
||||
while numeral.value <= number {
|
||||
min_numeral = min_numeral + numeral.symbol;
|
||||
number -= numeral.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "".to_string();
|
||||
min_numeral
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let nums = [2014, 1999, 25, 1666, 3888];
|
||||
for n in nums.iter() {
|
||||
println!("{:u} = {:s}", *n, to_roman(*n));
|
||||
}
|
||||
let nums = [2014, 1999, 25, 1666, 3888];
|
||||
for &n in nums.iter() {
|
||||
// 4 is minimum printing width, for alignment
|
||||
println!("{:2$} = {}", n, to_roman(n), 4);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue