aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-04-11 15:28:31 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-04-11 15:28:31 +0100
commit607dc165c04bc2dbe9fbd12ec3d18d9fe5f994b6 (patch)
treea6b9506c27bcb7102997a7d1f70d080c8819f2f5
parent65d163bc9208ec056bf376d0b4802b4b18fad599 (diff)
downloadperlweeklychallenge-club-607dc165c04bc2dbe9fbd12ec3d18d9fe5f994b6.tar.gz
perlweeklychallenge-club-607dc165c04bc2dbe9fbd12ec3d18d9fe5f994b6.tar.bz2
perlweeklychallenge-club-607dc165c04bc2dbe9fbd12ec3d18d9fe5f994b6.zip
- Added APL solutions by Richard Park.
-rw-r--r--challenge-055/richard-park/apl/FlipBinary.aplf12
-rw-r--r--challenge-055/richard-park/apl/Pmat.aplf6
-rw-r--r--challenge-055/richard-park/apl/WaveArray.aplf13
-rw-r--r--challenge-055/richard-park/apl/ch-1.apl12
-rw-r--r--challenge-055/richard-park/apl/ch-2.apl13
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 ⍵
+ }