-
-
Save ikaruga777/72e69b0692aa399bce07e80c245e4380 to your computer and use it in GitHub Desktop.
shuffle実装した
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rand_once(seed): (48271 * seed) % 2147483647; | |
def rand(seed): rand_once(rand_once(rand_once(seed))); | |
def swap(s): . as $a | |
| if s[0] == s[1] then | |
. | |
else | |
if s[0] > s[1] then [s[1], s[0]] else s end | |
| .[0] as $x | .[1] as $y | |
| $a | [ .[:$x], .[$y], .[$x+1:$y], .[$x], .[$y+1:] ] | flatten | |
end | |
; | |
def shuffle(seed): . as $a | |
| $a | length as $length | |
| [range(0; $length-1)] | reduce .[] as $i ( $a; . as $x | (rand(seed+$i) % $length) as $r | $x | swap([$r,$i] )) | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "./shuffle" as S; | |
["A", "B", "C", "D", "E", "F", "G"] | S::shuffle(now) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment