diff options
| -rw-r--r-- | challenge-055/richard-park/apl/FlipBinary.aplf | 12 | ||||
| -rw-r--r-- | challenge-055/richard-park/apl/Pmat.aplf | 6 | ||||
| -rw-r--r-- | challenge-055/richard-park/apl/WaveArray.aplf | 13 | ||||
| -rw-r--r-- | challenge-055/richard-park/apl/ch-1.apl | 12 | ||||
| -rw-r--r-- | challenge-055/richard-park/apl/ch-2.apl | 13 |
5 files changed, 56 insertions, 0 deletions
diff --git a/challenge-055/richard-park/apl/FlipBinary.aplf b/challenge-055/richard-park/apl/FlipBinary.aplf new file mode 100644 index 0000000000..248a6f09dc --- /dev/null +++ b/challenge-055/richard-park/apl/FlipBinary.aplf @@ -0,0 +1,12 @@ + FlipBinary←{ +⍝ ⍵: Binary number b of length n + n←≢b←⍵ +⍝ Flip 2 indices L≤R<N and count the number of 1s +⍝ ←: L R pairs which result in maximum 1s + To←{⍺+⍳1+⍵-⍺} ⍝ Integers ⍺ to ⍵ inclusive + flip←{⍵/⍨≤/¨⍵},⍳n n ⍝ Possible flip indices L R + flerp←(⊃To/)¨flip ⍝ Interpolate integers from L to R + f←⍵∘{~@⍵⊢⍺}¨flerp ⍝ b flipped at each set of flip indices + max←⌈/sum←+/¨f ⍝ Maximum number of 1s + flip[⍸sum=max] ⍝ Flip indices which result in maximum 1s + } diff --git a/challenge-055/richard-park/apl/Pmat.aplf b/challenge-055/richard-park/apl/Pmat.aplf new file mode 100644 index 0000000000..12be0666b9 --- /dev/null +++ b/challenge-055/richard-park/apl/Pmat.aplf @@ -0,0 +1,6 @@ + Pmat←{⎕ML←1 ⍝ dfns pmat: Permutation matrix of ⍳⍵. + { ⍝ perms of ⍳⍵: + 1≥⍴⍵:↑,↓⍵ ⍝ short vector: done. + ↑⍪/⍵,∘∇¨⍵∘~¨⍵ ⍝ items prefix sub-perms of remainder. + }⍳⍵ ⍝ permutations of identity perm. + } diff --git a/challenge-055/richard-park/apl/WaveArray.aplf b/challenge-055/richard-park/apl/WaveArray.aplf new file mode 100644 index 0000000000..06fdfa0dd9 --- /dev/null +++ b/challenge-055/richard-park/apl/WaveArray.aplf @@ -0,0 +1,13 @@ + WaveArray←{ +⍝ Thanks to Total Array Ordering, an array ⍵ can be reordered so that its major cells, m, have the property +⍝ (1⌷⍵)≥(2⌷⍵)≤(3⌷⍵)≥(4⌷⍵)≤... +⍝ ⍵: Array +⍝ ←: Array of rank (1+≢⍴⍵) where each major cell is a wave permutation of ⍵ + n←≢⍵ + p←⍵⌷⍤0 99⍨Pmat n ⍝ Permutations of ⍵ + gte←(¯1+n)⍴1 0 ⍝ Wave-array greater-than or equal pattern + lte←~gte ⍝ Wave-array less-than or equal pattern + g←gte≡⍤1⊢2≥/(⍋⍋)⍤¯1⊢p ⍝ Permutations with wave gte pattern + l←lte≡⍤1⊢2≤/(⍋⍋)⍤¯1⊢p ⍝ Permutations with wave lte pattern + ∪(g∧l)⌿p ⍝ Unique wave permutations of ⍵ + } diff --git a/challenge-055/richard-park/apl/ch-1.apl b/challenge-055/richard-park/apl/ch-1.apl new file mode 100644 index 0000000000..248a6f09dc --- /dev/null +++ b/challenge-055/richard-park/apl/ch-1.apl @@ -0,0 +1,12 @@ + FlipBinary←{ +⍝ ⍵: Binary number b of length n + n←≢b←⍵ +⍝ Flip 2 indices L≤R<N and count the number of 1s +⍝ ←: L R pairs which result in maximum 1s + To←{⍺+⍳1+⍵-⍺} ⍝ Integers ⍺ to ⍵ inclusive + flip←{⍵/⍨≤/¨⍵},⍳n n ⍝ Possible flip indices L R + flerp←(⊃To/)¨flip ⍝ Interpolate integers from L to R + f←⍵∘{~@⍵⊢⍺}¨flerp ⍝ b flipped at each set of flip indices + max←⌈/sum←+/¨f ⍝ Maximum number of 1s + flip[⍸sum=max] ⍝ Flip indices which result in maximum 1s + } diff --git a/challenge-055/richard-park/apl/ch-2.apl b/challenge-055/richard-park/apl/ch-2.apl new file mode 100644 index 0000000000..06fdfa0dd9 --- /dev/null +++ b/challenge-055/richard-park/apl/ch-2.apl @@ -0,0 +1,13 @@ + WaveArray←{ +⍝ Thanks to Total Array Ordering, an array ⍵ can be reordered so that its major cells, m, have the property +⍝ (1⌷⍵)≥(2⌷⍵)≤(3⌷⍵)≥(4⌷⍵)≤... +⍝ ⍵: Array +⍝ ←: Array of rank (1+≢⍴⍵) where each major cell is a wave permutation of ⍵ + n←≢⍵ + p←⍵⌷⍤0 99⍨Pmat n ⍝ Permutations of ⍵ + gte←(¯1+n)⍴1 0 ⍝ Wave-array greater-than or equal pattern + lte←~gte ⍝ Wave-array less-than or equal pattern + g←gte≡⍤1⊢2≥/(⍋⍋)⍤¯1⊢p ⍝ Permutations with wave gte pattern + l←lte≡⍤1⊢2≤/(⍋⍋)⍤¯1⊢p ⍝ Permutations with wave lte pattern + ∪(g∧l)⌿p ⍝ Unique wave permutations of ⍵ + } |
