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,8 @@
Shuffle[T] (arr : array[T]) : array[T]
{
def rnd = Random();
foreach (i in [0 .. (arr.Length - 2)])
arr[i] <-> arr[(rnd.Next(i, arr.Length))];
arr
}

View file

@ -0,0 +1,49 @@
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
import java.util.List
cards = [String -
'hA', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9', 'h10', 'hJ', 'hQ', 'hK' -
, 'cA', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10', 'cJ', 'cQ', 'cK' -
, 'dA', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'dJ', 'dQ', 'dK' -
, 'sA', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 'sJ', 'sQ', 'sK' -
]
cardsLen = cards.length
deck = ArrayList(cardsLen)
loop c_ = 0 to cardsLen - 1
deck.add(String(cards[c_]))
end c_
showHand(deck)
deck = ArrayList shuffle(deck)
showHand(deck)
return
method shuffle(deck = List) public static binary returns List
rn = Random()
dl = deck.size
loop i_ = dl - 1 to 1 by -1
j_ = rn.nextInt(i_)
__ = deck.get(i_)
deck.set(i_, deck.get(j_))
deck.set(j_, __)
end i_
return deck
method showHand(deck = ArrayList) public static binary
dl = deck.size
hl = dl % 4
loop c_ = 0 to dl - 1 by hl
d_ = c_ + hl
if d_ >= dl then d_ = dl
say ArrayList(deck.subList(c_, d_)).toString
end c_
say
return

View file

@ -0,0 +1,7 @@
let shuffle arr =
for n = Array.length arr - 1 downto 1 do
let k = Random.int (n + 1) in
let temp = arr.(n) in
arr.(n) <- arr.(k);
arr.(k) <- temp
done

View file

@ -0,0 +1,19 @@
declare
proc {Shuffle Arr}
Low = {Array.low Arr}
High = {Array.high Arr}
in
for I in High..Low;~1 do
J = Low + {OS.rand} mod (I - Low + 1)
OldI = Arr.I
in
Arr.I := Arr.J
Arr.J := OldI
end
end
X = {Tuple.toArray unit(0 1 2 3 4 5 6 7 8 9)}
in
{Show {Array.toRecord unit X}}
{Shuffle X}
{Show {Array.toRecord unit X}}

View file

@ -0,0 +1,10 @@
FY(v)={
forstep(n=#v,2,-1,
my(i=random(n)+1,t=v[i]);
v[i]=v[n];
v[n]=t
);
v
};
FY(vector(52,i,i))

View file

@ -0,0 +1,6 @@
declare T(0:10) fixed binary initial (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
declare (i, j, temp) fixed binary;
do i = lbound(T,1) to hbound(T,1);
j = min(random() * 12, 11);
temp = T(j); T(j) = T(i); T(i) = temp;
end;

View file

@ -0,0 +1,33 @@
program Knuth;
const
max = 10;
type
list = array [1..max] of integer;
procedure shuffle(var a: list);
var
i,k,tmp: integer;
begin
randomize;
for i := max downto 2 do begin
k := random(i) + 1;
if (a[i] <> a[k]) then begin
tmp := a[i]; a[i] := a[k]; a[k] := tmp
end
end
end;
{ Test and display }
var
a: list;
i: integer;
begin
for i := 1 to max do
a[i] := i;
shuffle(a);
for i := 1 to max do
write(a[i], ' ');
writeln
end.

View file

@ -0,0 +1,7 @@
sub shuffle (@a is copy) {
for 1 ..^ @a -> $n {
my $k = (0 .. $n).pick;
$k == $n or @a[$k, $n] = @a[$n, $k];
}
return @a;
}

View file

@ -0,0 +1 @@
my @deck = @cards.pick(*);

View file

@ -0,0 +1,9 @@
function shuffle ($a) {
$c = $a.Clone() # make copy to avoid clobbering $a
1..($c.Length - 1) | ForEach-Object {
$i = Get-Random -Minimum $_ -Maximum $c.Length
$c[$_-1],$c[$i] = $c[$i],$c[$_-1]
$c[$_-1] # return newly-shuffled value
}
$c[-1] # last value
}

View file

@ -0,0 +1,32 @@
EnableExplicit
Procedure KnuthShuffle(Array a(1))
Protected i, last = ArraySize(a())
For i = last To 1 Step -1
Swap a(i), a(Random(i))
Next
EndProcedure
Procedure.s ArrayToString(Array a(1))
Protected ret$, i, last = ArraySize(a())
ret$ = Str(a(0))
For i = 1 To last
ret$ + "," + Str(a(i))
Next
ProcedureReturn ret$
EndProcedure
#NumElements = 10
Dim a(#NumElements-1)
Define i
For i = 0 To #NumElements-1
a(i) = i
Next
KnuthShuffle(a())
Debug "shuffled: " + ArrayToString(a())

View file

@ -0,0 +1,18 @@
REBOL [
Title: "Fisher-Yates"
Purpose: {Fisher-Yates shuffling algorithm}
]
fisher-yates: func [b [block!] /local n i j k] [
n: length? b: copy b
i: n
while [i > 1] [
if i <> j: random i [
error? set/any 'k pick b j
change/only at b j pick b i
change/only at b i get/any 'k
]
i: i - 1
]
b
]

View file

@ -0,0 +1,20 @@
dim cards(52)
for i = 1 to 52 ' make deck
cards(i) = i
next
for i = 52 to 1 step -1 ' shuffle deck
r = int((rnd(1)*i) + 1)
if r <> i then
hold = cards(r)
cards(r) = cards(i)
cards(i) = hold
end if
next
print "== Shuffled Cards ==" ' print shuffled cards
for i = 1 to 52
print cards(i);" ";
if i mod 18 = 0 then print
next
print

View file

@ -0,0 +1,30 @@
* Library for random()
-include 'Random.sno'
* # String -> array
define('s2a(str,n)i') :(s2a_end)
s2a s2a = array(n); str = str ' '
sa1 str break(' ') . s2a<i = i + 1> span(' ') = :s(sa1)f(return)
s2a_end
* # Array -> string
define('a2s(a)i') :(a2s_end)
a2s a2s = a2s a<i = i + 1> ' ' :s(a2s)f(return)
a2s_end
* # Knuth shuffle in-place
define('shuffle(a)alen,n,k,tmp') :(shuffle_end)
shuffle n = alen = prototype(a);
sh1 k = convert(random() * alen,'integer') + 1
eq(a<n>,a<k>) :s(sh2)
tmp = a<n>; a<n> = a<k>; a<k> = tmp
sh2 n = gt(n,1) n - 1 :s(sh1)
shuffle = a :(return)
shuffle_end
* # Test and display
a = s2a('1 2 3 4 5 6 7 8 9 10',10)
output = a2s(a) '->'
shuffle(a)
output = a2s(a)
end

View file

@ -0,0 +1,15 @@
$$ MODE TUSCRIPT
oldnumbers=newnumbers="",range=20
LOOP nr=1,#range
oldnumbers=APPEND(oldnumbers,nr)
ENDLOOP
PRINT "before ",oldnumbers
LOOP r=#range,1,-1
RANDNR=RANDOM_NUMBERS (1,#r,1)
shuffle=SELECT (oldnumbers,#randnr,oldnumbers)
newnumbers=APPEND(newnumbers,shuffle)
ENDLOOP
PRINT "after ",newnumbers

View file

@ -0,0 +1,22 @@
# Shuffle array[@].
function shuffle {
integer i j t
((i = ${#array[@]}))
while ((i > 1)); do
((j = RANDOM)) # 0 <= j < 32768
((j < 32768 % i)) && continue # no modulo bias
((j %= i)) # 0 <= j < i
((i -= 1))
((t = array[i]))
((array[i] = array[j]))
((array[j] = t))
done
}
# Test program.
set -A array 11 22 33 44 55 66 77 88 99 110
shuffle
echo "${array[@]}"

View file

@ -0,0 +1 @@
shuffle = @iNX ~&l->r ^jrX/~&l ~&lK8PrC

View file

@ -0,0 +1,3 @@
#cast %s
example = shuffle 'abcdefghijkl'

View file

@ -0,0 +1,19 @@
function shuffle( a )
dim i
dim r
randomize timer
for i = lbound( a ) to ubound( a )
r = int( rnd * ( ubound( a ) + 1 ) )
if r <> i then
swap a(i), a(r)
end if
next
shuffle = a
end function
sub swap( byref a, byref b )
dim tmp
tmp = a
a = b
b = tmp
end sub

View file

@ -0,0 +1,14 @@
dim a
a = array( 1,2,3,4,5,6,7,8,9)
wscript.echo "before: ", join( a, ", " )
shuffle a
wscript.echo "after: ", join( a, ", " )
shuffle a
wscript.echo "after: ", join( a, ", " )
wscript.echo "--"
a = array( now(), "cow", 123, true, sin(1), 16.4 )
wscript.echo "before: ", join( a, ", " )
shuffle a
wscript.echo "after: ", join( a, ", " )
shuffle a
wscript.echo "after: ", join( a, ", " )

View file

@ -0,0 +1,42 @@
// Test main
#90 = Time_Tick // seed for random number generator
#99 = 20 // number of items in the array
IT("Before:") IN
for (#100 = 0; #100 < #99; #100++) {
#@100 = #100
Num_Ins(#@100, LEFT+NOCR) IT(" ")
}
IN
Call("SHUFFLE_NUMBERS")
IT("After:") IN
for (#100 = 0; #100 < #99; #100++) {
Num_Ins(#@100, LEFT+NOCR) IT(" ")
}
IN
Return
//--------------------------------------------------------------
// Shuffle numeric registers #0 to #nn
// #99 = number of registers to shuffle (nn-1)
//
:SHUFFLE_NUMBERS:
for (#91 = #99-1; #91 > 0; #91--) {
Call("RANDOM")
#101 = Return_Value
#102 = #@101; #@101 = #@91; #@91 = #102
}
Return
//--------------------------------------------------------------
// Generate random numbers in range 0 <= Return_Value < #91
// #90 = Seed (0 to 0x7fffffff)
// #91 = Scaling (0 to 0x10000)
//
:RANDOM:
#92 = 0x7fffffff / 48271
#93 = 0x7fffffff % 48271
#90 = (48271 * (#90 % #92) - #93 * (#90 / #92)) & 0x7fffffff
Return ((#90 & 0xffff) * #91 / 0x10000)