2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
49
Task/Knuths-algorithm-S/Elena/knuths-algorithm-s.elena
Normal file
49
Task/Knuths-algorithm-S/Elena/knuths-algorithm-s.elena
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#import system.
|
||||
#import system'dynamic.
|
||||
#import extensions.
|
||||
#import system'routines.
|
||||
#import system'collections.
|
||||
|
||||
#class(extension)algorithmOp
|
||||
{
|
||||
#method s_of_n
|
||||
[
|
||||
#var counter := Integer new.
|
||||
|
||||
^ ArrayList new mix &into:
|
||||
{
|
||||
eval : n
|
||||
[
|
||||
counter += 1.
|
||||
|
||||
(this length < self)
|
||||
? [ this += n. ]
|
||||
! [
|
||||
(randomGenerator eval:counter < self)
|
||||
? [ this@(randomGenerator eval:self) := n. ].
|
||||
].
|
||||
|
||||
^ this array.
|
||||
]
|
||||
}.
|
||||
]
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
#var bin := Array new:10 set &every:(&index:n) [ Integer new ].
|
||||
0 till:10000 &doEach: trial
|
||||
[
|
||||
#var s_of_n := 3 s_of_n.
|
||||
|
||||
0 till:10 &doEach:n
|
||||
[
|
||||
#var sample := s_of_n eval:n.
|
||||
|
||||
(n == 9)
|
||||
? [ sample run &each: i [ bin@i += 1. ]. ].
|
||||
].
|
||||
].
|
||||
|
||||
console writeLine:bin.
|
||||
].
|
||||
25
Task/Knuths-algorithm-S/Elixir/knuths-algorithm-s.elixir
Normal file
25
Task/Knuths-algorithm-S/Elixir/knuths-algorithm-s.elixir
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
defmodule Knuth do
|
||||
def s_of_n_creator(n), do: {n, 1, []}
|
||||
|
||||
def s_of_n({n, i, ys}, x) do
|
||||
cond do
|
||||
i <= n -> {n, i+1, [x|ys]}
|
||||
|
||||
:rand.uniform(i) <= n ->
|
||||
{n, i+1, List.replace_at(ys, :rand.uniform(n)-1, x)}
|
||||
|
||||
true -> {n, i+1, ys}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
results = Enum.reduce(1..100000, %{}, fn _, freq ->
|
||||
{_, _, xs} = Enum.reduce(1..10, Knuth.s_of_n_creator(3), fn x, s ->
|
||||
Knuth.s_of_n(s, x)
|
||||
end)
|
||||
Enum.reduce(xs, freq, fn x, freq ->
|
||||
Map.put(freq, x, (freq[x] || 0) + 1)
|
||||
end)
|
||||
end)
|
||||
|
||||
IO.inspect results
|
||||
|
|
@ -1,3 +1,8 @@
|
|||
s_of_n_creator=: 1 :0
|
||||
ctx=: conew&'inefficient' m
|
||||
s_of_n__ctx
|
||||
)
|
||||
|
||||
coclass'inefficient'
|
||||
create=:3 :0
|
||||
N=: y
|
||||
|
|
@ -7,19 +12,13 @@ coclass'inefficient'
|
|||
|
||||
s_of_n=:3 :0
|
||||
K=: K+1
|
||||
if. N>#ITEMS do.
|
||||
if. N>:#ITEMS do.
|
||||
ITEMS=: ITEMS,y
|
||||
else.
|
||||
if. (N%K)>?0 do.
|
||||
ITEMS=: (((i.#ITEMS)-.?N){ITEMS),y
|
||||
ITEMS=: ((<<<?N){ITEMS),y
|
||||
else.
|
||||
ITEMS
|
||||
end.
|
||||
end.
|
||||
)
|
||||
|
||||
|
||||
s_of_n_creator_base_=: 1 :0
|
||||
ctx=: conew&'inefficient' m
|
||||
s_of_n__ctx
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ run=:3 :0
|
|||
)
|
||||
|
||||
(~.,._1 + #/.~) (i.10),,D=:run"0 i.1e5
|
||||
0 30099
|
||||
1 29973
|
||||
2 29795
|
||||
3 29995
|
||||
4 29996
|
||||
5 30289
|
||||
6 29903
|
||||
7 29993
|
||||
8 30215
|
||||
9 29742
|
||||
0 40119
|
||||
1 40050
|
||||
2 40163
|
||||
3 57996
|
||||
4 42546
|
||||
5 40990
|
||||
6 38680
|
||||
7 36416
|
||||
8 33172
|
||||
9 29868
|
||||
|
|
|
|||
|
|
@ -1,41 +1,40 @@
|
|||
/*REXX program using Knuth's algorithm S (a random sampling N of M items). */
|
||||
/*REXX program using Knuth's algorithm S (a random sampling N of M items).*/
|
||||
parse arg trials size . /*obtain optional arguments from the CL*/
|
||||
if trials=='' then trials=100000 /*Not specified? Then use the default.*/
|
||||
if size=='' then size=3 /* " " " " " " */
|
||||
#.=0 /*initialize an array of freq counters.*/
|
||||
do trials /*OK, now let's light this candle. */
|
||||
call SofN_creator size /*create initial list of N items. */
|
||||
if size=='' then size= 3 /* " " " " " " */
|
||||
#.=0 /*initialize frequency counter array. */
|
||||
do trials /*OK, now let's light this candle. */
|
||||
call s_of_n_creator size /*create an initial list of N items. */
|
||||
|
||||
do gener=0 for 10 /*and then call SofN for each digit. */
|
||||
call SofN gener /*call SofN with a single decimal dig*/
|
||||
do gener=0 for 10
|
||||
call s_of_n gener /*call s_of_n with a single decimal dig*/
|
||||
end /*gener*/
|
||||
|
||||
do count=1 for size /*let's examine what SofN generated. */
|
||||
_=!.count /*get a digit from the Nth item, and */
|
||||
#._=#._+1 /* ··· count it, of course. */
|
||||
_=!.count /*get a decimal digit from the Nth */
|
||||
#._=#._+1 /* ··· item, and count it, of course.*/
|
||||
end /*count*/
|
||||
end /*trials*/
|
||||
|
||||
say "Using Knuth's algorithm S for" commas(trials),
|
||||
'trials, and with size='commas(size)":"; say
|
||||
@='trials, and with size='
|
||||
say "Using Knuth's algorithm S for" commas(trials) @ || commas(size)":"
|
||||
say
|
||||
do dig=0 to 9 /* [↓] display the frequency of a dig.*/
|
||||
say left('',15) "frequency of the" dig 'digit is:' commas(#.dig)
|
||||
say left('',20) "frequency of the" dig 'digit is:' commas(#.dig)
|
||||
end /*dig*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
SofN_creator: parse arg item 1 items /*generate ITEM number of items. */
|
||||
do k=1 for item /*traipse through the firs N items. */
|
||||
!.k=random(0, 9) /*set the Kth item with random digit.*/
|
||||
end /*k*/
|
||||
return /*the piddly stuff is done for now. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
SofN: parse arg item; items=items+1 /*get "item", bump the items counter.*/
|
||||
c=random(1, items) /*should we replace a previous item? */
|
||||
if c>size then return /*probability isn't good, so skip it. */
|
||||
_=random(1, size) /*now, figure out which previous ··· */
|
||||
!._=item /* ··· item to replace with ITEM. */
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
commas: procedure; parse arg _; n=_'.9'; #=123456789; b=verify(n,#,"M")
|
||||
e=verify(n,#'0',,verify(n,#"0.",'M'))-4
|
||||
e=verify(n, #'0', , verify(n, #"0.", 'M')) - 4
|
||||
do j=e to b by -3; _=insert(',',_,j); end /*j*/; return _
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
s_of_n: parse arg item; items=items+1 /*get "item", bump the items counter.*/
|
||||
c=random(1, items) /* [↓] should replace a previous item?*/
|
||||
if c>size then return /*probability isn't good, so skip it. */
|
||||
_=random(1, size); !._=item /*now, figure out which previous ··· */
|
||||
return /* ··· item to replace with ITEM.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
s_of_n_creator: parse arg item 1 items /*generate ITEM number of items. */
|
||||
do k=1 for item /*traipse through the first N items. */
|
||||
!.k=random(0, 9) /*set the Kth item with random digit.*/
|
||||
end /*k*/
|
||||
return /*the piddly stuff is done (for now). */
|
||||
|
|
|
|||
|
|
@ -1,29 +1,21 @@
|
|||
#lang racket/base
|
||||
|
||||
(define (s-of-n-creator n)
|
||||
(let ([count 0] ; 'i' in the description
|
||||
[vec (make-vector n)]) ; store the elts we've seen so far
|
||||
(lambda (item)
|
||||
(if (< count n)
|
||||
; we're not full, so, kind of boring
|
||||
(begin
|
||||
(vector-set! vec count item)
|
||||
(set! count (+ count 1)))
|
||||
; we've already seen n elts; fun starts
|
||||
(begin
|
||||
(set! count (+ count 1))
|
||||
(when (< (random count) n)
|
||||
(vector-set! vec (random n) item))))
|
||||
vec)))
|
||||
(define i 0)
|
||||
(define sample (make-vector n)) ; the sample of n items
|
||||
(lambda (item)
|
||||
(set! i (add1 i))
|
||||
(cond [(<= i n) ; we're not full, so kind of boring
|
||||
(vector-set! sample (sub1 i) item)]
|
||||
[(< (random i) n) ; we've already seen n items; swap one?
|
||||
(vector-set! sample (random n) item)])
|
||||
sample))
|
||||
|
||||
(define counts (make-vector 10))
|
||||
(define counts (make-vector 10 0))
|
||||
|
||||
(for ([iter (in-range 0 100000)]) ; trials
|
||||
(let ([s-of-n (s-of-n-creator 3)]) ; set up the chooser
|
||||
(for ([d (in-vector ; iterate over the chosen digits
|
||||
(for/last ([digit (in-range 0 10)]) ; loop through the digits
|
||||
(s-of-n digit)))]) ; feed them in
|
||||
(vector-set! counts d (add1 (vector-ref counts d)))))) ; update counts
|
||||
(for ([i 100000])
|
||||
(define s-of-n (s-of-n-creator 3))
|
||||
(define sample (for/last ([digit 10]) (s-of-n digit)))
|
||||
(for ([d sample]) (vector-set! counts d (add1 (vector-ref counts d)))))
|
||||
|
||||
(for ([d (in-range 0 10)])
|
||||
(printf "~a ~a~n" d (vector-ref counts d)))
|
||||
(for ([d 10]) (printf "~a ~a\n" d (vector-ref counts d)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue