September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
12
Task/Loops-Nested/BASIC/loops-nested-2.basic
Normal file
12
Task/Loops-Nested/BASIC/loops-nested-2.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
10 DIM A$(20,20)
|
||||
20 FOR I=1 TO 20
|
||||
30 FOR J=1 TO 20
|
||||
40 LET A$(I,J)=CHR$ (1+INT (RND*20))
|
||||
50 NEXT J
|
||||
60 NEXT I
|
||||
70 FOR I=1 TO 20
|
||||
80 FOR J=1 TO 20
|
||||
90 PRINT CODE A$(I,J);" ";
|
||||
100 IF CODE A$(I,J)=20 THEN GOTO 130
|
||||
110 NEXT J
|
||||
120 NEXT I
|
||||
46
Task/Loops-Nested/Bc/loops-nested.bc
Normal file
46
Task/Loops-Nested/Bc/loops-nested.bc
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
s = 1 /* Seed of the random number generator */
|
||||
|
||||
/* Random number from 1 to 20. */
|
||||
define r() {
|
||||
auto r
|
||||
while (1) {
|
||||
/*
|
||||
* Formula (from POSIX) for random numbers of low
|
||||
* quality, from 0 to 32767.
|
||||
*/
|
||||
s = (s * 1103515245 + 12345) % 4294967296
|
||||
r = (s / 65536) % 32768
|
||||
|
||||
/* Prevent modulo bias. */
|
||||
if (r >= 32768 % 20) break
|
||||
}
|
||||
return ((r % 20) + 1)
|
||||
}
|
||||
|
||||
r = 5 /* Total rows */
|
||||
c = 5 /* Total columns */
|
||||
|
||||
/* Fill array a[] with random numbers from 1 to 20. */
|
||||
for (i = 0; i < r; i++) {
|
||||
for (j = 0; j < c; j++) {
|
||||
a[i * c + j] = r()
|
||||
}
|
||||
}
|
||||
|
||||
/* Find a 20. */
|
||||
b = 0
|
||||
for (i = 0; i < r; i++) {
|
||||
for (j = 0; j < c; j++) {
|
||||
v = a[i * c + j]
|
||||
v /* Print v and a newline. */
|
||||
if (v == 20) {
|
||||
b = 1
|
||||
break
|
||||
}
|
||||
}
|
||||
if (b) break
|
||||
/* Print "==" and a newline. */
|
||||
"==
|
||||
"
|
||||
}
|
||||
quit
|
||||
68
Task/Loops-Nested/Dc/loops-nested.dc
Normal file
68
Task/Loops-Nested/Dc/loops-nested.dc
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
1 ss [Seed of the random number generator.]sz
|
||||
|
||||
[*
|
||||
* lrx -- (number)
|
||||
* Push a random number from 1 to 20.
|
||||
*]sz
|
||||
[
|
||||
[ [If preventing modulo bias:]sz
|
||||
sz [Drop this random number.]sz
|
||||
lLx [Loop.]sz
|
||||
]SI
|
||||
[ [Loop:]sz
|
||||
[*
|
||||
* Formula (from POSIX) for random numbers of low quality.
|
||||
* Push a random number from 0 to 32767.
|
||||
*]sz
|
||||
ls 1103515245 * 12345 + 4294967296 % ss
|
||||
ls 65536 / 32768 %
|
||||
|
||||
d 32768 20 % >I [Prevent modulo bias.]sz
|
||||
]d SL x
|
||||
20 % 1 + [Be from 1 to 20.]sz
|
||||
LLsz LIsz [Restore L, I.]sz
|
||||
]sr
|
||||
|
||||
|
||||
5 sb [b = Total rows]sz
|
||||
5 sc [c = Total columns]sz
|
||||
|
||||
[Fill array a[] with random numbers from 1 to 20.]sz
|
||||
[ [Inner loop for j:]sz
|
||||
lrx [Push random number.]sz
|
||||
li lc * lj + [Push index of a[i, j].]sz
|
||||
:a [Put in a[].]sz
|
||||
lj 1 + d sj [j += 1]sz
|
||||
lc >I [Loop while c > j.]sz
|
||||
]sI
|
||||
[ [Outer loop for i:]sz
|
||||
0 d sj [j = 0]sz
|
||||
lc >I [Enter inner loop.]sz
|
||||
li 1 + d si [i += 1]sz
|
||||
lb >L [Loop while b > i.]sz
|
||||
]sL
|
||||
0 d si [i = 0]sz
|
||||
lb >L [Enter outer loop.]sz
|
||||
|
||||
[Find a 20.]sz
|
||||
[ [If detecting a 20:]sz
|
||||
li lj + 3 + Q [Break outer loop.]sz
|
||||
]sD
|
||||
[ [Inner loop for j:]sz
|
||||
li lc * lj + [Push index of a[i,j].]sz
|
||||
;a [Push value from a[].]sz
|
||||
p [Print value and a newline.]sz
|
||||
20 =D [Detect a 20.]sz
|
||||
lj 1 + d sj [j += 1]sz
|
||||
lc >I [Loop while c > j.]sz
|
||||
]sI
|
||||
[ [Outer loop for i:]sz
|
||||
0 d sj [j = 0]sz
|
||||
lc >I [Enter inner loop.]sz
|
||||
[==
|
||||
]P [Print "==" and a newline.]sz
|
||||
li 1 + d si [i += 1]sz
|
||||
lb >L [Loop while b > i.]sz
|
||||
]sL
|
||||
0 d si [i = 0]sz
|
||||
lb >L [Enter outer loop.]sz
|
||||
25
Task/Loops-Nested/Gambas/loops-nested.gambas
Normal file
25
Task/Loops-Nested/Gambas/loops-nested.gambas
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Public Sub Main()
|
||||
Dim siArray As New Short[5, 5]
|
||||
Dim siCount0, siCount1 As Short
|
||||
Dim bBreak As Boolean
|
||||
|
||||
For siCount0 = 0 To 4
|
||||
For siCount1 = 0 To 4
|
||||
siArray[siCount0, siCount1] = Rand(1, 20)
|
||||
siArray[siCount0, siCount1] = Rand(1, 20)
|
||||
Next
|
||||
Next
|
||||
|
||||
For siCount0 = 0 To 4
|
||||
For siCount1 = 0 To 4
|
||||
If siArray[siCount0, siCount1] = 20 Then
|
||||
bBreak = True
|
||||
Break
|
||||
Endif
|
||||
Next
|
||||
If bBreak Then Break
|
||||
Next
|
||||
|
||||
Print "Row " & Str(siCount0) & " column " & Str(siCount1) & " = 20"
|
||||
|
||||
End
|
||||
11
Task/Loops-Nested/Jq/loops-nested-1.jq
Normal file
11
Task/Loops-Nested/Jq/loops-nested-1.jq
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Given an m x n matrix,
|
||||
# produce a stream of the matrix elements (taken row-wise)
|
||||
# up to but excluding the first occurrence of $max
|
||||
def stream($max):
|
||||
. as $matrix
|
||||
| length as $m
|
||||
| (.[0] | length) as $n
|
||||
| label $ok
|
||||
| {i: range(0;$m), j: range(0;$n)}
|
||||
| $matrix[.i][.j] as $m
|
||||
| if $m == $max then break $ok else $m end ;
|
||||
9
Task/Loops-Nested/Jq/loops-nested-2.jq
Normal file
9
Task/Loops-Nested/Jq/loops-nested-2.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# Create an array of arrays by using the items in the stream, s,
|
||||
# to create successive rows, each row having at most n items.
|
||||
def reshape(s; n):
|
||||
reduce s as $s ({i:0, j:0, matrix: []};
|
||||
.matrix[.i][.j] = $s
|
||||
| if .j + 1 == n then .i += 1 | .j = 0
|
||||
else .j += 1
|
||||
end)
|
||||
| .matrix;
|
||||
10
Task/Loops-Nested/Jq/loops-nested-3.jq
Normal file
10
Task/Loops-Nested/Jq/loops-nested-3.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Create an m x n matrix filled with numbers in [1 .. max]
|
||||
def randomMatrix(m; n; max):
|
||||
reshape(limit(m * n; rand(max) + 1); n);
|
||||
|
||||
# Present the matrix up to but excluding the first occurrence of $max
|
||||
def show($m; $n; $max):
|
||||
reshape( randomMatrix($m; $n; $max) | stream($max); $n)[] ;
|
||||
|
||||
# Main program for the problem at hand.
|
||||
show(20; 4; 20)
|
||||
17
Task/Loops-Nested/Jq/loops-nested-4.jq
Normal file
17
Task/Loops-Nested/Jq/loops-nested-4.jq
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# LCG::Microsoft generates 15-bit integers using the same formula
|
||||
# as rand() from the Microsoft C Runtime.
|
||||
# Input: [ count, state, random ]
|
||||
def next_rand_Microsoft:
|
||||
.[0] as $count
|
||||
| ((214013 * .[1]) + 2531011) % 2147483648 # mod 2^31
|
||||
| [$count+1 , ., (. / 65536 | floor) ];
|
||||
|
||||
def rand_Microsoft(seed):
|
||||
[0,seed]
|
||||
| next_rand_Microsoft # the seed is not so random
|
||||
| recurse( next_rand_Microsoft )
|
||||
| .[2];
|
||||
|
||||
# A random integer in [0 ... (n-1)]:
|
||||
# rand_Microsoft returns an integer in 0 .. 32767
|
||||
def rand(n): n * (rand_Microsoft($seed|tonumber) / 32768) | trunc;
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
import math, strutils
|
||||
|
||||
const arrSize = 10
|
||||
|
||||
var a: array[0..arrSize-1, array[0..arrSize-1, int]]
|
||||
var s: string = ""
|
||||
|
||||
randomize() # different results each time this runs
|
||||
|
||||
for i in 0 .. arrSize-1:
|
||||
for j in countup(0,arrSize-1):
|
||||
a[i][j] = random(20)+1
|
||||
|
||||
block outer:
|
||||
for i in countup(0,arrSize-1):
|
||||
for j in 0 .. arrSize-1:
|
||||
if a[i][j] < 10:
|
||||
s.add(" ")
|
||||
addf(s,"$#",$a[i][j])
|
||||
if a[i][j] == 20:
|
||||
break outer
|
||||
s.add(", ")
|
||||
s.add("\n")
|
||||
echo(s)
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
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";
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
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";
|
||||
15
Task/Loops-Nested/SPL/loops-nested.spl
Normal file
15
Task/Loops-Nested/SPL/loops-nested.spl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
'fill array
|
||||
mx,my = 30
|
||||
> y, 1..my
|
||||
> x, 1..mx
|
||||
a[x,y] = #.rnd(20)+1
|
||||
<
|
||||
<
|
||||
'scan array
|
||||
> y, 1..my
|
||||
> x, 1..mx
|
||||
#.output("x=",x,", y=",y, ", a=",a[x,y])
|
||||
<< a[x,y] = 20
|
||||
<
|
||||
<< x!>mx
|
||||
<
|
||||
6
Task/Loops-Nested/Stata/loops-nested-1.stata
Normal file
6
Task/Loops-Nested/Stata/loops-nested-1.stata
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
matrix a=J(20,20,0)
|
||||
forv i=1/20 {
|
||||
forv j=1/20 {
|
||||
matrix a[`i',`j']=runiformint(1,20)
|
||||
}
|
||||
}
|
||||
16
Task/Loops-Nested/Stata/loops-nested-2.stata
Normal file
16
Task/Loops-Nested/Stata/loops-nested-2.stata
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
local q 0
|
||||
forv i=1/20 {
|
||||
forv j=1/20 {
|
||||
display "check `i',`j'"
|
||||
if el("a",`i',`j')==20 {
|
||||
display "found at `i',`j'"
|
||||
local q 1
|
||||
continue, break
|
||||
}
|
||||
}
|
||||
|
||||
if `q' continue, break
|
||||
}
|
||||
if !`q' {
|
||||
display "not found"
|
||||
}
|
||||
17
Task/Loops-Nested/Stata/loops-nested-3.stata
Normal file
17
Task/Loops-Nested/Stata/loops-nested-3.stata
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
local q 0
|
||||
local i=1
|
||||
while !`q' & `i'<=20 {
|
||||
local j=1
|
||||
while !`q' & `j'<=20 {
|
||||
display "check `i',`j'"
|
||||
if el("a",`i',`j')==20 {
|
||||
display "found at `i',`j'"
|
||||
local q 1
|
||||
}
|
||||
local ++j
|
||||
}
|
||||
local ++i
|
||||
}
|
||||
if !`q' {
|
||||
display "not found"
|
||||
}
|
||||
18
Task/Loops-Nested/Stata/loops-nested-4.stata
Normal file
18
Task/Loops-Nested/Stata/loops-nested-4.stata
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
capture {
|
||||
forv i=1/20 {
|
||||
forv j=1/20 {
|
||||
display "check `i',`j'"
|
||||
if el("a",`i',`j')==20 {
|
||||
display "found at `i',`j'"
|
||||
exit -1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if _rc==-1 {
|
||||
// value was found
|
||||
}
|
||||
else if _rc==0 {
|
||||
display "not found"
|
||||
}
|
||||
else exit _rc
|
||||
52
Task/Loops-Nested/Stata/loops-nested-5.stata
Normal file
52
Task/Loops-Nested/Stata/loops-nested-5.stata
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
function findval1(a,x,i0,j0) {
|
||||
n=rows(a)
|
||||
p=cols(a)
|
||||
for (i=1; i<=n; i++) {
|
||||
for (j=1; j<=p; j++) {
|
||||
if (a[i,j]==x) {
|
||||
i0=i
|
||||
j0=j
|
||||
return(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
return(0)
|
||||
}
|
||||
|
||||
function findval2(a,x,i0,j0) {
|
||||
n=rows(a)
|
||||
p=cols(a)
|
||||
q=0
|
||||
for (i=1; i<=n; i++) {
|
||||
for (j=1; j<=p; j++) {
|
||||
if (a[i,j]==x) {
|
||||
i0=i
|
||||
j0=j
|
||||
q=1
|
||||
goto END
|
||||
}
|
||||
}
|
||||
}
|
||||
END:
|
||||
return(q)
|
||||
}
|
||||
|
||||
function findval3(a,x,i0,j0) {
|
||||
n=rows(a)
|
||||
p=cols(a)
|
||||
q=0
|
||||
for (i=1; i<=n; i++) {
|
||||
for (j=1; j<=p; j++) {
|
||||
if (a[i,j]==x) {
|
||||
i0=i
|
||||
j0=j
|
||||
q=1
|
||||
break
|
||||
}
|
||||
}
|
||||
if (q) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return(q)
|
||||
}
|
||||
4
Task/Loops-Nested/Stata/loops-nested-6.stata
Normal file
4
Task/Loops-Nested/Stata/loops-nested-6.stata
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
a=st_matrix("a")
|
||||
findval1(a,20,i=.,j=.)
|
||||
findval2(a,20,i=.,j=.)
|
||||
findval3(a,20,i=.,j=.)
|
||||
11
Task/Loops-Nested/Zkl/loops-nested.zkl
Normal file
11
Task/Loops-Nested/Zkl/loops-nested.zkl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
const N=5;
|
||||
var rows=(0).pump(N,List, (0).pump.fp(N,List,(1).random.fpM("1-",21)) );
|
||||
try{
|
||||
foreach r in (N){
|
||||
foreach c in (N){
|
||||
x:=rows[r][c]; x.print(",");
|
||||
if (x==20) { println("Found it!"); throw(Exception.Generic); }
|
||||
}
|
||||
}
|
||||
println("Not found");
|
||||
}catch(Generic){}
|
||||
Loading…
Add table
Add a link
Reference in a new issue