Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import std.stdio, std.random;
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.random;
|
||||
|
||||
auto a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
a.randomShuffle();
|
||||
writeln(a);
|
||||
a.randomShuffle;
|
||||
a.writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
import std.stdio, std.algorithm, std.random;
|
||||
import std.stdio, std.algorithm, std.random, std.range;
|
||||
|
||||
void shuffle(Range)(Range r) {
|
||||
foreach_reverse (i; 1 .. r.length)
|
||||
swap(r[i], r[uniform(0, i + 1)]);
|
||||
void knuthShuffle(Range)(Range r)
|
||||
if (isRandomAccessRange!Range && hasLength!Range &&
|
||||
hasSwappableElements!Range) {
|
||||
foreach_reverse (immutable i; 1 .. r.length)
|
||||
r[i].swap(r[uniform(0, i + 1)]);
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
a.shuffle();
|
||||
writeln(a);
|
||||
a.knuthShuffle;
|
||||
a.writeln;
|
||||
}
|
||||
|
|
|
|||
34
Task/Knuth-shuffle/NetRexx/knuth-shuffle-2.netrexx
Normal file
34
Task/Knuth-shuffle/NetRexx/knuth-shuffle-2.netrexx
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* NetRexx ------------------------------------------------------------
|
||||
* 08.01.2014 Walter Pachl modified to show state development a la Rexx
|
||||
*--------------------------------------------------------------------*/
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
import java.util.List
|
||||
|
||||
cards = [String '1','2','3','4','5','6','7','8','9','10']
|
||||
cardsLen = cards.length
|
||||
deck = ArrayList(cardsLen)
|
||||
loop c_ = 0 to cardsLen - 1
|
||||
deck.add(String(cards[c_]))
|
||||
end c_
|
||||
|
||||
showHand(deck,'In ')
|
||||
deck = ArrayList shuffle(deck)
|
||||
showHand(deck,'Out')
|
||||
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_, __)
|
||||
say i_ j_ ArrayList(deck.subList(0,i_+1)).toString
|
||||
end i_
|
||||
return deck
|
||||
|
||||
method showHand(deck = ArrayList,tag=REXX) public static binary
|
||||
say tag ArrayList(deck.subList(0,deck.size)).toString
|
||||
return
|
||||
23
Task/Knuth-shuffle/PL-I/knuth-shuffle-2.pli
Normal file
23
Task/Knuth-shuffle/PL-I/knuth-shuffle-2.pli
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
kn: Proc Options(main);
|
||||
/*--------------------------------------------------------------------
|
||||
* 07.01.2014 Walter Pachl translated from REXX version 2
|
||||
* Iteration i: only the first i elements are candidates for swapping
|
||||
*-------------------------------------------------------------------*/
|
||||
Dcl T(10) Bin Fixed(15) Init(1,2,3,4,5,6,7,8,9,10);
|
||||
Dcl (i,j,temp) Bin Fixed(15) init(0);
|
||||
Dcl h Char(6);
|
||||
Call show('In',10); /* show start */
|
||||
do i = 10 To 2 By -1; /* shuffle */
|
||||
j=random()*i+1;
|
||||
Put string(h)Edit(i,j)(f(2),f(3));
|
||||
temp=t(i); t(i)=t(j); t(j)=temp; /* t(i) <-> t(j) */
|
||||
Call show(h,i); /* show intermediate states */
|
||||
end;
|
||||
Call show('Out',10); /* show final state */
|
||||
|
||||
show: Proc(txt,n);
|
||||
Dcl txt Char(*);
|
||||
Dcl n Bin Fixed(15);
|
||||
Put Edit(txt,(t(k) do k=1 To n))(Skip,a(7),10(f(3)));
|
||||
End;
|
||||
end;
|
||||
23
Task/Knuth-shuffle/REXX/knuth-shuffle-1.rexx
Normal file
23
Task/Knuth-shuffle/REXX/knuth-shuffle-1.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/*REXX program shuffles a deck of playing cards using the Knuth shuffle.*/
|
||||
rank='A 2 3 4 5 6 7 8 9 10 J Q K' /*pips of the playing cards. */
|
||||
suit='♣♠♦♥' /*suit " " " " */
|
||||
parse arg seed .; if seed\=='' then call random ,,seed /*repeatability?*/
|
||||
say '────────────────── getting a new deck out of the box ···'
|
||||
deck.1='highJoker' /*good decks have a color joker, */
|
||||
deck.2='lowJoker' /*··· and a black & white joker. */
|
||||
cards=2 /*now, two cards are in the deck.*/
|
||||
do j =1 for length(suit)
|
||||
do k=1 for words(rank); cards=cards+1
|
||||
deck.cards=substr(suit,j,1)word(rank,k)
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
call showDeck
|
||||
say '────────────────── shuffling' cards "cards ···"
|
||||
do s=cards by -1 to 2; rand=random(1,s)
|
||||
parse value deck.rand deck.s with deck.s deck.rand
|
||||
/* [↑] swap two cards in the deck*/
|
||||
end /*s*/
|
||||
call showDeck
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SHOWDECK subroutine─────────────────*/
|
||||
showDeck: _=; do m=1 for cards; _=_ deck.m; end /*m*/; say _; say; return
|
||||
30
Task/Knuth-shuffle/REXX/knuth-shuffle-2.rexx
Normal file
30
Task/Knuth-shuffle/REXX/knuth-shuffle-2.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*REXX program shuffles a deck of playing cards using the Knuth shuffle.*/
|
||||
rank = 'ace deuce trey 4 5 6 7 8 9 10 jack queen king' /*use pip names.*/
|
||||
suit = 'club spade diamond heart' /* " suit " */
|
||||
say '────────────────── getting a new deck out of the box ···'
|
||||
deck.1 = ' color joker' /*good decks have a color joker, */
|
||||
deck.2 = ' b&w joker' /*··· and a black & white joker. */
|
||||
cards=2 /*now, two cards are in the deck.*/
|
||||
do j =1 for words(suit)
|
||||
do k=1 for words(rank); cards=cards+1 /*bump counter.*/
|
||||
deck.cards=right(word(suit,j),7) word(rank,k) /*assign.*/
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
|
||||
call showDeck 'ace' /*inserts blank when ACE is found*/
|
||||
say '────────────────── shuffling' cards "cards ···"
|
||||
|
||||
do s=cards by -1 to 2; rand=random(1,s) /*get random number for swap*/
|
||||
_=deck.rand; deck.rand=deck.s; deck.s=_ /*swap 2 cards in card deck.*/
|
||||
end /*s*/
|
||||
|
||||
call showDeck
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SHOWDECK subroutine─────────────────*/
|
||||
showDeck: parse arg break; say /*get sep card, shows blank line*/
|
||||
do m=1 for cards /*traipse through the deck. */
|
||||
if pos(break,deck.m)\==0 then say /*a blank: easier to read cards.*/
|
||||
say 'card' right(m,2) '───►' deck.m /*display a particular card. */
|
||||
end /*m*/
|
||||
say /*show a trailing blank line. */
|
||||
return
|
||||
23
Task/Knuth-shuffle/REXX/knuth-shuffle-3.rexx
Normal file
23
Task/Knuth-shuffle/REXX/knuth-shuffle-3.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* REXX ---------------------------------------------------------------
|
||||
* 05.01.2014 Walter Pachl
|
||||
* borrow one improvement from version 1
|
||||
* 06.01.2014 removed -"- (many tests cost more than few "swaps")
|
||||
*--------------------------------------------------------------------*/
|
||||
Call random ,,123456 /* seed for random */
|
||||
Do i=1 To 10; a.i=i; End; /* fill array */
|
||||
Call show 'In',10 /* show start */
|
||||
do i = 10 To 2 By -1 /* shuffle */
|
||||
j=random(i-1)+1;
|
||||
h=right(i,2) right(j,2)
|
||||
Parse Value a.i a.j With a.j a.i /* a.i <-> a.j */
|
||||
Call show h,i /* show intermediate states */
|
||||
end;
|
||||
Call show 'Out',10 /* show fomaö state */
|
||||
Exit
|
||||
|
||||
show: Procedure Expose a.
|
||||
Parse Arg txt,n
|
||||
ol=left(txt,6);
|
||||
Do k=1 To n; ol=ol right(a.k,2); End
|
||||
Say ol
|
||||
Return
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
/*REXX program shuffles a deck of playing cards using the Knuth shuffle.*/
|
||||
rank='ace deuce trey 4 5 6 7 8 9 10 jack queen king'
|
||||
suit='club spade diamond heart'
|
||||
say '────────────────── getting a new deck out of the box...'
|
||||
deck.1=' color joker' /*good decks have a color joker, */
|
||||
deck.2=' b&w joker' /*∙∙∙ and a black & white joker. */
|
||||
cards=2 /*now, two cards are in the deck.*/
|
||||
do j =1 for words(suit)
|
||||
do k=1 for words(rank)
|
||||
cards=cards+1
|
||||
deck.cards=right(word(suit,j),7) word(rank,k)
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
|
||||
call showDeck 'ace' /*inserts blank when ACE is found*/
|
||||
say '────────────────── shuffling' cards "cards..."
|
||||
|
||||
do s=cards by -1 to 1; rand=random(1,s)
|
||||
if rand\==s then do /*swap two cards in the card deck*/
|
||||
_=deck.rand
|
||||
deck.rand=deck.s
|
||||
deck.s=_
|
||||
end
|
||||
end /*s*/
|
||||
|
||||
call showDeck
|
||||
say '────────────────── ready to play schafkopf (take out jokers first).'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
|
||||
/*──────────────────────────────────SHOWDECK subroutine─────────────────*/
|
||||
showDeck: parse arg break; say
|
||||
do m=1 for cards
|
||||
if pos(break,deck.m)\==0 then say /*blank, easier to read cards*/
|
||||
say 'card' right(m,2) '───►' deck.m
|
||||
end /*m*/
|
||||
say
|
||||
return
|
||||
22
Task/Knuth-shuffle/Rust/knuth-shuffle.rust
Normal file
22
Task/Knuth-shuffle/Rust/knuth-shuffle.rust
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use std::iter;
|
||||
use std::rand;
|
||||
use std::rand::Rng;
|
||||
use std::vec;
|
||||
|
||||
fn knuth_shuffle<T>(v: &mut [T]) {
|
||||
let mut rng = rand::rng();
|
||||
let l = v.len();
|
||||
|
||||
for n in iter::range(0, l) {
|
||||
let i = rng.gen_range(0, l - n);
|
||||
v.swap(i, l - n - 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut v = vec::from_fn(10, |i| i);
|
||||
|
||||
println!("before: {:?}", v);
|
||||
knuth_shuffle(v);
|
||||
println!("after: {:?}", v);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue