diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-26 17:33:37 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-26 17:33:37 +0100 |
| commit | 719555854d38b51a6cf9ea6cc7e0015283fc4f71 (patch) | |
| tree | 998191e4180c48b513b0586bd088ca82b575a9d9 | |
| parent | 4c1a8dbf9f93de0d5ed62137290905ae25c60f28 (diff) | |
| download | perlweeklychallenge-club-719555854d38b51a6cf9ea6cc7e0015283fc4f71.tar.gz perlweeklychallenge-club-719555854d38b51a6cf9ea6cc7e0015283fc4f71.tar.bz2 perlweeklychallenge-club-719555854d38b51a6cf9ea6cc7e0015283fc4f71.zip | |
- Added APL solutions by Richard Park.
| -rw-r--r-- | challenge-057/richard-park/apl/InvertTree.aplf | 44 | ||||
| -rw-r--r-- | challenge-057/richard-park/apl/ShortestUniquePrefix.aplf | 21 | ||||
| -rw-r--r-- | challenge-057/richard-park/apl/TreeMatrix.aplf | 9 | ||||
| -rw-r--r-- | challenge-057/richard-park/apl/ch-1.aplf | 44 | ||||
| -rw-r--r-- | challenge-057/richard-park/apl/ch-2.aplf | 21 |
5 files changed, 139 insertions, 0 deletions
diff --git a/challenge-057/richard-park/apl/InvertTree.aplf b/challenge-057/richard-park/apl/InvertTree.aplf new file mode 100644 index 0000000000..fb4455fd53 --- /dev/null +++ b/challenge-057/richard-park/apl/InvertTree.aplf @@ -0,0 +1,44 @@ + InvertTree←{ + +⍝ ⍵: Full binary tree in the format +⍝ Node number (ID), value, parent +⍝ +⍝ 1 +⍝ / \ +⍝ 2 3 +⍝ / \ / \ +⍝ 4 5 6 7 +⍝ +⍝ For example, the above tree in depth-first pre-order traversal +⍝ n ← 0 1 2 3 4 5 6 +⍝ v ← 1 2 4 5 3 6 7 +⍝ p ← 0 0 1 1 0 4 4 + (n v p)←⍵ + + TreeMatrix←{ + ⍝ Binary matrix for a full binary tree + ⍝ Each row is a binary mask for elements on that row + ⍝ + ⍝ For the example tree: + ⍝ v⌿⍤1⍨ TreeMatrix p + ⍝ 1 0 0 0 + ⍝ 2 3 0 0 + ⍝ 4 5 6 7 + ⍝ + ⎕IO←0 + ⍺←0 + r←⍵(∨⌿∘.=)⍨,⍺ + c←⍸r + 0≡⍺:(↑1(0@0⊢r))⍪(1↓c)∇ ⍵ + ⍬≡c:⍬⍴⍨0,⍴⍵ + r⍪(c ∇ ⍵) + } + InvertValues←{ + ⍝ Horizontally flip values at each level of the tree + ⍵≡⍬⍴⍨0,⊃⌽⍴⍵:⍺ ⍝ No more rows: Return inverted values + (⌽@(⍸1⌷⍵)⊢⍺)∇ 1↓⍵ ⍝ Horizontal flip ∇ Recurse remaining levels / rows + } + iv←v InvertValues TreeMatrix p + (n iv p) ⍝ Return inverted tree + + } diff --git a/challenge-057/richard-park/apl/ShortestUniquePrefix.aplf b/challenge-057/richard-park/apl/ShortestUniquePrefix.aplf new file mode 100644 index 0000000000..e4d2f0c984 --- /dev/null +++ b/challenge-057/richard-park/apl/ShortestUniquePrefix.aplf @@ -0,0 +1,21 @@ + ShortestUniquePrefix←{ +⍝ ⍵: Nested list of words +⍝ ←: Shortest unique prefixes, not in order + I←((⊃⊣)⌷⊢)⍤0 99 ⍝ Select indexer + ⍺←1 ⍝ Prefix length + p←⍺∘↑¨⍵ ⍝ Prefixes + (u c i)←↓⍉{(⍺)(≢⍵)(⍵)}⌸p ⍝ (Unique Count Indices) + um←c=1 ⍝ Unique prefix mask + 0=⍴⍵:⍬ ⍝ All SUPs found: Return empty array + 0=∨/um:(⍺+1)∇ ⍵ ⍝ No unique prefixes: + ⍝ increment prefix length + ⍝ ∇ recurse + cwi←i⌿⍨um ⍝ Completed word indices + cw←⍵ I⍨cwi ⍝ Completed words + up←u⌿⍨um ⍝ Unique prefixes + ⍝ Unique prefixes found: + ⍝ Return unique prefixes, + ⍝ increment prefix length + ⍝ ∇ recurse with remaining words + ∨⌿um:(⊃¨up),(⍺+1)∇ ⍵~cw + } diff --git a/challenge-057/richard-park/apl/TreeMatrix.aplf b/challenge-057/richard-park/apl/TreeMatrix.aplf new file mode 100644 index 0000000000..4b6ec805a5 --- /dev/null +++ b/challenge-057/richard-park/apl/TreeMatrix.aplf @@ -0,0 +1,9 @@ + TreeMatrix←{ + ⎕IO←0 + ⍺←0 + r←⍵(∨⌿∘.=)⍨,⍺ + c←⍸r + 0≡⍺:(↑1(0@0⊢r))⍪(1↓c)∇ ⍵ + ⍬≡c:⍬⍴⍨0,⍴⍵ + r⍪(c ∇ ⍵) + } diff --git a/challenge-057/richard-park/apl/ch-1.aplf b/challenge-057/richard-park/apl/ch-1.aplf new file mode 100644 index 0000000000..fb4455fd53 --- /dev/null +++ b/challenge-057/richard-park/apl/ch-1.aplf @@ -0,0 +1,44 @@ + InvertTree←{ + +⍝ ⍵: Full binary tree in the format +⍝ Node number (ID), value, parent +⍝ +⍝ 1 +⍝ / \ +⍝ 2 3 +⍝ / \ / \ +⍝ 4 5 6 7 +⍝ +⍝ For example, the above tree in depth-first pre-order traversal +⍝ n ← 0 1 2 3 4 5 6 +⍝ v ← 1 2 4 5 3 6 7 +⍝ p ← 0 0 1 1 0 4 4 + (n v p)←⍵ + + TreeMatrix←{ + ⍝ Binary matrix for a full binary tree + ⍝ Each row is a binary mask for elements on that row + ⍝ + ⍝ For the example tree: + ⍝ v⌿⍤1⍨ TreeMatrix p + ⍝ 1 0 0 0 + ⍝ 2 3 0 0 + ⍝ 4 5 6 7 + ⍝ + ⎕IO←0 + ⍺←0 + r←⍵(∨⌿∘.=)⍨,⍺ + c←⍸r + 0≡⍺:(↑1(0@0⊢r))⍪(1↓c)∇ ⍵ + ⍬≡c:⍬⍴⍨0,⍴⍵ + r⍪(c ∇ ⍵) + } + InvertValues←{ + ⍝ Horizontally flip values at each level of the tree + ⍵≡⍬⍴⍨0,⊃⌽⍴⍵:⍺ ⍝ No more rows: Return inverted values + (⌽@(⍸1⌷⍵)⊢⍺)∇ 1↓⍵ ⍝ Horizontal flip ∇ Recurse remaining levels / rows + } + iv←v InvertValues TreeMatrix p + (n iv p) ⍝ Return inverted tree + + } diff --git a/challenge-057/richard-park/apl/ch-2.aplf b/challenge-057/richard-park/apl/ch-2.aplf new file mode 100644 index 0000000000..e4d2f0c984 --- /dev/null +++ b/challenge-057/richard-park/apl/ch-2.aplf @@ -0,0 +1,21 @@ + ShortestUniquePrefix←{ +⍝ ⍵: Nested list of words +⍝ ←: Shortest unique prefixes, not in order + I←((⊃⊣)⌷⊢)⍤0 99 ⍝ Select indexer + ⍺←1 ⍝ Prefix length + p←⍺∘↑¨⍵ ⍝ Prefixes + (u c i)←↓⍉{(⍺)(≢⍵)(⍵)}⌸p ⍝ (Unique Count Indices) + um←c=1 ⍝ Unique prefix mask + 0=⍴⍵:⍬ ⍝ All SUPs found: Return empty array + 0=∨/um:(⍺+1)∇ ⍵ ⍝ No unique prefixes: + ⍝ increment prefix length + ⍝ ∇ recurse + cwi←i⌿⍨um ⍝ Completed word indices + cw←⍵ I⍨cwi ⍝ Completed words + up←u⌿⍨um ⍝ Unique prefixes + ⍝ Unique prefixes found: + ⍝ Return unique prefixes, + ⍝ increment prefix length + ⍝ ∇ recurse with remaining words + ∨⌿um:(⊃¨up),(⍺+1)∇ ⍵~cw + } |
