Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
63
Task/Knuth-shuffle/DuckDB/knuth-shuffle.duckdb
Normal file
63
Task/Knuth-shuffle/DuckDB/knuth-shuffle.duckdb
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
### Generic utilities
|
||||
|
||||
create or replace function printstring(s, width) as (
|
||||
regexp_replace(s, '(.{1,' || width || '})', '\1' || chr(10), 'g' )
|
||||
);
|
||||
|
||||
# It is assumed that 1 <= i, j <= length(ary)
|
||||
create or replace function swap(ary, i, j) as (
|
||||
case when i = j then ary
|
||||
when i < j then ary[1:i -1] || [ary[j]] || ary[i+1:j-1] || [ary[i]] || ary[j+1:]
|
||||
else ary[1:j -1] || [ary[i]] || ary[j+1:i-1] || [ary[j]] || ary[i+1:]
|
||||
end
|
||||
);
|
||||
|
||||
# Generate a pseudo-random integer in range(0,n), i.e. excluding n
|
||||
create or replace function rnd(n) as (
|
||||
SELECT floor(random() * n)::BIGINT AS rnd
|
||||
);
|
||||
|
||||
### A deck of cards as codepoints
|
||||
create or replace function deck() as (
|
||||
range(127137, 127148) || range(127149, 127151) -- Spades
|
||||
|| range(127153, 127164) || range(127165, 127167) -- Hearts
|
||||
|| range(127169, 127180) || range(127181, 127183) -- Diamonds
|
||||
|| range(127185, 127196) || range(127197, 127199) -- Clubs
|
||||
);
|
||||
|
||||
create or replace function printdeck(deck) as (
|
||||
list_transform(deck, x -> chr(x::INTEGER)).array_to_string('')
|
||||
);
|
||||
|
||||
### Knuth Shuffle of any list
|
||||
create or replace function knuth_shuffle_table(arr) as table (
|
||||
WITH RECURSIVE cte(a, idx, r) AS (
|
||||
-- Base case: the original array, the index of the last element, and a random index
|
||||
SELECT arr as a, length(arr) AS idx, 1 + rnd(length(arr)) as r
|
||||
UNION ALL
|
||||
-- Recursive case: shuffle the array by swapping the current index with a random element
|
||||
SELECT
|
||||
swap(a, r, idx) AS a,
|
||||
idx - 1 AS idx,
|
||||
1 + rnd(idx - 1) as r
|
||||
FROM cte
|
||||
WHERE idx > 1
|
||||
)
|
||||
SELECT a
|
||||
FROM cte
|
||||
WHERE idx = 1
|
||||
LIMIT 1
|
||||
);
|
||||
|
||||
# A scalar version of knuth_shuffle_table() specialized for INTEGER[]
|
||||
# Currently DuckDB requires the specialization.
|
||||
create or replace function knuth_shuffle(arr) as (
|
||||
from knuth_shuffle_table(arr::INTEGER[]) _(a)
|
||||
);
|
||||
|
||||
### Examples
|
||||
select a, knuth_shuffle(a)
|
||||
from values ([]), ([10,20]), ([0,10,20,30,40]) as t(a);
|
||||
|
||||
.mode list
|
||||
select knuth_shuffle(deck()).printdeck().printstring(13);
|
||||
4
Task/Knuth-shuffle/REBOL/knuth-shuffle-1.rebol
Normal file
4
Task/Knuth-shuffle/REBOL/knuth-shuffle-1.rebol
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
probe random []
|
||||
probe random [10]
|
||||
probe random [10 20]
|
||||
probe random [10 20 30]
|
||||
48
Task/Knuth-shuffle/REBOL/knuth-shuffle-2.rebol
Normal file
48
Task/Knuth-shuffle/REBOL/knuth-shuffle-2.rebol
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Knuth shuffle"
|
||||
file: %Knuth_shuffle.r3
|
||||
url: https://rosettacode.org/wiki/Knuth_shuffle
|
||||
]
|
||||
fisher-yates: func [
|
||||
"Fisher-Yates shuffle algorithm - randomly shuffles elements in a block"
|
||||
;Uses the modern variant that works backwards through the array
|
||||
data [series!] "Input block to shuffle"
|
||||
/local i j tmp ;; Local variables: i=current index, j=random index, tmp=swap variable
|
||||
][
|
||||
;; Make a copy of the input block and get its length
|
||||
;; This prevents modification of the original block
|
||||
i: length? data: copy data
|
||||
|
||||
;; Loop from the last element down to the second element
|
||||
;; We stop at 1 because there's no need to swap the first element with itself
|
||||
while [i > 1] [
|
||||
;; Generate random index j from 1 to i (inclusive)
|
||||
j: random i
|
||||
;; Previous version tried to avoid self-swap as an optimization
|
||||
;; but actually it is faster to avoid it!
|
||||
|
||||
;; Three-step swap using path notation for direct block access
|
||||
;; Step 1: Store element at random position j in temporary variable
|
||||
tmp: data/:j
|
||||
|
||||
;; Step 2: Move current element (at position i) to random position j
|
||||
data/:j: data/:i
|
||||
|
||||
;; Step 3: Move stored element to current position i
|
||||
;; Using :tmp to get the value (word evaluation)
|
||||
data/:i: :tmp
|
||||
|
||||
;; Move to previous element (working backwards through the block)
|
||||
i: i - 1
|
||||
]
|
||||
|
||||
;; Return the shuffled block
|
||||
data
|
||||
]
|
||||
|
||||
random/seed 0 ;; to get consistent results
|
||||
|
||||
probe fisher-yates []
|
||||
probe fisher-yates [10]
|
||||
probe fisher-yates [10 20]
|
||||
probe fisher-yates [10 20 30]
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
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
|
||||
]
|
||||
8
Task/Knuth-shuffle/YAMLScript/knuth-shuffle.ys
Normal file
8
Task/Knuth-shuffle/YAMLScript/knuth-shuffle.ys
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
!YS-v0
|
||||
|
||||
defn main(*list):
|
||||
say:
|
||||
reduce _ list:V range(list.#.-- 1 -1):
|
||||
fn(v i):
|
||||
r =: i:rand-int
|
||||
assoc v: i (v r) r (v i)
|
||||
Loading…
Add table
Add a link
Reference in a new issue