Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
46
Task/Middle-three-digits/Batch-File/middle-three-digits.bat
Normal file
46
Task/Middle-three-digits/Batch-File/middle-three-digits.bat
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
%== Initialization ==%
|
||||
set "numbers=123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0"
|
||||
|
||||
%== The Main Thing ==%
|
||||
for %%N in (%numbers%) do (
|
||||
call :middle3 %%N
|
||||
)
|
||||
echo.
|
||||
pause
|
||||
exit /b 0
|
||||
%==/The Main Thing ==%
|
||||
|
||||
%== The Procedure ==%
|
||||
:middle3
|
||||
|
||||
set str=%1
|
||||
%== Making sure that str is positive ==%
|
||||
if !str! lss 0 set /a str*=-1
|
||||
|
||||
%== Alternative for finding the string length ==%
|
||||
%== But this has a limit of 1000 characters ==%
|
||||
set leng=0&if not "!str!"=="" for /l %%. in (0,1,1000) do if not "!str:~%%.,1!"=="" set /a leng+=1
|
||||
|
||||
if !leng! lss 3 (
|
||||
echo.%~1: [ERROR] Input too small.
|
||||
goto :EOF
|
||||
)
|
||||
|
||||
set /a "test2=leng %% 2,trimmer=(leng - 3) / 2"
|
||||
|
||||
if !test2! equ 0 (
|
||||
echo.%~1: [ERROR] Even number of digits.
|
||||
goto :EOF
|
||||
)
|
||||
|
||||
%== Passed the tests. Now, really find the middle 3 digits... ==%
|
||||
if !trimmer! equ 0 (
|
||||
echo.%~1: !str!
|
||||
) else (
|
||||
echo.%~1: !str:~%trimmer%,-%trimmer%!
|
||||
)
|
||||
goto :EOF
|
||||
%==/The Procedure ==%
|
||||
8
Task/Middle-three-digits/Befunge/middle-three-digits.bf
Normal file
8
Task/Middle-three-digits/Befunge/middle-three-digits.bf
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
>&>:0`2*1-*0>v
|
||||
v+*86%+55:p00<
|
||||
>\55+/:00g1+\|
|
||||
v3_v#*%2\`2::<
|
||||
->@>0".rorrE"v
|
||||
2^,+55_,#!>#:<
|
||||
>/>\#<$#-:#1_v
|
||||
>_@#<,+55,,,$<
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
blsq ) {123 12345 1234567 987654321 -10001 -123}{XX{~-}{L[3.>}w!m]\[}m[uN
|
||||
123
|
||||
234
|
||||
345
|
||||
654
|
||||
000
|
||||
123
|
||||
15
Task/Middle-three-digits/DCL/middle-three-digits.dcl
Normal file
15
Task/Middle-three-digits/DCL/middle-three-digits.dcl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
$ list = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0"
|
||||
$ i = 0
|
||||
$ loop:
|
||||
$ number = f$element( i, ",", list )
|
||||
$ if number .eqs. "," then $ exit
|
||||
$ abs_number = number - "-"
|
||||
$ len = f$length( abs_number )
|
||||
$ if len .lt. 3 .or. .not. len
|
||||
$ then
|
||||
$ write sys$output f$fao( "!9SL: ", f$integer( number )), "has no middle three"
|
||||
$ else
|
||||
$ write sys$output f$fao( "!9SL: ", f$integer( number )), f$extract( ( len - 3 ) / 2, 3, abs_number )
|
||||
$ endif
|
||||
$ i = i + 1
|
||||
$ goto loop
|
||||
61
Task/Middle-three-digits/Eiffel/middle-three-digits.e
Normal file
61
Task/Middle-three-digits/Eiffel/middle-three-digits.e
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
-- Test of middle_three_digits.
|
||||
local
|
||||
test_1, test_2: ARRAY [INTEGER]
|
||||
do
|
||||
test_1 := <<123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345>>
|
||||
test_2 := <<1, 2, -1, -10, 2002, -2002, 0>>
|
||||
across
|
||||
test_1 as t
|
||||
loop
|
||||
io.put_string ("The middle three digits of " + t.item.out + " are: %T ")
|
||||
io.put_string (middle_three_digits (t.item) + "%N")
|
||||
end
|
||||
across
|
||||
test_2 as t
|
||||
loop
|
||||
io.put_string ("The middle three digits of " + t.item.out + " are: %T")
|
||||
io.put_string (middle_three_digits (t.item) + "%N")
|
||||
end
|
||||
end
|
||||
|
||||
middle_three_digits (n: INTEGER): STRING
|
||||
-- The middle three digits of 'n'.
|
||||
local
|
||||
k, i: INTEGER
|
||||
in: STRING
|
||||
do
|
||||
create in.make_empty
|
||||
in := n.out
|
||||
if n < 0 then
|
||||
in.prune ('-')
|
||||
end
|
||||
create Result.make_empty
|
||||
if in.count < 3 then
|
||||
io.put_string (" Not enough digits. ")
|
||||
elseif in.count \\ 2 = 0 then
|
||||
io.put_string (" Even number of digits. ")
|
||||
else
|
||||
i := (in.count - 3) // 2
|
||||
from
|
||||
k := i + 1
|
||||
until
|
||||
k > i + 3
|
||||
loop
|
||||
Result.extend (in.at (k))
|
||||
k := k + 1
|
||||
end
|
||||
end
|
||||
ensure
|
||||
length_is_three: Result.count = 3 or Result.count = 0
|
||||
end
|
||||
|
||||
end
|
||||
30
Task/Middle-three-digits/Elixir/middle-three-digits.elixir
Normal file
30
Task/Middle-three-digits/Elixir/middle-three-digits.elixir
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
defmodule Middle do
|
||||
def three(num) do
|
||||
n = num |> abs |> to_string
|
||||
|
||||
case {n,String.length(n) > 2,even?(n)} do
|
||||
{n, true, false} ->
|
||||
cut(n)
|
||||
{_, false, _} ->
|
||||
raise "Number must have at least three digits"
|
||||
{_, _, true} ->
|
||||
raise "Number must have an odd number of digits"
|
||||
end
|
||||
end
|
||||
|
||||
defp even?(n), do: rem(String.length(n),2) == 0
|
||||
defp cut(n), do: String.slice(n,(div(String.length(n),2) - 1),3)
|
||||
end
|
||||
|
||||
valids = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345]
|
||||
Enum.each(valids, fn n -> :io.format "~10w : ~s~n", [n, Middle.three(n)] end)
|
||||
|
||||
errors = [1, 2, -1, -10, 2002, -2002, 0]
|
||||
Enum.each(errors, fn n ->
|
||||
:io.format "~10w : ", [n]
|
||||
try do
|
||||
IO.puts Middle.three(n)
|
||||
rescue
|
||||
e -> IO.puts e.message
|
||||
end
|
||||
end)
|
||||
|
|
@ -1,22 +1,12 @@
|
|||
function middle(i)
|
||||
s = string(abs(i))
|
||||
l = length(s)
|
||||
mid = int((l+1)/2)
|
||||
mid = round(Int,(l+1)/2)
|
||||
l < 3 ?
|
||||
"error: not enough digits" :
|
||||
iseven(l) ?
|
||||
"error: number of digits is even" :
|
||||
join((s[mid-1],s[mid],s[mid+1]))
|
||||
end
|
||||
|
||||
function dummy(x,y,z=5)
|
||||
"""
|
||||
A dummy docstring over two lines
|
||||
for text coloration.
|
||||
"""
|
||||
print("this is just some text to show text", 1.0, 3)
|
||||
# and comment coloration
|
||||
// and comment coloration 2
|
||||
s[mid-1:mid+1]
|
||||
end
|
||||
|
||||
julia>
|
||||
|
|
|
|||
19
Task/Middle-three-digits/Logo/middle-three-digits.logo
Normal file
19
Task/Middle-three-digits/Logo/middle-three-digits.logo
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
to middle3digits :n
|
||||
if [less? :n 0] [make "n minus :n]
|
||||
local "len make "len count :n
|
||||
if [less? :len 3] [(throw "error [Number must have at least 3 digits])]
|
||||
if [equal? 0 modulo :len 2] [(throw "error [Number must have odd number of digits])]
|
||||
while [greater? count :n 3] [
|
||||
make "n butlast butfirst :n
|
||||
]
|
||||
output :n
|
||||
end
|
||||
|
||||
foreach [123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345
|
||||
1 2 -1 -10 2002 -2002 0] [
|
||||
type sentence (word ? ": char 9) runresult [if [less? count ? 7] [char 9]]
|
||||
make "mid runresult [catch "error [middle3digits ?]]
|
||||
print ifelse [empty? :mid] [item 2 error] [:mid]
|
||||
]
|
||||
|
||||
bye
|
||||
11
Task/Middle-three-digits/NewLISP/middle-three-digits.newlisp
Normal file
11
Task/Middle-three-digits/NewLISP/middle-three-digits.newlisp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(define (middle3 x)
|
||||
(if (even? (length x))
|
||||
(println "You entered " x ". I need an odd number of digits, not " (length x) ".")
|
||||
(if (< (length x) 3)
|
||||
(println "You entered " x ". Sorry, but I need 3 or more digits.")
|
||||
(println "The middle 3 digits of " x " are " ((- (/ (- (length x) 1) 2) 1) 3 (string (abs x))) ".")
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(map middle3 lst)
|
||||
13
Task/Middle-three-digits/Objeck/middle-three-digits.objeck
Normal file
13
Task/Middle-three-digits/Objeck/middle-three-digits.objeck
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class Test {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
text := "123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0";
|
||||
ins := text->Split(",");
|
||||
each(i : ins) {
|
||||
in := ins[i]->Trim();
|
||||
IO.Console->Print(in)->Print(": ");
|
||||
in := (in->Size() > 0 & in->Get(0) = '-') ? in->SubString(1, in->Size() - 1) : in;
|
||||
IO.Console->PrintLine((in->Size() < 2 | in->Size() % 2 = 0) ? "Error" : in->SubString((in->Size() - 3) / 2, 3));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
24
Task/Middle-three-digits/PowerShell/middle-three-digits.psh
Normal file
24
Task/Middle-three-digits/PowerShell/middle-three-digits.psh
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function middle3($inp){
|
||||
|
||||
$str = [Math]::abs($inp)
|
||||
|
||||
$leng = "$str".length
|
||||
|
||||
if ($leng -lt 3){
|
||||
Write-host $inp": [ERROR] too short."
|
||||
Return
|
||||
}
|
||||
if (($leng % 2) -eq 0){
|
||||
Write-host $inp": [ERROR] even number of digits."
|
||||
} else {
|
||||
$trimmer = ($leng - 3) / 2
|
||||
$ans = "$str".subString($trimmer,3)
|
||||
|
||||
Write-host $inp": $ans"
|
||||
}
|
||||
Return
|
||||
}
|
||||
|
||||
$sample = 123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0
|
||||
|
||||
foreach ($x in $sample){middle3 $x}
|
||||
|
|
@ -1,16 +1,17 @@
|
|||
/*REXX program returns the 3 middle digits of a number (or an error). */
|
||||
n ='123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345',
|
||||
'2 -1 -10 2002 -2002 0 abc 1e3 -17e-3 1234567. 1237654.00',
|
||||
/*REXX program returns the three middle digits of a number (or an error msg).*/
|
||||
n= '123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345',
|
||||
'2 -1 -10 2002 -2002 0 abc 1e3 -17e-3 1234567. 1237654.00',
|
||||
'1234567890123456789012345678901234567890123456789012345678901234567'
|
||||
|
||||
do j=1 for words(n); z=word(n,j) /* [↓] format the output nicely.*/
|
||||
say 'middle 3 digits of' right(z,max(15,length(z))) '──►' middle3(z)
|
||||
do j=1 for words(n); #=word(n,j) /* [↓] format the output number nicely*/
|
||||
say 'middle 3 digits of' right(z, max(15, length(#))) '──►' middle3(#)
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────MIDDLE3 subroutine──────────────────*/
|
||||
middle3: procedure; arg x; numeric digits 1e5; er=' ***error!*** '
|
||||
if datatype(x,'N') then x=abs(x); L=length(x)
|
||||
if \datatype(x,'W') then return er "arg isn't an integer"
|
||||
if L<3 then return er "arg is less than three digits"
|
||||
if L//2==0 then return er "arg isn't an odd number of digits"
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
middle3: procedure; parse arg x; numeric digits 1e6; er=' ***error!*** '
|
||||
if pos(.,x)\==0 then x=x/1 /*normalize it, contains decimal point.*/
|
||||
if datatype(x,'N') then x=abs(x); L=length(x)
|
||||
if \datatype(x,'W') then return er "argument isn't an integer."
|
||||
if L<3 then return er "argument is less than three digits."
|
||||
if L//2==0 then return er "argument isn't an odd number of digits."
|
||||
return substr(x, (L-3)%2+1, 3)
|
||||
|
|
|
|||
|
|
@ -1,18 +1,16 @@
|
|||
// rust 0.8
|
||||
|
||||
fn middle_three_digits(x: int) -> Result<~str, ~str> {
|
||||
let s = x.abs().to_str();
|
||||
fn middle_three_digits(x: i32) -> Result<String, String> {
|
||||
let s: String = x.abs().to_string();
|
||||
let len = s.len();
|
||||
if len < 3 {
|
||||
Err(~"Too short")
|
||||
Err("Too short".into())
|
||||
} else if len % 2 == 0 {
|
||||
Err(~"Even number of digits")
|
||||
Err("Even number of digits".into())
|
||||
} else {
|
||||
Ok(s.slice(len/2 - 1, len/2 + 2).to_owned())
|
||||
Ok(s[len/2 - 1 .. len/2 + 2].to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
fn print_result(x: int) {
|
||||
fn print_result(x: i32) {
|
||||
print!("middle_three_digits({}) returned: ", x);
|
||||
match middle_three_digits(x) {
|
||||
Ok(s) => println!("Success, {}", s),
|
||||
|
|
|
|||
21
Task/Middle-three-digits/VBScript/middle-three-digits.vb
Normal file
21
Task/Middle-three-digits/VBScript/middle-three-digits.vb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
'http://rosettacode.org/wiki/Middle_three_digits
|
||||
|
||||
Function mid3n(n)
|
||||
'Remove the number's sign.
|
||||
n = CStr(Abs(n))
|
||||
If Len(n) < 3 Or Len(n) Mod 2 = 0 Then
|
||||
mid3n = "Invalid: Either the length of n < 3 or an even number."
|
||||
ElseIf Round(Len(n)/2) > Len(n)/2 Then
|
||||
mid3n = Mid(n,Round(Len(n)/2)-1,3)
|
||||
Else
|
||||
mid3n = Mid(n,Round(Len(n)/2),3)
|
||||
End If
|
||||
End Function
|
||||
|
||||
'Calling the function.
|
||||
arrn = Array(123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,_
|
||||
1,2,-1,-10,2002,-2002,0)
|
||||
For Each n In arrn
|
||||
WScript.StdOut.Write n & ": " & mid3n(n)
|
||||
WScript.StdOut.WriteLine
|
||||
Next
|
||||
Loading…
Add table
Add a link
Reference in a new issue