A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
1
Task/Loops-Nested/0DESCRIPTION
Normal file
1
Task/Loops-Nested/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over <math>[1,\ldots,20]</math>. The loops iterate rows and columns of the array printing the elements until the value <math>20</math> is met. Specifically, this task also shows how to [[Loop/Break|break]] out of nested loops.
|
||||
4
Task/Loops-Nested/1META.yaml
Normal file
4
Task/Loops-Nested/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Loop modifiers
|
||||
note: Iteration
|
||||
20
Task/Loops-Nested/ALGOL-68/loops-nested.alg
Normal file
20
Task/Loops-Nested/ALGOL-68/loops-nested.alg
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
main: (
|
||||
[10][10]INT a; INT i, j;
|
||||
|
||||
FOR i FROM LWB a TO UPB a DO
|
||||
FOR j FROM LWB a[i] TO UPB a[i] DO
|
||||
a[i][j] := ENTIER (random * 20 + 1)
|
||||
OD
|
||||
OD ;
|
||||
FOR i FROM LWB a TO UPB a DO
|
||||
FOR j FROM LWB a[i] TO UPB a[i] DO
|
||||
print(whole(a[i][j], -3));
|
||||
IF a[i][j] = 20 THEN
|
||||
GO TO xkcd com 292 # http://xkcd.com/292/ #
|
||||
FI
|
||||
OD;
|
||||
print(new line)
|
||||
OD;
|
||||
xkcd com 292:
|
||||
print(new line)
|
||||
)
|
||||
26
Task/Loops-Nested/AWK/loops-nested.awk
Normal file
26
Task/Loops-Nested/AWK/loops-nested.awk
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
BEGIN {
|
||||
rows = 5
|
||||
columns = 5
|
||||
|
||||
# Fill ary[] with random numbers from 1 to 20.
|
||||
for (r = 1; r <= rows; r++) {
|
||||
for (c = 1; c <= columns; c++)
|
||||
ary[r, c] = int(rand() * 20) + 1
|
||||
}
|
||||
|
||||
# Find a 20.
|
||||
b = 0
|
||||
for (r = 1; r <= rows; r++) {
|
||||
for (c = 1; c <= columns; c++) {
|
||||
v = ary[r, c]
|
||||
printf " %2d", v
|
||||
if (v == 20) {
|
||||
print
|
||||
b = 1
|
||||
break
|
||||
}
|
||||
}
|
||||
if (b) break
|
||||
print
|
||||
}
|
||||
}
|
||||
21
Task/Loops-Nested/Ada/loops-nested.ada
Normal file
21
Task/Loops-Nested/Ada/loops-nested.ada
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Numerics.Discrete_Random;
|
||||
|
||||
procedure Test_Loop_Nested is
|
||||
type Value_Type is range 1..20;
|
||||
package Random_Values is new Ada.Numerics.Discrete_Random (Value_Type);
|
||||
use Random_Values;
|
||||
Dice : Generator;
|
||||
A : array (1..10, 1..10) of Value_Type :=
|
||||
(others => (others => Random (Dice)));
|
||||
begin
|
||||
|
||||
Outer :
|
||||
for I in A'Range (1) loop
|
||||
for J in A'Range (2) loop
|
||||
Put (Value_Type'Image (A (I, J)));
|
||||
exit Outer when A (I, J) = 20;
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop Outer;
|
||||
end Test_Loop_Nested;
|
||||
24
Task/Loops-Nested/AutoHotkey/loops-nested.ahk
Normal file
24
Task/Loops-Nested/AutoHotkey/loops-nested.ahk
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
Loop, 10
|
||||
{
|
||||
i := A_Index
|
||||
Loop, 10
|
||||
{
|
||||
j := A_Index
|
||||
Random, a%i%%j%, 1, 20
|
||||
}
|
||||
}
|
||||
|
||||
Loop, 10
|
||||
{
|
||||
i := A_Index
|
||||
Loop, 10
|
||||
{
|
||||
j := A_Index
|
||||
If (a%i%%j% == 20)
|
||||
Goto finish
|
||||
}
|
||||
}
|
||||
|
||||
finish:
|
||||
MsgBox % "a[" . i . "][" . j . "]" is 20
|
||||
Return
|
||||
14
Task/Loops-Nested/BASIC/loops-nested.bas
Normal file
14
Task/Loops-Nested/BASIC/loops-nested.bas
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
DIM a(1 TO 10, 1 TO 10) AS INTEGER
|
||||
CLS
|
||||
FOR row = 1 TO 10
|
||||
FOR col = 1 TO 10
|
||||
a(row, col) = INT(RND * 20) + 1
|
||||
NEXT col
|
||||
NEXT row
|
||||
|
||||
FOR row = LBOUND(a, 1) TO UBOUND(a, 1)
|
||||
FOR col = LBOUND(a, 2) TO UBOUND(a, 2)
|
||||
PRINT a(row, col)
|
||||
IF a(row, col) = 20 THEN END
|
||||
NEXT col
|
||||
NEXT row
|
||||
12
Task/Loops-Nested/BBC-BASIC/loops-nested.bbc
Normal file
12
Task/Loops-Nested/BBC-BASIC/loops-nested.bbc
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
DIM array(10,10)
|
||||
FOR row% = 0 TO 10
|
||||
FOR col% = 0 TO 10
|
||||
array(row%,col%) = RND(20) + 1
|
||||
NEXT
|
||||
NEXT row%
|
||||
FOR row% = 0 TO 10
|
||||
FOR col% = 0 TO 10
|
||||
PRINT "row "; row%, "col ";col%, "value "; array(row%,col%)
|
||||
IF array(row%,col%) = 20 EXIT FOR row%
|
||||
NEXT
|
||||
NEXT row%
|
||||
23
Task/Loops-Nested/C++/loops-nested.cpp
Normal file
23
Task/Loops-Nested/C++/loops-nested.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include<cstdlib>
|
||||
#include<ctime>
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int arr[10][10];
|
||||
srand(time(NULL));
|
||||
for(auto& row: arr)
|
||||
for(auto& col: row)
|
||||
col = rand() % 20 + 1;
|
||||
|
||||
([&](){
|
||||
for(auto& row : arr)
|
||||
for(auto& col: row)
|
||||
{
|
||||
cout << col << endl;
|
||||
if(col == 20)return;
|
||||
}
|
||||
})();
|
||||
return 0;
|
||||
}
|
||||
24
Task/Loops-Nested/C/loops-nested.c
Normal file
24
Task/Loops-Nested/C/loops-nested.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int a[10][10], i, j;
|
||||
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < 10; i++)
|
||||
for (j = 0; j < 10; j++)
|
||||
a[i][j] = rand() % 20 + 1;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
for (j = 0; j < 10; j++) {
|
||||
printf(" %d", a[i][j]);
|
||||
if (a[i][j] == 20)
|
||||
goto Done;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
Done:
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
18
Task/Loops-Nested/Clojure/loops-nested.clj
Normal file
18
Task/Loops-Nested/Clojure/loops-nested.clj
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(ns nested)
|
||||
|
||||
(defn create-matrix [width height]
|
||||
(for [_ (range width)]
|
||||
(for [_ (range height)]
|
||||
(inc (rand-int 20)))))
|
||||
|
||||
(defn print-matrix [matrix]
|
||||
(loop [[row & rs] matrix]
|
||||
(when (= (loop [[x & xs] row]
|
||||
(println x)
|
||||
(cond (= x 20) :stop
|
||||
xs (recur xs)
|
||||
:else :continue))
|
||||
:continue)
|
||||
(when rs (recur rs)))))
|
||||
|
||||
(print-matrix (create-matrix 10 10))
|
||||
14
Task/Loops-Nested/Common-Lisp/loops-nested.lisp
Normal file
14
Task/Loops-Nested/Common-Lisp/loops-nested.lisp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(let ((a (make-array '(10 10))))
|
||||
(dotimes (i 10)
|
||||
(dotimes (j 10)
|
||||
(setf (aref a i j) (1+ (random 20)))))
|
||||
|
||||
(block outer
|
||||
(dotimes (i 10)
|
||||
(dotimes (j 10)
|
||||
(princ " ")
|
||||
(princ (aref a i j))
|
||||
(if (= 20 (aref a i j))
|
||||
(return-from outer)))
|
||||
(terpri))
|
||||
(terpri)))
|
||||
18
Task/Loops-Nested/D/loops-nested.d
Normal file
18
Task/Loops-Nested/D/loops-nested.d
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import std.stdio, std.random;
|
||||
|
||||
void main() {
|
||||
int[10][10] mat;
|
||||
foreach (ref row; mat)
|
||||
foreach (ref item; row)
|
||||
item = uniform(1, 21);
|
||||
|
||||
outer:
|
||||
foreach (row; mat)
|
||||
foreach (item; row) {
|
||||
write(item, ' ');
|
||||
if (item == 20)
|
||||
break outer;
|
||||
}
|
||||
|
||||
writeln();
|
||||
}
|
||||
27
Task/Loops-Nested/Delphi/loops-nested.delphi
Normal file
27
Task/Loops-Nested/Delphi/loops-nested.delphi
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
var
|
||||
matrix: array[1..10,1..10] of Integer;
|
||||
row, col: Integer;
|
||||
broken: Boolean;
|
||||
begin
|
||||
// Launch random number generator
|
||||
randomize;
|
||||
// Filling matrix with random numbers
|
||||
for row := 1 to 10 do
|
||||
for col := 1 to 10 do
|
||||
matrix[row, col] := Succ(Random(20));
|
||||
// Displaying values one by one, until at the end or reached number 20
|
||||
Broken := False;
|
||||
for row := 1 to 10 do
|
||||
begin
|
||||
for col := 1 to 10 do
|
||||
begin
|
||||
ShowMessage(IntToStr(matrix[row, col]));
|
||||
if matrix[row, col] = 20 then
|
||||
begin
|
||||
Broken := True;
|
||||
break;
|
||||
end;
|
||||
end;
|
||||
if Broken then break;
|
||||
end;
|
||||
end;
|
||||
14
Task/Loops-Nested/E/loops-nested.e
Normal file
14
Task/Loops-Nested/E/loops-nested.e
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
def array := accum [] for i in 1..5 { _.with(accum [] for i in 1..5 { _.with(entropy.nextInt(20) + 1) }) }
|
||||
|
||||
escape done {
|
||||
for row in array {
|
||||
for x in row {
|
||||
print(`$x$\t`)
|
||||
if (x == 20) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
println()
|
||||
}
|
||||
}
|
||||
println("done.")
|
||||
18
Task/Loops-Nested/Euphoria/loops-nested.euphoria
Normal file
18
Task/Loops-Nested/Euphoria/loops-nested.euphoria
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
sequence a
|
||||
a = rand(repeat(repeat(20, 10), 10))
|
||||
|
||||
integer wantExit
|
||||
wantExit = 0
|
||||
|
||||
for i = 1 to 10 do
|
||||
for j = 1 to 10 do
|
||||
printf(1, "%g ", {a[i][j]})
|
||||
if a[i][j] = 20 then
|
||||
wantExit = 1
|
||||
exit
|
||||
end if
|
||||
end for
|
||||
if wantExit then
|
||||
exit
|
||||
end if
|
||||
end for
|
||||
34
Task/Loops-Nested/Fantom/loops-nested.fantom
Normal file
34
Task/Loops-Nested/Fantom/loops-nested.fantom
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
rows := 10
|
||||
cols := 10
|
||||
// create and fill an array of given size with random numbers
|
||||
Int[][] array := [,]
|
||||
rows.times
|
||||
{
|
||||
row := [,]
|
||||
cols.times { row.add(Int.random(1..20)) }
|
||||
array.add (row)
|
||||
}
|
||||
// now do the search
|
||||
try
|
||||
{
|
||||
for (i := 0; i < rows; i++)
|
||||
{
|
||||
for (j := 0; j < cols; j++)
|
||||
{
|
||||
echo ("now at ($i, $j) which is ${array[i][j]}")
|
||||
if (array[i][j] == 20) throw (Err("found it"))
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Err e)
|
||||
{
|
||||
echo (e.msg)
|
||||
return // and finish
|
||||
}
|
||||
echo ("No 20")
|
||||
}
|
||||
}
|
||||
18
Task/Loops-Nested/Forth/loops-nested.fth
Normal file
18
Task/Loops-Nested/Forth/loops-nested.fth
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
include random.fs
|
||||
|
||||
10 constant X
|
||||
10 constant Y
|
||||
|
||||
: ,randoms ( range n -- ) 0 do dup random 1+ , loop drop ;
|
||||
|
||||
create 2darray 20 X Y * ,randoms
|
||||
|
||||
: main
|
||||
Y 0 do
|
||||
cr
|
||||
X 0 do
|
||||
j X * i + cells 2darray + @
|
||||
dup .
|
||||
20 = if unloop unloop exit then
|
||||
loop
|
||||
loop ;
|
||||
76
Task/Loops-Nested/Fortran/loops-nested-1.f
Normal file
76
Task/Loops-Nested/Fortran/loops-nested-1.f
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
PROGRAM LOOPNESTED
|
||||
INTEGER A, I, J, RNDINT
|
||||
|
||||
C Build a two-dimensional twenty-by-twenty array.
|
||||
DIMENSION A(20,20)
|
||||
|
||||
C It doesn't matter what number you put here.
|
||||
CALL SDRAND(123)
|
||||
|
||||
C Fill the array with random numbers.
|
||||
DO 20 I = 1, 20
|
||||
DO 10 J = 1, 20
|
||||
A(I, J) = RNDINT(1, 20)
|
||||
10 CONTINUE
|
||||
20 CONTINUE
|
||||
|
||||
C Print the numbers.
|
||||
DO 40 I = 1, 20
|
||||
DO 30 J = 1, 20
|
||||
WRITE (*,5000) I, J, A(I, J)
|
||||
|
||||
C If this number is twenty, break out of both loops.
|
||||
IF (A(I, J) .EQ. 20) GOTO 50
|
||||
30 CONTINUE
|
||||
40 CONTINUE
|
||||
|
||||
C If we had gone to 40, the DO loop would have continued. You can
|
||||
C label STOP instead of adding another CONTINUE, but it is good
|
||||
C form to only label CONTINUE statements as much as possible.
|
||||
50 CONTINUE
|
||||
STOP
|
||||
|
||||
C Print the value so that it looks like one of those C arrays that
|
||||
C makes everybody so comfortable.
|
||||
5000 FORMAT('A[', I2, '][', I2, '] is ', I2)
|
||||
END
|
||||
|
||||
C FORTRAN 77 does not have come with a random number generator, but it
|
||||
C is easy enough to type "fortran 77 random number generator" into your
|
||||
C preferred search engine and to copy and paste what you find. The
|
||||
C following code is a slightly-modified version of:
|
||||
C
|
||||
C http://www.tat.physik.uni-tuebingen.de/
|
||||
C ~kley/lehre/ftn77/tutorial/subprograms.html
|
||||
SUBROUTINE SDRAND (IRSEED)
|
||||
COMMON /SEED/ UTSEED, IRFRST
|
||||
UTSEED = IRSEED
|
||||
IRFRST = 0
|
||||
RETURN
|
||||
END
|
||||
INTEGER FUNCTION RNDINT (IFROM, ITO)
|
||||
INTEGER IFROM, ITO
|
||||
PARAMETER (MPLIER=16807, MODLUS=2147483647, &
|
||||
& MOBYMP=127773, MOMDMP=2836)
|
||||
COMMON /SEED/ UTSEED, IRFRST
|
||||
INTEGER HVLUE, LVLUE, TESTV, NEXTN
|
||||
SAVE NEXTN
|
||||
IF (IRFRST .EQ. 0) THEN
|
||||
NEXTN = UTSEED
|
||||
IRFRST = 1
|
||||
ENDIF
|
||||
HVLUE = NEXTN / MOBYMP
|
||||
LVLUE = MOD(NEXTN, MOBYMP)
|
||||
TESTV = MPLIER*LVLUE - MOMDMP*HVLUE
|
||||
IF (TESTV .GT. 0) THEN
|
||||
NEXTN = TESTV
|
||||
ELSE
|
||||
NEXTN = TESTV + MODLUS
|
||||
ENDIF
|
||||
IF (NEXTN .GE. 0) THEN
|
||||
RNDINT = MOD(MOD(NEXTN, MODLUS), ITO - IFROM + 1) + IFROM
|
||||
ELSE
|
||||
RNDINT = MOD(MOD(NEXTN, MODLUS), ITO - IFROM + 1) + ITO + 1
|
||||
ENDIF
|
||||
RETURN
|
||||
END
|
||||
19
Task/Loops-Nested/Fortran/loops-nested-2.f
Normal file
19
Task/Loops-Nested/Fortran/loops-nested-2.f
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
program Example
|
||||
implicit none
|
||||
|
||||
real :: ra(5,10)
|
||||
integer :: ia(5,10)
|
||||
integer :: i, j
|
||||
|
||||
call random_number(ra)
|
||||
ia = int(ra * 20.0) + 1
|
||||
|
||||
outer: do i = 1, size(ia, 1)
|
||||
do j = 1, size(ia, 2)
|
||||
write(*, "(i3)", advance="no") ia(i,j)
|
||||
if (ia(i,j) == 20) exit outer
|
||||
end do
|
||||
write(*,*)
|
||||
end do outer
|
||||
|
||||
end program Example
|
||||
32
Task/Loops-Nested/Go/loops-nested.go
Normal file
32
Task/Loops-Nested/Go/loops-nested.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
values := make([][]int, 10)
|
||||
for i := range values {
|
||||
values[i] = make([]int, 10)
|
||||
for j := range values[i] {
|
||||
values[i][j] = rand.Intn(20) + 1
|
||||
}
|
||||
}
|
||||
|
||||
outerLoop:
|
||||
for i, row := range values {
|
||||
fmt.Printf("%3d)", i)
|
||||
for _, value := range row {
|
||||
fmt.Printf(" %3d", value)
|
||||
if value == 20 {
|
||||
break outerLoop
|
||||
}
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
22
Task/Loops-Nested/Groovy/loops-nested.groovy
Normal file
22
Task/Loops-Nested/Groovy/loops-nested.groovy
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
final random = new Random()
|
||||
def a = []
|
||||
(0..<10).each {
|
||||
def row = []
|
||||
(0..<10).each {
|
||||
row << (random.nextInt(20) + 1)
|
||||
}
|
||||
a << row
|
||||
}
|
||||
|
||||
a.each { println it }
|
||||
println ()
|
||||
|
||||
Outer:
|
||||
for (i in (0..<a.size())) {
|
||||
for (j in (0..<a[i].size())) {
|
||||
if (a[i][j] == 20){
|
||||
println ([i:i, j:j])
|
||||
break Outer
|
||||
}
|
||||
}
|
||||
}
|
||||
5
Task/Loops-Nested/Haskell/loops-nested-1.hs
Normal file
5
Task/Loops-Nested/Haskell/loops-nested-1.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import Data.List
|
||||
|
||||
breakIncl p = uncurry ((. take 1). (++)). break p
|
||||
|
||||
taskLLB k = map (breakIncl (==k)). breakIncl (k`elem`)
|
||||
17
Task/Loops-Nested/Haskell/loops-nested-2.hs
Normal file
17
Task/Loops-Nested/Haskell/loops-nested-2.hs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
mij :: [[Int]]
|
||||
mij = takeWhile(not.null). unfoldr (Just. splitAt 5) $
|
||||
[2, 6, 17, 5, 14, 1, 9, 11, 18, 10, 13, 20, 8, 7, 4, 16, 15, 19, 3, 12]
|
||||
|
||||
*Main> mapM_ (mapM_ print) $ taskLLB 20 mij
|
||||
2
|
||||
6
|
||||
17
|
||||
5
|
||||
14
|
||||
1
|
||||
9
|
||||
11
|
||||
18
|
||||
10
|
||||
13
|
||||
20
|
||||
12
Task/Loops-Nested/HicEst/loops-nested.hicest
Normal file
12
Task/Loops-Nested/HicEst/loops-nested.hicest
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
REAL :: n=20, array(n,n)
|
||||
|
||||
array = NINT( RAN(10,10) )
|
||||
|
||||
DO row = 1, n
|
||||
DO col = 1, n
|
||||
WRITE(Name) row, col, array(row,col)
|
||||
IF( array(row, col) == 20 ) GOTO 99
|
||||
ENDDO
|
||||
ENDDO
|
||||
|
||||
99 END
|
||||
10
Task/Loops-Nested/Icon/loops-nested-1.icon
Normal file
10
Task/Loops-Nested/Icon/loops-nested-1.icon
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
procedure main()
|
||||
|
||||
every !(!(L := list(10)) := list(10)) := ?20 # setup a 2d array of random numbers up to 20
|
||||
|
||||
every i := 1 to *L do # using nested loops
|
||||
every j := 1 to *L[i] do
|
||||
if L[i,j] = 20 then
|
||||
break break write("L[",i,",",j,"]=20")
|
||||
|
||||
end
|
||||
4
Task/Loops-Nested/Icon/loops-nested-2.icon
Normal file
4
Task/Loops-Nested/Icon/loops-nested-2.icon
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
every x := L[i := 1 to *L,1 to *L[i]] do
|
||||
if x = 20 then break write("L[",i,",",j,"]=20") # more succinctly
|
||||
|
||||
every if !!L = 20 then break write("Found !!L=20") # even more so (but looses the values of i and j
|
||||
1
Task/Loops-Nested/J/loops-nested-1.j
Normal file
1
Task/Loops-Nested/J/loops-nested-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
use=: ({.~ # <. 1+i.&20)@:,
|
||||
8
Task/Loops-Nested/J/loops-nested-2.j
Normal file
8
Task/Loops-Nested/J/loops-nested-2.j
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
doubleLoop=:verb define
|
||||
for_row.i.#y do.
|
||||
for_col.i.1{$y do.
|
||||
smoutput t=.(<row,col) { y
|
||||
if.20=t do.''return.end.
|
||||
end.
|
||||
end.
|
||||
)
|
||||
21
Task/Loops-Nested/Java/loops-nested.java
Normal file
21
Task/Loops-Nested/Java/loops-nested.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import java.util.Random;
|
||||
|
||||
public class NestedLoopTest {
|
||||
public static final Random gen = new Random();
|
||||
public static void main(String[] args) {
|
||||
int[][] a = new int[10][10];
|
||||
for (int i = 0; i < a.length; i++)
|
||||
for (int j = 0; j < a[i].length; j++)
|
||||
a[i][j] = gen.nextInt(20) + 1;
|
||||
|
||||
Outer:for (int i = 0; i < a.length; i++) {
|
||||
for (int j = 0; j < a[i].length; j++) {
|
||||
System.out.print(" " + a[i][j]);
|
||||
if (a[i][j] == 20)
|
||||
break Outer; //adding a label breaks out of all loops up to and including the labelled loop
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
13
Task/Loops-Nested/JavaScript/loops-nested.js
Normal file
13
Task/Loops-Nested/JavaScript/loops-nested.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// a "random" 2-D array
|
||||
var a = [[2, 12, 10, 4], [18, 11, 9, 3], [14, 15, 7, 17], [6, 19, 8, 13], [1, 20, 16, 5]];
|
||||
|
||||
outer_loop:
|
||||
for (var i in a) {
|
||||
print("row " + i);
|
||||
for (var j in a[i]) {
|
||||
print(" " + a[i][j]);
|
||||
if (a[i][j] == 20)
|
||||
break outer_loop;
|
||||
}
|
||||
}
|
||||
print("done");
|
||||
19
Task/Loops-Nested/Liberty-BASIC/loops-nested.liberty
Normal file
19
Task/Loops-Nested/Liberty-BASIC/loops-nested.liberty
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
dim ar(10,10)
|
||||
for i = 1 to 10
|
||||
for j = 1 to 10
|
||||
ar(i, j) = int(rnd(1) * 20) + 1
|
||||
next
|
||||
next
|
||||
|
||||
flag=0
|
||||
for x = 1 to 10
|
||||
for y = 1 to 10
|
||||
print ar(x,y)
|
||||
if ar(x,y) = 20 then
|
||||
flag=1
|
||||
exit for
|
||||
end if
|
||||
next
|
||||
if flag then exit for
|
||||
next
|
||||
print "Completed row ";x;" and column ";y
|
||||
35
Task/Loops-Nested/Lisaac/loops-nested.lisaac
Normal file
35
Task/Loops-Nested/Lisaac/loops-nested.lisaac
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
Section Header
|
||||
|
||||
+ name := TEST_LOOP_NESTED;
|
||||
|
||||
- external := `#include <time.h>`;
|
||||
|
||||
Section Public
|
||||
|
||||
- main <- (
|
||||
+ a : ARRAY2[INTEGER];
|
||||
+ i, j: INTEGER;
|
||||
|
||||
`srand(time(NULL))`;
|
||||
a := ARRAY2[INTEGER].create(0, 0) to (9, 9);
|
||||
0.to 9 do { ii : INTEGER;
|
||||
0.to 9 do { jj : INTEGER;
|
||||
a.put (`rand()`:INTEGER % 20 + 1) to (ii, jj);
|
||||
};
|
||||
};
|
||||
{ i < 10 }.while_do {
|
||||
j := 0;
|
||||
{ j < 10 }.while_do {
|
||||
' '.print;
|
||||
a.item(i, j).print;
|
||||
(a.item(i, j) = 20).if {
|
||||
i := 999;
|
||||
j := 999;
|
||||
};
|
||||
j := j + 1;
|
||||
};
|
||||
i := i + 1;
|
||||
'\n'.print;
|
||||
};
|
||||
'\n'.print;
|
||||
);
|
||||
15
Task/Loops-Nested/Logo/loops-nested.logo
Normal file
15
Task/Loops-Nested/Logo/loops-nested.logo
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
make "a mdarray [10 10]
|
||||
|
||||
for [j 1 10] [for [i 1 10] [mdsetitem list :i :j :a (1 + random 20)]]
|
||||
|
||||
to until.20
|
||||
for [j 1 10] [
|
||||
for [i 1 10] [
|
||||
type mditem list :i :j :a
|
||||
type "| |
|
||||
if equal? 20 mditem list :i :j :a [stop]
|
||||
]
|
||||
print "||
|
||||
]
|
||||
end
|
||||
until.20
|
||||
15
Task/Loops-Nested/Lua/loops-nested.lua
Normal file
15
Task/Loops-Nested/Lua/loops-nested.lua
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
t = {}
|
||||
for i = 1, 20 do
|
||||
t[i] = {}
|
||||
for j = 1, 20 do t[i][j] = math.random(20) end
|
||||
end
|
||||
function exitable()
|
||||
for i = 1, 20 do
|
||||
for j = 1, 20 do
|
||||
if t[i][j] == 20 then
|
||||
return i, j
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
print(exitable())
|
||||
2
Task/Loops-Nested/MATLAB/loops-nested.m
Normal file
2
Task/Loops-Nested/MATLAB/loops-nested.m
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a = ceil(rand(100,100)*20);
|
||||
[ix,iy]=find(a==20,1)
|
||||
18
Task/Loops-Nested/MOO/loops-nested.moo
Normal file
18
Task/Loops-Nested/MOO/loops-nested.moo
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
a = make(10, make(10));
|
||||
for i in [1..10]
|
||||
for j in [1..10]
|
||||
a[i][j] = random(20);
|
||||
endfor
|
||||
endfor
|
||||
for i in [1..10]
|
||||
s = "";
|
||||
for j in [1..10]
|
||||
s += tostr(" ", a[i][j]);
|
||||
if (a[i][j] == 20)
|
||||
break i;
|
||||
endif
|
||||
endfor
|
||||
player:tell(s);
|
||||
s = "";
|
||||
endfor
|
||||
player:tell(s);
|
||||
13
Task/Loops-Nested/MUMPS/loops-nested.mumps
Normal file
13
Task/Loops-Nested/MUMPS/loops-nested.mumps
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
NESTLOOP
|
||||
;.../loops/nested
|
||||
;set up the 2D array with random values
|
||||
NEW A,I,J,K,FLAG,TRIGGER
|
||||
SET K=15 ;Magic - just to give us a size to work with
|
||||
SET TRIGGER=20 ;Magic - the max value, and the end value
|
||||
FOR I=1:1:K FOR J=1:1:K SET A(I,J)=$RANDOM(TRIGGER)+1
|
||||
;Now, search through the array, halting when the value of TRIGGER is found
|
||||
SET FLAG=0
|
||||
SET (I,J)=0
|
||||
FOR I=1:1:K Q:FLAG W ! FOR J=1:1:K WRITE A(I,J),$SELECT(J'=K:", ",1:"") SET FLAG=(A(I,J)=TRIGGER) Q:FLAG
|
||||
KILL A,I,J,K,FLAG,TRIGGER
|
||||
QUIT
|
||||
4
Task/Loops-Nested/Mathematica/loops-nested.mathematica
Normal file
4
Task/Loops-Nested/Mathematica/loops-nested.mathematica
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Do[ Print[m[[i, j]]];
|
||||
If[m[[i, j]] === 20, Return[]],
|
||||
{i, 1, Dimensions[m][[1]]},
|
||||
{j, 1, Dimensions[m][[2]]}]
|
||||
15
Task/Loops-Nested/Maxima/loops-nested.maxima
Normal file
15
Task/Loops-Nested/Maxima/loops-nested.maxima
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
data: apply(matrix, makelist(makelist(random(100), 20), 20))$
|
||||
|
||||
find_value(a, x) := block(
|
||||
[p, q],
|
||||
[p, q]: matrix_size(a),
|
||||
catch(
|
||||
for i thru p do
|
||||
for j thru q do
|
||||
if a[i, j] = x then throw([i, j]),
|
||||
'not\ found
|
||||
)
|
||||
)$
|
||||
|
||||
find_value(data, 100);
|
||||
not found
|
||||
15
Task/Loops-Nested/PHP/loops-nested.php
Normal file
15
Task/Loops-Nested/PHP/loops-nested.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
for ($i = 0; $i < 10; $i++)
|
||||
for ($j = 0; $j < 10; $j++)
|
||||
$a[$i][$j] = rand(1, 20);
|
||||
|
||||
foreach ($a as $row) {
|
||||
foreach ($row as $element) {
|
||||
echo " $element";
|
||||
if ($element == 20)
|
||||
break 2; // 2 is the number of loops we want to break out of
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
echo "\n";
|
||||
?>
|
||||
13
Task/Loops-Nested/Perl/loops-nested.pl
Normal file
13
Task/Loops-Nested/Perl/loops-nested.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
my $a = [ map [ map { int(rand(20)) + 1 } 1 .. 10 ], 1 .. 10];
|
||||
|
||||
Outer:
|
||||
foreach (@$a) {
|
||||
foreach (@$_) {
|
||||
print " $_";
|
||||
if ($_ == 20) {
|
||||
last Outer;
|
||||
}
|
||||
}
|
||||
print "\n";
|
||||
}
|
||||
print "\n";
|
||||
5
Task/Loops-Nested/PicoLisp/loops-nested-1.l
Normal file
5
Task/Loops-Nested/PicoLisp/loops-nested-1.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(for Lst (make (do 10 (link (make (do 10 (link (rand 1 20)))))))
|
||||
(T
|
||||
(for N Lst
|
||||
(printsp N)
|
||||
(T (= N 20) T) ) ) )
|
||||
5
Task/Loops-Nested/PicoLisp/loops-nested-2.l
Normal file
5
Task/Loops-Nested/PicoLisp/loops-nested-2.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(catch NIL
|
||||
(for Lst (make (do 10 (link (make (do 10 (link (rand 1 20)))))))
|
||||
(for N Lst
|
||||
(printsp N)
|
||||
(and (= N 20) (throw)) ) ) )
|
||||
14
Task/Loops-Nested/Python/loops-nested-1.py
Normal file
14
Task/Loops-Nested/Python/loops-nested-1.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from random import randint
|
||||
|
||||
def do_scan(mat):
|
||||
for row in mat:
|
||||
for item in row:
|
||||
print item,
|
||||
if item == 20:
|
||||
print
|
||||
return
|
||||
print
|
||||
print
|
||||
|
||||
mat = [[randint(1, 20) for x in xrange(10)] for y in xrange(10)]
|
||||
do_scan(mat)
|
||||
16
Task/Loops-Nested/Python/loops-nested-2.py
Normal file
16
Task/Loops-Nested/Python/loops-nested-2.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from random import randint
|
||||
|
||||
class Found20(Exception):
|
||||
pass
|
||||
|
||||
mat = [[randint(1, 20) for x in xrange(10)] for y in xrange(10)]
|
||||
|
||||
try:
|
||||
for row in mat:
|
||||
for item in row:
|
||||
print item,
|
||||
if item == 20:
|
||||
raise Found20
|
||||
print
|
||||
except Found20:
|
||||
print
|
||||
14
Task/Loops-Nested/Python/loops-nested-3.py
Normal file
14
Task/Loops-Nested/Python/loops-nested-3.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from random import randint
|
||||
|
||||
mat = [[randint(1, 20) for x in xrange(10)] for y in xrange(10)]
|
||||
|
||||
found20 = False
|
||||
for row in mat:
|
||||
for item in row:
|
||||
print item,
|
||||
if item == 20:
|
||||
found20 = True
|
||||
break
|
||||
print
|
||||
if found20:
|
||||
break
|
||||
22
Task/Loops-Nested/R/loops-nested-1.r
Normal file
22
Task/Loops-Nested/R/loops-nested-1.r
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
m <- 10
|
||||
n <- 10
|
||||
mat <- matrix(sample(1:20L, m*n, replace=TRUE), nrow=m); mat
|
||||
done <- FALSE
|
||||
for(i in seq_len(m))
|
||||
{
|
||||
for(j in seq_len(n))
|
||||
{
|
||||
cat(mat[i,j])
|
||||
if(mat[i,j] == 20)
|
||||
{
|
||||
done <- TRUE
|
||||
break
|
||||
}
|
||||
cat(", ")
|
||||
}
|
||||
if(done)
|
||||
{
|
||||
cat("\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
5
Task/Loops-Nested/R/loops-nested-2.r
Normal file
5
Task/Loops-Nested/R/loops-nested-2.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
m <- 10; n <- 10; mat <- matrix(sample(1:20L, m*n, replace=TRUE), nrow=m);
|
||||
x<-which(mat==20,arr.ind=TRUE,useNames=FALSE)
|
||||
x<-x[order(x[,1]),]
|
||||
for(i in mat[1:x[1,1]-1,]) print(i)
|
||||
for(i in mat[x[1,1],1:x[1,2]]) print(i)
|
||||
18
Task/Loops-Nested/REXX/loops-nested.rexx
Normal file
18
Task/Loops-Nested/REXX/loops-nested.rexx
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/*REXX program to loop through a 2-dimensional array to look for a '20'.*/
|
||||
rows=60
|
||||
cols=10
|
||||
do row =1 for rows /*1st dimension ∙∙∙ */
|
||||
do col=1 for cols /*2nd dimension ∙∙∙ */
|
||||
@.row.col=random(1,20) /*generate some nums.*/
|
||||
end /*row*/
|
||||
end /*col*/
|
||||
/*─────────────────────────────────────now, search for the hidden twenty*/
|
||||
do r =1 for rows
|
||||
do c=1 for cols
|
||||
say left('@.'r"."c,9) '=' right(@.r.c,4)
|
||||
if @.r.c==20 then leave r
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
|
||||
say right(' Done with Loops/Nested. ',50,'─')
|
||||
/*stick a fork in it, we're done.*/
|
||||
13
Task/Loops-Nested/Racket/loops-nested.rkt
Normal file
13
Task/Loops-Nested/Racket/loops-nested.rkt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#lang racket
|
||||
(define (scan xss)
|
||||
(for* ([xs xss]
|
||||
[x xs]
|
||||
#:final (= x 20))
|
||||
(displayln x)))
|
||||
|
||||
(define matrix
|
||||
(for/list ([x 10])
|
||||
(for/list ([y 10])
|
||||
(+ (random 20) 1))))
|
||||
|
||||
(scan matrix)
|
||||
15
Task/Loops-Nested/Ruby/loops-nested.rb
Normal file
15
Task/Loops-Nested/Ruby/loops-nested.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
srand
|
||||
ary = (1..20).to_a.shuffle.each_slice(4).to_a
|
||||
p ary
|
||||
|
||||
catch :found_it do
|
||||
for row in ary
|
||||
for element in row
|
||||
print "%2d " % element
|
||||
throw :found_it if element == 20
|
||||
end
|
||||
puts ","
|
||||
end
|
||||
end
|
||||
|
||||
puts "done"
|
||||
24
Task/Loops-Nested/Sather/loops-nested.sa
Normal file
24
Task/Loops-Nested/Sather/loops-nested.sa
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class MAIN is
|
||||
main is
|
||||
a:ARRAY2{INT} := #(10,10);
|
||||
i, j :INT;
|
||||
|
||||
RND::seed(1230);
|
||||
loop i := 0.upto!(9);
|
||||
loop j := 0.upto!(9);
|
||||
a[i, j] := RND::int(1, 20);
|
||||
end;
|
||||
end;
|
||||
|
||||
loopthis ::= true;
|
||||
loop i := 0.upto!(9); while!( loopthis );
|
||||
loop j := 0.upto!(9);
|
||||
#OUT + " " + a[i, j];
|
||||
if a[i, j] = 20 then
|
||||
loopthis := false;
|
||||
break!;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
9
Task/Loops-Nested/Scala/loops-nested.scala
Normal file
9
Task/Loops-Nested/Scala/loops-nested.scala
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import scala.util.control.Breaks._
|
||||
val a=Array.fill(5,4)(scala.util.Random.nextInt(21))
|
||||
println(a map (_.mkString("[", ", ", "]")) mkString "\n")
|
||||
breakable {
|
||||
for(row <- a; x <- row){
|
||||
println(x)
|
||||
if (x==20) break
|
||||
}
|
||||
}
|
||||
12
Task/Loops-Nested/Scheme/loops-nested-1.ss
Normal file
12
Task/Loops-Nested/Scheme/loops-nested-1.ss
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(call-with-current-continuation
|
||||
(lambda (return)
|
||||
(for-each (lambda (a)
|
||||
(for-each (lambda (b)
|
||||
(cond ((= 20 b)
|
||||
(newline)
|
||||
(return))
|
||||
(else
|
||||
(display " ")(display b))))
|
||||
a)
|
||||
(newline))
|
||||
array)))
|
||||
11
Task/Loops-Nested/Scheme/loops-nested-2.ss
Normal file
11
Task/Loops-Nested/Scheme/loops-nested-2.ss
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(let loop ((a array))
|
||||
(if (pair? a)
|
||||
(let loop2 ((b (car a)))
|
||||
(cond ((null? b)
|
||||
(newline)
|
||||
(loop (cdr a)))
|
||||
((= 20 (car b))
|
||||
(newline))
|
||||
(else
|
||||
(display " ")(display (car b))
|
||||
(loop2 (cdr b)))))))
|
||||
8
Task/Loops-Nested/Smalltalk/loops-nested-1.st
Normal file
8
Task/Loops-Nested/Smalltalk/loops-nested-1.st
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|i|
|
||||
|
||||
i := 1.
|
||||
[:exit |
|
||||
Transcript showCR:i.
|
||||
i == 5 ifTrue:[ exit value:'stopped' ].
|
||||
i := i + 1.
|
||||
] loopWithExit
|
||||
15
Task/Loops-Nested/Smalltalk/loops-nested-2.st
Normal file
15
Task/Loops-Nested/Smalltalk/loops-nested-2.st
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
|i|
|
||||
|
||||
i := 1.
|
||||
[:exit1 |
|
||||
|j|
|
||||
|
||||
j := 0.
|
||||
[:exit2 |
|
||||
Transcript showCR:('i is %1 / j is %2' bindWith:i with:j).
|
||||
j == 5 ifTrue:[ exit2 value: nil ].
|
||||
i == 5 ifTrue:[ exit1 value: nil ].
|
||||
j := j + 1.
|
||||
] loopWithExit.
|
||||
i := i + 1
|
||||
] loopWithExit
|
||||
10
Task/Loops-Nested/Smalltalk/loops-nested-3.st
Normal file
10
Task/Loops-Nested/Smalltalk/loops-nested-3.st
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
!Block methodsFor:'looping'!
|
||||
loopWithExit
|
||||
"the receiver must be a block of one argument. It is evaluated in a loop forever,
|
||||
and is passed a block, which, if sent a value:-message, will exit the receiver block,
|
||||
returning the parameter of the value:-message. Used for loops with exit in the middle."
|
||||
|
||||
|exitBlock|
|
||||
|
||||
exitBlock := [:exitValue | ^ exitValue].
|
||||
[true] whileTrue:[ self value:exitBlock ]
|
||||
24
Task/Loops-Nested/Smalltalk/loops-nested-4.st
Normal file
24
Task/Loops-Nested/Smalltalk/loops-nested-4.st
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
|v result|
|
||||
|
||||
v := 1 to:20 collect:[:i |
|
||||
1 to:20 collect:[:j | Random nextIntegerBetween:1 and:20 ]
|
||||
].
|
||||
|
||||
result :=
|
||||
[:exit |
|
||||
1 to:20 do:[:row |
|
||||
1 to:20 do:[:col |
|
||||
|element|
|
||||
|
||||
(element := (v at:row) at:col) printCR.
|
||||
element == 20 ifTrue:[ exit value:(row @ col) ].
|
||||
]
|
||||
].
|
||||
nil
|
||||
] valueWithExit.
|
||||
|
||||
result isNil ifTrue:[
|
||||
'ouch - no 20 found' printCR.
|
||||
] ifFalse:[
|
||||
'20 found at ' print. result printCR
|
||||
]
|
||||
49
Task/Loops-Nested/Smalltalk/loops-nested-5.st
Normal file
49
Task/Loops-Nested/Smalltalk/loops-nested-5.st
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
"this simple implementation of a bidimensional array
|
||||
lacks controls over the indexes, but has a way of iterating
|
||||
over array's elements, from left to right and top to bottom"
|
||||
Object subclass: BiArray [
|
||||
|cols rows elements|
|
||||
BiArray class >> columns: columns rows: howManyRows [
|
||||
^ super basicNew init: columns per: howManyRows
|
||||
]
|
||||
init: columns per: howManyRows [
|
||||
cols := columns.
|
||||
rows := howManyRows.
|
||||
elements := Array new: ( columns * howManyRows )
|
||||
]
|
||||
calcIndex: biIndex [ "column, row (x,y) to linear"
|
||||
^ ( (biIndex at: 1) + (((biIndex at: 2) - 1) * cols) )
|
||||
]
|
||||
at: biIndex [ "biIndex is an indexable containing column row"
|
||||
^ elements at: (self calcIndex: biIndex).
|
||||
]
|
||||
directAt: i [ ^ elements at: i ]
|
||||
at: biIndex put: anObject [
|
||||
elements at: (self calcIndex: biIndex) put: anObject
|
||||
]
|
||||
whileTrue: aBlock do: anotherBlock [
|
||||
|i lim|
|
||||
i := 1. lim := rows * cols.
|
||||
[ ( i <= lim )
|
||||
& (aBlock value: (self directAt: i) )
|
||||
] whileTrue: [
|
||||
anotherBlock value: (self directAt: i).
|
||||
i := i + 1.
|
||||
]
|
||||
]
|
||||
].
|
||||
|
||||
|biarr|
|
||||
biarr := BiArray columns: 10 rows: 10.
|
||||
|
||||
"fill the array; this illustrates nested loop but not how to
|
||||
escape from them"
|
||||
1 to: 10 do: [ :c |
|
||||
1 to: 10 do: [ :r |
|
||||
biarr at: {c . r} put: (Random between: 1 and: 20)
|
||||
]
|
||||
].
|
||||
|
||||
"loop searching for 20; each block gets the element passed as argument"
|
||||
biarr whileTrue: [ :v | v ~= 20 ]
|
||||
do: [ :v | v displayNl ]
|
||||
14
Task/Loops-Nested/Tcl/loops-nested.tcl
Normal file
14
Task/Loops-Nested/Tcl/loops-nested.tcl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
set ary [subst [lrepeat 10 [lrepeat 5 {[expr int(rand()*20+1)]}]]]
|
||||
|
||||
try {
|
||||
foreach row $ary {
|
||||
foreach col $row {
|
||||
puts -nonewline [format %3s $col]
|
||||
if {$col == 20} {
|
||||
throw MULTIBREAK "we're done"
|
||||
}
|
||||
}
|
||||
puts ,
|
||||
}
|
||||
} trap MULTIBREAK {} {}
|
||||
puts " done"
|
||||
Loading…
Add table
Add a link
Reference in a new issue