langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,46 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
sl = '123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345' -
'1 2 -1 -10 2002 -2002 0' -
'abc 1e3 -17e-3 4004.5 12345678 9876543210' -- extras
parse arg digsL digsR .
if \digsL.datatype('w') then digsL = 3
if \digsR.datatype('w') then digsR = digsL
if digsL > digsR then digsR = digsL
loop dc = digsL to digsR
say 'Middle' dc 'characters'
loop nn = 1 to sl.words()
val = sl.word(nn)
say middleDigits(dc, val)
end nn
say
end dc
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method middle3Digits(val) constant
return middleDigits(3, val)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method middleDigits(digitCt, val) constant
text = val.right(15)':'
even = digitCt // 2 == 0 -- odd or even?
select
when digitCt <= 0 then text = 'digit selection size must be >= 1'
when \val.datatype('w') then text = text 'is not a whole number'
when val.abs().length < digitCt then text = text 'has less than' digitCt 'digits'
when \even & val.abs().length // 2 == 0 then text = text 'does not have an odd number of digits'
when even & val.abs().length // 2 \= 0 then text = text 'does not have an even number of digits'
otherwise do
val = val.abs()
valL = val.length
cutP = (valL - digitCt) % 2
text = text val.substr(cutP + 1, digitCt)
end
catch NumberFormatException
text = val 'is not numeric'
end
return text

View file

@ -0,0 +1,25 @@
let even x = (x land 1) <> 1
let middle_three_digits x =
let s = string_of_int (abs x) in
let n = String.length s in
if n < 3 then failwith "need >= 3 digits" else
if even n then failwith "need odd number of digits" else
String.sub s (n / 2 - 1) 3
let passing = [123; 12345; 1234567; 987654321; 10001; -10001; -123; -100; 100; -12345]
let failing = [1; 2; -1; -10; 2002; -2002; 0]
let print x =
let res =
try (middle_three_digits x)
with Failure e -> "failure: " ^ e
in
Printf.printf "%d: %s\n" x res
let () =
print_endline "Should pass:";
List.iter print passing;
print_endline "Should fail:";
List.iter print failing;
;;

View file

@ -0,0 +1,12 @@
sub middle-three($n) {
given $n.abs {
when .chars < 3 { "$n is too short" }
when .chars %% 2 { "$n has an even number of digits" }
default { "The three middle digits of $n are: ", .substr: (.chars - 3)/2, 3 }
}
}
say middle-three($_) for <
123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345
1 2 -1 -10 2002 -2002 0
>;

View file

@ -0,0 +1,13 @@
x$ = "123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0"
while word$(x$,i+1,",") <> ""
i = i + 1
a1$ = trim$(word$(x$,i,","))
if left$(a1$,1) = "-" then a$ = mid$(a1$,2) else a$ = a1$
if (len(a$) and 1) = 0 or len(a$) < 3 then
print a1$;chr$(9);" length < 3 or is even"
else
print mid$(a$,((len(a$)-3)/2)+1,3);" ";a1$
end if
wend
end

View file

@ -0,0 +1,30 @@
fn middle_three_digits(x: int) -> Result<~str, ~str> {
let s = int::abs(x).to_str();
let len = s.len();
if len < 3 {
Err(~"Too short")
} else if len % 2 == 0 {
Err(~"Even number of digits")
} else {
Ok(s.slice(len/2 - 1, len/2 + 2))
}
}
fn print_result(x: int) {
io::print(fmt!("middle_three_digits(%?) returned: ", x));
match middle_three_digits(x) {
Ok(move s) => io::println(fmt!("Success, %s", s)),
Err(move s) => io::println(fmt!("Failure, %s", s))
}
}
fn main() {
let passing = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345];
let failing = [1, 2, -1, -10, 2002, -2002, 0];
for passing.each |i| {
print_result(*i);
}
for failing.each |i| {
print_result(*i);
}
}

View file

@ -0,0 +1,23 @@
function middle3digits
{
typeset -i n="${1#-}"
typeset -i l=${#n}
if (( l < 3 )); then
echo >&2 "$1 has less than 3 digits"
return 1
elif (( l % 2 == 0 )); then
echo >&2 "$1 has an even number of digits"
return 1
else
echo ${n:$((l/2-1)):3}
return 0
fi
}
# test
testdata=(123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 1 2 -1
-10 2002 -2002 0)
for n in ${testdata[@]}; do
printf "%10d: " $n
middle3digits "$n"
done

View file

@ -0,0 +1,29 @@
do {
#1 = Get_Num("Enter a number, or 0 to stop: ", STATLINE)
Ins_Text("Input: ") Num_Ins(#1, COUNT, 10)
Call("MIDDLE_3_DIGITS")
Ins_Text(" Result: ") Reg_Ins(10) Ins_Newline
Update()
} while (#1);
Return
// Find middle 3 digits of a number
// in: #1 = numeric value
// out: @10 = the result, or error text
//
:MIDDLE_3_DIGITS:
Buf_Switch(Buf_Free)
Num_Ins(abs(#1), LEFT+NOCR) // the input value as string
#2 = Cur_Col-1 // #2 = number of digits
if (#2 < 3) {
Reg_Set(10, "Too few digits!")
} else {
if ((#2 & 1) == 0) {
Reg_Set(10, "Not odd number of digits!")
} else {
Goto_Pos((#2-3)/2)
Reg_Copy_Block(10, Cur_Pos, Cur_Pos+3)
}
}
Buf_Quit(OK)
Return

View file

@ -0,0 +1,24 @@
include c:\cxpl\stdlib;
func Mid3Digits(I); \Return the middle three digits of I
int I;
int Len, Mid;
char S(10);
[ItoA(abs(I), S);
Len:= StrLen(S);
if Len<3 or (Len&1)=0 then return "Must be 3, 5, 7 or 9 digits";
Mid:= Len/2;
S:= S + Mid - 1;
S(2):= S(2) ! $80; \terminate string
return S; \WARNING: very temporary
];
int Passing, Failing, X;
[Passing:= [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345];
Failing:= [1, 2, -1, -10, 2002, -2002, 0]; \WARNING: nasty trick
for X:= 0 to 16 do
[Text(0, "Middle three digits of "); IntOut(0, Passing(X));
Text(0, " returned: ");
Text(0, Mid3Digits(Passing(X))); CrLf(0);
];
]