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,12 +1,13 @@
-- KNUTH SHUFFLE -------------------------------------------------------------
-- knuthShuffle :: [a] -> [a]
on knuthShuffle(lst)
on knuthShuffle(xs)
-- randomSwap :: [Int] -> Int -> [Int]
script randomSwap
on lambda(a, i)
on |λ|(a, i)
if i > 1 then
set iRand to random number from 1 to i
tell a
set tmp to item iRand
set item iRand to item i
@ -16,23 +17,35 @@ on knuthShuffle(lst)
else
a
end if
end lambda
end |λ|
end script
foldr(randomSwap, lst, range(1, length of lst))
foldr(randomSwap, xs, enumFromTo(1, length of xs))
end knuthShuffle
-- TEST
-- TEST ----------------------------------------------------------------------
on run
knuthShuffle(["alpha", "beta", "gamma", "delta", "epsilon", ¬
"zeta", "eta", "theta", "iota", "kappa", "lambda", "mu"])
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- GENERIC LIBRARY FUNCTIONS
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
@ -40,7 +53,7 @@ on foldr(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
@ -53,21 +66,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range

View file

@ -0,0 +1,14 @@
10 RAND
20 LET A$=""
30 FOR I=1 TO 26
40 LET A$=A$+CHR$ (37+I)
50 NEXT I
60 PRINT A$
70 FOR I=26 TO 2 STEP -1
80 LET J=1+INT (RND*I)
90 LET T$=A$(I)
100 LET A$(I)=A$(J)
110 LET A$(J)=T$
120 PRINT AT 0,I-1;CHR$ (CODE A$(I)+128)
130 PRINT AT 0,J-1;CHR$ (CODE A$(J)+128)
140 NEXT I

View file

@ -1,29 +1,28 @@
#import system.
#import system'routines.
#import extensions.
import system'routines.
import extensions.
#symbol(const)MAX = 10.
const int MAX = 10.
#class(extension) randomOp
extension randomOp
{
#method randomize
randomize
[
#var max := self length.
var max := self length.
0 till:max &doEach: i
0 till:max do(:i)
[
#var j := randomGenerator eval:i:max.
var j := randomGenerator eval(i,max).
self exchange:i:j.
self exchange(i,j)
].
^ self.
^ self
]
}
#symbol program =
program =
[
#var a := Array new:MAX set &every:(&index:i) [ i ].
var a := Array new:MAX; populate(:i)( i ).
console writeLine:(a randomize).
console printLine(a randomize).
].

View file

@ -0,0 +1,26 @@
Public Sub Main()
Dim iTotal As Integer = 40
Dim iCount, iRand1, iRand2 As Integer
Dim iArray As New Integer[]
For iCount = 0 To iTotal
iArray.add(iCount)
Next
Print "Original = ";
For iCount = 0 To iArray.Max
If iCount = iArray.max Then Print iArray[iCount]; Else Print iArray[iCount] & ",";
Next
For iCount = iTotal DownTo 0
iRand1 = Rand(iTotal)
iRand2 = Rand(iTotal)
Swap iArray[iRand1], iArray[iRand2]
Next
Print gb.NewLine & "Shuffled = ";
For iCount = 0 To iArray.Max
If iCount = iArray.max Then Print iArray[iCount]; Else Print iArray[iCount] & ",";
Next
End

View file

@ -1,11 +1,9 @@
func shuffle(a) {
{ |n|
var k = (n+1 -> irand);
k == n || (a[k, n] = a[n, k]);
} * a.end;
return a;
func knuth_shuffle(a) {
for i (a.len ^.. 1) {
var j = i.irand
a[i, j] = a[j, i]
}
return a
}
say shuffle(@(1..10));
say knuth_shuffle(@(1..10))

View file

@ -0,0 +1,7 @@
fcn kshuffle(xs){foreach i in ([xs.len()-1..1,-1]){
xs.swap(i,(0).random(0,i+1))}
xs
}
fcn kshufflep(xs){[xs.len()-1..1,-1].pump(Void,'wrap(i){
xs.swap(i,(0).random(0,i+1))})
}