diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-10-16 22:26:14 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-16 22:26:14 -0400 |
| commit | 116fbde94ec7d7f40dcb01150d80f01fc6675b43 (patch) | |
| tree | e3e1670876123250a33b975bedafaa49d1501cee | |
| parent | 3173e8358c54e97e48c369903217a99e8dd9b7ac (diff) | |
| parent | c1193c5152d2b04590a83b31db41f66ebf51db7b (diff) | |
Merge pull request #7 from frasiyav/history-of-APL-50-examples
added History of APL in 50 Functions examples 19-23
| -rw-r--r-- | examples/fifty.bqn | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/fifty.bqn b/examples/fifty.bqn index cc452ce4..34222304 100644 --- a/examples/fifty.bqn +++ b/examples/fifty.bqn @@ -93,6 +93,47 @@ SeventeenTwentyNine ← {F: } # ! 1729 ≡ SeventeenTwentyNine⟨⟩ # slow +# 19 +Permutations ← {𝕊0:1‿0⥊0; ∾<˘(0∾˘1+𝕊𝕩-1)⊏⎉2‿1⍒˘=⌜˜↕𝕩} +! (6‿3⥊0‿1‿2‿0‿2‿1‿1‿0‿2‿1‿2‿0‿2‿0‿1‿2‿1‿0)≡Permutations 3 + +# 20 +InversePermutation ← ⍋ #Alternatively: ⊐⟜(↕≠) +! (↕10)≡InversePermutation⊸⊏1‿4‿5‿2‿6‿8‿3‿7‿0‿9 + +# 21 +IndexFromPermutation‿PermutationFromIndex ← { + Rfd ← ⊢+´∘>¨¯1↓↓ + Dfr ← (⍋∘⍋∾)´ + Ifp ← {⌽×`1+»↕≠𝕩} +´∘× Rfd + Pfi ← Dfr (1+↕)⊸(⌽⊣|¯1↓·⌊∘÷`∾˜) + Ifp‿Pfi +} +! { + p ← 1‿3‿0‿7‿6‿5‿4‿9‿8‿2 + p ≡ 10 PermutationFromIndex IndexFromPermutation p +} + +# 22 +Combinations ← {𝕨(=∨0=⊣)◶⟨(0∾˘𝕊⌾(-⟜1))∾1+𝕊⟜(-⟜1), ≍∘↕⊣⟩𝕩} +! (3 Combinations 5) ≡ 10‿3⥊0‿1‿2‿0‿1‿3‿0‿1‿4‿0‿2‿3‿0‿2‿4‿0‿3‿4‿1‿2‿3‿1‿2‿4‿1‿3‿4‿2‿3‿4 + +# 23 +IndexFromCombination‿CombinationFromIndex ← { + C ← ((-˜+↕∘⊣)÷○(×´1⊸+)↕∘⊣)˘ + Ic ← {m‿n←𝕨 ⋄ ⊑-˝(m-↕m) +˝∘(C˘) n-(»1+𝕩)∾˘𝕩} + Ci ← { + 0‿i𝕊𝕩: ⟨⟩; + m‿n←𝕨 + v←⥊+`(m-1)C(1-m)↓⌽↕n + k←(v>𝕩)⊐1 + k∾(1+k)+(𝕨-1∾1+k)𝕊(𝕩-k⊏0∾v) + } + Ic‿Ci +} +! 11 ≡ 4‿6 IndexFromCombination 1‿2‿3‿5 +! 1‿2‿3‿5 ≡ 4‿6 CombinationFromIndex 11 + # 24 SymmetricArray ← ⍉⊸≡ ∧ 1⊸⍉⊸≡ ! SymmetricArray +⌜´ 3⥊<2‿3‿7‿11 |
