September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,92 +1,63 @@
# we define a UNION MODE so that our middle 3 digits PROC can #
# return either an integer on success or a error message if #
# the middle 3 digits couldn't be extracted #
MODE EINT = UNION( INT # success value #, STRING # error message # );
# PROC to return the middle 3 digits of an integer. #
# if this is not possible, an error message is returned #
# instead #
PROC middle 3 digits = ( INT number ) EINT:
BEGIN
# convert the absolute value of the number to a string with the #
# minumum possible number characters #
STRING digits = whole( ABS number, 0 );
INT len = UPB digits;
IF len < 3
THEN
# number has less than 3 digits #
# return an error message #
"number must have at least three digits"
ELIF ( len MOD 2 ) = 0
THEN
# the number has an even number of digits #
# return an error message #
"number must have an odd number of digits"
ELSE
# the number is suitable for extraction of the middle 3 digits #
INT first digit pos = 1 + ( ( len - 3 ) OVER 2 );
# the result is the integer value of the three digits #
( ( ( ABS digits[ first digit pos ] - ABS "0" ) * 100 )
+ ( ( ABS digits[ first digit pos + 1 ] - ABS "0" ) * 10 )
+ ( ( ABS digits[ first digit pos + 2 ] - ABS "0" ) )
)
FI
END; # middle 3 digits #
main: (
# test the middle 3 digits PROC #
[]INT test values = ( 123, 12345, 1234567, 987654321
, 10001, -10001, -123, -100
, 100, -12345
# the following values should fail #
, 1, 2, -1, -10, 2002, -2002, 0
);
FOR test number FROM LWB test values TO UPB test values
DO
CASE middle 3 digits( test values[ test number ] )
IN
( INT success value ): # got the middle 3 digits #
printf( ( $ 11z-d, " : ", 3d $
, test values[ test number ]
, success value
)
)
,
( STRING error message ): # got an error message #
printf( ( $ 11z-d, " : ", n( UPB error message )a $
, test values[ test number ]
, error message
)
)
ESAC;
print( ( newline ) )
OD
)

View file

@ -0,0 +1,48 @@
begin
% record structure that will be used to return the middle 3 digits of a number %
% if the middle three digits can't be determined, isOk will be false and message %
% will contain an error message %
% if the middle 3 digts can be determined, middle3 will contain the middle 3 %
% digits and isOk will be true %
record MiddleThreeDigits ( integer middle3; logical isOk; string(80) message );
% finds the middle3 digits of a number or describes why they can't be found %
reference(MiddleThreeDigits) procedure findMiddleThreeDigits ( integer value number ) ;
begin
integer n, digitCount, d;
n := abs( number );
% count the number of digits the number has %
digitCount := if n = 0 then 1 else 0;
d := n;
while d > 0 do begin
digitCount := digitCount + 1;
d := d div 10
end while_d_gt_0 ;
if digitCount < 3 then MiddleThreeDigits( 0, false, "Number must have at least 3 digits" )
else if not odd( digitCount ) then MiddleThreeDigits( 0, false, "Number must have an odd number of digits" )
else begin
% can find the middle three digits %
integer m3;
m3 := n;
for d := 1 until ( digitCount - 3 ) div 2 do m3 := m3 div 10;
MiddleThreeDigits( m3 rem 1000, true, "" )
end
end findMiddleThreeDigits ;
% test the findMiddleThreeDigits procedure %
for n := 123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0 do begin
reference(MiddleThreeDigits) m3;
i_w := 10; s_w := 0; % set output formating %
m3 := findMiddleThreeDigits( n );
write( n, ": " );
if not isOk(m3) then writeon( message(m3) )
else begin
% as we return the middle three digits as an integer, we must manually 0 pad %
if middle3(m3) < 100 then writeon( "0" );
if middle3(m3) < 10 then writeon( "0" );
writeon( i_w := 1, middle3(m3) )
end
end for_n
end.

View file

@ -0,0 +1,120 @@
(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
#include
"share/HATS/atspre_staload_libats_ML.hats"
//
(* ****** ****** *)
//
extern
fun
int2digits(x: int): list0(int)
//
(* ****** ****** *)
implement
int2digits(x) =
loop(x, list0_nil) where
{
//
fun
loop
(
x: int, res: list0(int)
) : list0(int) =
if x > 0 then loop(x/10, list0_cons(x%10, res)) else res
//
} (* end of [int2digits] *)
(* ****** ****** *)
extern
fun
Middle_three_digits(x: int): void
(* ****** ****** *)
implement
Middle_three_digits
(x0) = let
//
val x1 =
(
if x0 >= 0 then x0 else ~x0
) : int
//
fun
skip
(
ds: list0(int), k: int
) : list0(int) =
if k > 0 then skip(ds.tail(), k-1) else ds
//
val ds =
int2digits(x1)
//
val n0 = length(ds)
//
in
//
if
(n0 <= 2)
then
(
println! ("Middle-three-digits(", x0, "): Too small!")
)
else
(
if
(n0 % 2 = 0)
then
(
println!
(
"Middle-three-digits(", x0, "): Even number of digits!"
)
)
else let
val ds =
skip(ds, (n0-3)/2)
val-list0_cons(d1, ds) = ds
val-list0_cons(d2, ds) = ds
val-list0_cons(d3, ds) = ds
in
println! ("Middle-three-digits(", x0, "): ", d1, d2, d3)
end // end of [else]
)
//
end // end of [Middle_three_digits]
(* ****** ****** *)
implement
main0() =
{
//
val
thePassing =
g0ofg1
(
$list{int}
(
123
, 12345
, 1234567
, 987654321
, 10001, ~10001
, ~123, ~100, 100, ~12345
)
)
val
theFailing =
g0ofg1($list{int}(1, 2, ~1, ~10, 2002, ~2002, 0))
//
val () = thePassing.foreach()(lam x => Middle_three_digits(x))
val () = theFailing.foreach()(lam x => Middle_three_digits(x))
//
} (* end of [main0] *)
(* ****** ****** *)

View file

@ -0,0 +1,46 @@
import'dart:math';
int length(int x)
{
int i,y;
for(i=0;;i++)
{
y=pow(10,i);
if(x%y==x)
break;
}
return i;
}
int middle(int x,int l)
{
int a=(x/10)-((x%10)/10);
int b=a%(pow(10,l-2));
int l2=length(b);
if(l2==3)
{
return b;
}
if(l2!=3)
{
return middle(b,l2);
}
return 0;
}
main()
{
int x=-100,y;
if(x<0)
x=-x;
int l=length(x);
if(l.isEven||x<100)
{print('error');}
if(l==3)
{print('$x');}
if(l.isOdd&& x>100)
{
y=middle(x,l);
print('$y');
}
}

View file

@ -1,45 +0,0 @@
#APPTYPE CONSOLE
DIM numbers AS STRING = "123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0"
DIM dict[] = Split(numbers, ",")
DIM num AS INTEGER
DIM num2 AS INTEGER
DIM powered AS INTEGER
FOR DIM i = 0 TO COUNT(dict) - 1
num2 = dict[i]
num = ABS(num2)
IF num < 100 THEN
display(num2, "is too small")
ELSE
FOR DIM j = 9 DOWNTO 1
powered = 10 ^ j
IF num >= powered THEN
IF j MOD 2 = 1 THEN
display(num2, "has even number of digits")
ELSE
display(num2, middle3(num, j))
END IF
EXIT FOR
END IF
NEXT
END IF
NEXT
PAUSE
FUNCTION display(num, msg)
PRINT LPAD(num, 11, " "), " --> ", msg
END FUNCTION
FUNCTION middle3(n, pwr)
DIM power AS INTEGER = (pwr \ 2) - 1
DIM m AS INTEGER = n
m = m \ (10 ^ power)
m = m MOD 1000
IF m = 0 THEN
RETURN "000"
ELSE
RETURN m
END IF
END FUNCTION

View file

@ -0,0 +1,24 @@
Public Sub Main()
Dim iList As Integer[] = [123, 12345, 1234567, 987654321, 10001,
-10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0] 'Array of integers to process
Dim sTemp As String 'Temp string
Dim siCount As Short 'Counter
Dim sAnswer As New String[] 'Array, resons for failure or 'middle three digits'
For siCount = 0 To iList.Max 'Loop through the integers
sTemp = Str(Abs(iList[siCount])) 'Convert integer to positive and place in sTemp as a string
If Len(sTemp) < 3 Then 'If sTemp has less than 3 characters then..
sAnswer.Add("This integer has less than 3 characters") 'Place text in sAnswers
Else If Even(Len(sTemp)) Then 'Else If sTemp is of even length then
sAnswer.Add("This integer has an even length") 'Place text in sAnswers
Else 'Else..
sAnswer.Add(Mid(sTemp, Int(Len(sTemp) / 2), 3)) 'Place the middle 3 digits in sAnswer
Endif
Next
For siCount = 0 To iList.Max 'Loop through the integers
Print Space$(10 - Len(Str(iList[siCount]))) &
iList[siCount] & " : " & sAnswer[siCount] 'Print out results
Next
End

View file

@ -0,0 +1,13 @@
var valid = {123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345}
valid.each(\ num ->print(middleThree(num)))
var errant = {1, 2, -1, -10, 2002, -2002, 0}
errant.each(\ num ->print(middleThree(num)))
function middleThree(x: int) : String {
var s = Math.abs(x) as String
if(s.length < 3) return "Error: ${x} has less than 3 digits"
if(s.length % 2 == 0) return "Error: ${x} has an even number of digits"
var start = (s.length / 2) - 1
return s.substring(start, start + 3)
}

View file

@ -1,17 +1,35 @@
import Numeric
mid3 :: Int -> Either String String
mid3 n
| m < 100 = Left "is too small"
| even l = Left "has an even number of digits"
| otherwise = Right . take 3 $ drop ((l - 3) `div` 2) s
where
m = abs n
s = show m
l = length s
mid3 :: Integral a => a -> Either String String
mid3 n | m < 100 = Left "is too small"
| even l = Left "has an even number of digits"
| otherwise = Right . take 3 $ drop ((l-3) `div` 2) s
where m = abs n
s = showInt m ""
l = length s
showMid3 :: Integer -> String
showMid3 :: Int -> String
showMid3 n = show n ++ ": " ++ either id id (mid3 n)
main :: IO ()
main = mapM_ (putStrLn . showMid3) [
123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345,
1, 2, -1, -10, 2002, -2002, 0]
main =
mapM_
(putStrLn . showMid3)
[ 123
, 12345
, 1234567
, 987654321
, 10001
, -10001
, -123
, -100
, 100
, -12345
, 1
, 2
, -1
, -10
, 2002
, -2002
, 0
]

View file

@ -0,0 +1,11 @@
procedure mid3(integer i)
string s = sprintf("%d",abs(i))
integer k = length(s)-3
printf(1,"%10d: %s\n",{i,iff(k<0 or and_bits(k,1)?"error":s[k/2+1..k/2+3])})
end procedure
constant tests = {123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,
1,2,-1,-10,2002,-2002,0}
for i=1 to length(tests) do
mid3(tests[i])
end for

View file

@ -1,28 +0,0 @@
Procedure.s middleThreeDigits(x.q)
Protected x$, digitCount
If x < 0: x = -x: EndIf
x$ = Str(x)
digitCount = Len(x$)
If digitCount < 3
ProcedureReturn "invalid input: too few digits"
ElseIf digitCount % 2 = 0
ProcedureReturn "invalid input: even number of digits"
EndIf
ProcedureReturn Mid(x$,digitCount / 2, 3)
EndProcedure
If OpenConsole()
Define testValues$ = "123 12345 1234567 987654321 10001 -10001 -123 -100 100 -12345 1 2 -1 -10 2002 -2002 0"
Define i, value.q, numTests = CountString(testValues$, " ") + 1
For i = 1 To numTests
value = Val(StringField(testValues$, i, " "))
PrintN(RSet(Str(value), 12, " ") + " : " + middleThreeDigits(value))
Next
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf

View file

@ -1,13 +0,0 @@
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,29 @@
define m3(i)
{
variable s = string(i), sabs = s, l;
if (sabs[0] == '-')
sabs = sabs[[1:]];
l = strlen(sabs);
if (l < 3)
print(sprintf("%s doesn't have enough digits", s));
else if (l & 1) {
variable st = (l-3) >> 1;
print(sprintf("%13s: %s", s, sabs[[st:st+2]]));
}
else
print(sprintf("%s has an even number of digits", s));
}
define middle_3(lst)
{
foreach (lst)
m3( () );
}
middle_3( { 123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345,
#ifexists Int64_Type
6644221335577ll, -11122588999ll,
#endif
1, 2, -1, -10, 2002, -2002, 0 } );

View file

@ -0,0 +1,16 @@
var num:Int = \\enter your number here
if num<0{num = -num}
var numArray:[Int]=[]
while num>0{
var temp:Int=num%10
numArray.append(temp)
num=num/10
}
var i:Int=numArray.count
if i<3||i%2==0{
print("Invalid Input")
}
else{
i=i/2
print("\(numArray[i+1]),\(numArray[i]),\(numArray[i-1])")
}

View file

@ -0,0 +1,9 @@
fcn middle(ns){
ns.apply("toString").apply('-("-"))
.apply(fcn(n){nl:=n.len();
if(nl<3 or nl.isEven) return(False);
n[(nl-3)/2,3] : "%03d".fmt(_)
})
}
middle(T(123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345)).println()
middle(T(1, 2, -1, -10, 2002, -2002, 0)).println();