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,24 @@
using System;
using System.Console;
using Nemerle.Imperative;
module NestedLoops
{
Main() : void
{
def arr = array(10, 10);
def rnd = Random();
foreach ((i, j) in $[(i, j) | i in [0 .. 9], j in [0 .. 9]])
arr[i, j] = rnd.Next(1, 21);
Finish:
{
foreach ((i, j) in $[(i, j) | i in [0 .. 9], j in [0 .. 9]])
{
Write("{0} ", arr[i, j]);
when (arr[i, j] == 20) Finish();
}
}
}
}

View file

@ -0,0 +1,28 @@
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
say
say 'Loops/Nested'
rnd = Random()
dim2 = int[10, 10]
-- build sample data
loop i1 = 0 for dim2.length
loop i2 = 0 for dim2[i1].length
dim2[i1, i2] = rnd.nextInt(20) + 1
end i2
end i1
-- run test
loop x1 = 0 for dim2.length
say Rexx(x1 + 1).right(4)': \-'
loop x2 = 0 for dim2[x1].length
say Rexx(dim2[x1, x2]).right(3) || '\-'
if dim2[x1, x2] = 20 then leave x1
finally
say
end x2
finally
say
end x1

View file

@ -0,0 +1,36 @@
$ ocaml
# Random.self_init();;
- : unit = ()
# let m = Array.make_matrix 10 10 0 ;;
val m : int array array =
[|[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|];
[|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]|]
# for i = 0 to pred 10 do
for j = 0 to pred 10 do
m.(i).(j) <- 1 + Random.int 20
done;
done;;
- : unit = ()
# try
for i = 0 to pred 10 do
for j = 0 to pred 10 do
Printf.printf " %d" m.(i).(j);
if m.(i).(j) = 20 then raise Exit;
done;
print_newline()
done;
with Exit ->
print_newline()
;;
15 8 15 9 9 6 1 18 6 18
17 1 13 15 13 1 16 4 13 9
15 3 5 19 17 3 1 11 5 2
1 1 6 19 20
- : unit = ()

View file

@ -0,0 +1,30 @@
function search_almost_twenty()
% create a 100x100 matrix...
m = unifrnd(0,20, 100,100);
for i = 1:100
for j = 1:100
disp( m(i,j) )
if ( abs(m(i,j) - 20) < 1e-2 )
return
endif
endfor
endfor
endfunction
search_almost_twenty()
% avoiding function, we need a control variable.
m = unifrnd(0,20, 100,100);
innerloopbreak = false;
for i = 1:100
for j = 1:100
disp( m(i,j) )
if ( abs(m(i,j) - 20) < 1e-2 )
innerloopbreak = true;
break;
endif
endfor
if ( innerloopbreak )
break;
endif
endfor

View file

@ -0,0 +1,24 @@
declare
fun {CreateMatrix Width Height}
Matrix = {List.make Height}
in
for Row in Matrix do
Row = {List.make Width}
for X in Row do
X = {OS.rand} mod 20 +1
end
end
Matrix
end
proc {PrintMatrix Matrix}
%% print until we see 20
for Row in Matrix break:OuterBreak do
for X in Row do
{Show X}
if X == 20 then {OuterBreak} end
end
end
end
in
{PrintMatrix {CreateMatrix 10 10}}

View file

@ -0,0 +1,2 @@
M=matrix(10,10,i,j,random(20)+1);
for(i=1,10,for(j=1,10,if(M[i,j]==20,break(2))))

View file

@ -0,0 +1,10 @@
declare x(20,20) fixed; /* 16 August 2010. */
x = random()*20 + 1;
loops:
do i = 1 to hbound(x,1);
do j = 1 to hbound(x,2);
put (x(i,j));
if x(i,j) = 20 then leave loops;
end;
if x(i,j) = 20 then leave;
end;

View file

@ -0,0 +1,10 @@
my @a := [ (1..20).roll(10) ] xx *;
LINE: for @a -> @line {
for @line -> $elem {
print " $elem";
last LINE if $elem == 20;
}
print "\n";
}
print "\n";

View file

@ -0,0 +1,13 @@
my @a := [ (1..20).roll(10) ] xx *;
try {
# LINE:
for @a -> @line {
for @line -> $elem {
print " $elem";
last LINE if $elem == 20;
}
print "\n";
}
}
print "\n";

View file

@ -0,0 +1,17 @@
; Creating and filling array
Dim Value(10, 5)
For a = 0 To 10
For b = 0 To 5
Value(a, b) = Random(19) + 1
Next
Next
; iterating trough array
For a = 0 To 10
For b = 0 To 5
Debug Value(a, b)
If Value(a, b) = 20
; 2 indicates, that there are two nested lopps to break out
Break 2
EndIf
Next
Next

View file

@ -0,0 +1,15 @@
(define random-list
0 -> []
M -> [(1+ (RANDOM 20)) | (random-list (1- M))])
(define random-array
0 _ -> []
N M -> [(random-list M) | (random-array (1- N) M)])
(define array->list
_ [] -> [] \ "end outer loop" \
Stop [[] | Ra] -> (array->list Stop Ra) \ "outer loop" \
Stop [[Stop | _ ] | _ ] -> [] \ "break out from inner loop" \
Stop [[X | Rl] | Ra] -> [X | (array->list Stop [Rl | Ra])]) \ "inner loop" \
(array->list 20 (random-array 10 10))

View file

@ -0,0 +1,40 @@
REBOL [
Title: "Loop/Nested"
Author: oofoe
Date: 2010-01-05
URL: http://rosettacode.org/wiki/Loop/Nested
]
; Number formatting.
zeropad: func [pad n][
n: to-string n insert/dup n "0" (pad - length? n) n]
; Initialize random number generator from current time.
random/seed now
; Create array and fill with random numbers, range 1..20.
soup: array [10 10]
repeat row soup [forall row [row/1: random 20]]
print "Loop break using state variable:"
done: no
for y 1 10 1 [
for x 1 10 1 [
prin rejoin [zeropad 2 soup/:x/:y " "]
if 20 = soup/:x/:y [done: yes break]
]
prin crlf
if done [break]
]
print [crlf "Loop break with catch/throw:"]
catch [
for y 1 10 1 [
for x 1 10 1 [
prin rejoin [zeropad 2 soup/:x/:y " "]
if 20 = soup/:x/:y [throw 'done]
]
prin crlf
]
]
prin crlf

View file

@ -0,0 +1,16 @@
dim a(10,10)
cls
for row = 1 TO 10
for col = 1 TO 10
a(row,col) = INT(20 * RND(1) + 1)
next col
next row
for row = 1 to 10
for col = 1 to 10
print a(row, col)
if a(row, col) = 20 then goto [end]
next col
next row
[end]
print "At row:";row;" col:";col

View file

@ -0,0 +1,22 @@
Prgm
Local mat,i,j
© randMat(5, 5) exists but returns -9 to 9 rather than 1 to 20
newMat(5, 5) → mat
For i,1,rowDim(mat)
For j,1,colDim(mat)
rand(20) → mat[i,j]
EndFor
EndFor
Disp mat
Pause "Press a key."
ClrIO
For i,1,rowDim(mat)
For j,1,colDim(mat)
If mat[i,j] = 20 Then
Stop
Else
Output i*8, j*18, mat[i,j]
EndIf
EndFor
EndFor
EndPrgm

View file

@ -0,0 +1,13 @@
$$ MODE TUSCRIPT
LOOP
row=""
LOOP/CLEAR x=1,10
x=RANDOM_NUMBERS (1,20,1)
row=APPEND(row," ",x)
IF (x==20) THEN
PRINT row
EXIT,EXIT
ENDIF
ENDLOOP
PRINT row
ENDLOOP

View file

@ -0,0 +1,19 @@
size=10
for ((i=0;i<size;i++)); do
unset t[@]
for ((j=0;j<size;j++)); do
t[$j]=$((RANDOM%20+1))
done
a[$i]="${t[*]}"
done
for ((i=0;i<size;i++)); do
t=(${a[$i]})
for ((j=0;j<size;j++)); do
printf "%2d " ${t[$j]}
[ ${t[$j]} -eq 20 ] && break 2
done
echo
done
echo

View file

@ -0,0 +1,26 @@
include c:\cxpl\codes; \intrinsic 'code' declarations
def Size=10;
proc Nest(A); \Display 2-dimensional array A contents until 20 is found
int A;
int I, J, K;
[for J:= 0 to Size-1 do
for I:= 0 to Size-1 do
[K:= A(I,J);
IntOut(0, K); ChOut(0, ^ );
if K = 20 then return; \there is no 'goto' instruction
];
]; \Nest
proc Fill(A); \Fill 2-dimensional array A with random numbers 1..20
int A;
int I, J;
[for J:= 0 to Size-1 do
for I:= 0 to Size-1 do
A(I,J):= Ran(20)+1;
]; \Fill
int Array(Size,Size);
[Fill(Array);
Nest(Array);
]

View file

@ -0,0 +1,12 @@
10 DIM a(10,10)
20 FOR i=1 TO 10: FOR j=1 TO 10
30 LET a(i,j)=INT (RND*20)+1
40 NEXT j: NEXT i
50 LET b=0: REM flag to abort loops
60 FOR i=1 TO 10: FOR j=1 TO 10
70 PRINT (" " AND a(i,j)<10);a(i,j);" ";
80 IF a(i,j)=20 THEN LET i=10: LET j=10: LET b=1: REM abort loops
90 NEXT j
100 IF b=0 THEN PRINT
110 NEXT i
120 STOP