diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-10-25 17:54:48 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-10-25 17:54:48 +0000 |
| commit | 6bcc61fd124f3f54cc1c0c55881b48f0a0aa05e6 (patch) | |
| tree | 6b892348fb96fc5977ac936fba9aa6fa3388b618 | |
| parent | 6fbad6968d13078528cd62df97ac8b099ccc8e06 (diff) | |
| download | perlweeklychallenge-club-6bcc61fd124f3f54cc1c0c55881b48f0a0aa05e6.tar.gz perlweeklychallenge-club-6bcc61fd124f3f54cc1c0c55881b48f0a0aa05e6.tar.bz2 perlweeklychallenge-club-6bcc61fd124f3f54cc1c0c55881b48f0a0aa05e6.zip | |
- Added APL solutions by Richard Park.
| -rw-r--r-- | challenge-083/richard-park/apl/FlipArray.aplf | 8 | ||||
| -rw-r--r-- | challenge-083/richard-park/apl/WordsLength.aplf | 9 | ||||
| -rw-r--r-- | challenge-083/richard-park/apl/ch-1.aplf | 9 | ||||
| -rw-r--r-- | challenge-083/richard-park/apl/ch-2.aplf | 8 |
4 files changed, 34 insertions, 0 deletions
diff --git a/challenge-083/richard-park/apl/FlipArray.aplf b/challenge-083/richard-park/apl/FlipArray.aplf new file mode 100644 index 0000000000..9aebc9b1e7 --- /dev/null +++ b/challenge-083/richard-park/apl/FlipArray.aplf @@ -0,0 +1,8 @@ + FlipArray←{ + ⍝ Number of elements in ⍵ to flip sign, resulting in minimum non-negative sum + flips←⍉¯1+2×2⊥⍣¯1⊢¯1+⍳2*≢⍵ ⍝ All possible sign flips + sums←+/⍵×⍤1⊢flips ⍝ Sums of all flips + pos←0≤sums ⍝ Positive number mask + minflip←(pos⌿flips)⌷⍨(⊢⍳⌊/)pos⌿sums ⍝ Flip resulting in min non-neg sum + +/¯1=minflip ⍝ How many elements to flip? + } diff --git a/challenge-083/richard-park/apl/WordsLength.aplf b/challenge-083/richard-park/apl/WordsLength.aplf new file mode 100644 index 0000000000..ed2aa3794a --- /dev/null +++ b/challenge-083/richard-park/apl/WordsLength.aplf @@ -0,0 +1,9 @@ + WordsLength←{ +⍝ Short and sweet + WL←{≢∊1↓¯1↓'., '((~∊⍨)⊆⊢)⍵} +⍝ More performant on long input strings + WL2←',. '∘{≢⍺~⍨⍵{(-⊃⌽⍵)↓(⊃⍵)↓⍺}⊃⍤⍸⍤1⊢(⊢,[0.5]⌽)⍺∊⍨⍵} +⍝ Regex + WL3←{≢⍤('^\w+' '\w+$' '\W'⎕R'')⍵} + WL2 ⍵ + } diff --git a/challenge-083/richard-park/apl/ch-1.aplf b/challenge-083/richard-park/apl/ch-1.aplf new file mode 100644 index 0000000000..ed2aa3794a --- /dev/null +++ b/challenge-083/richard-park/apl/ch-1.aplf @@ -0,0 +1,9 @@ + WordsLength←{ +⍝ Short and sweet + WL←{≢∊1↓¯1↓'., '((~∊⍨)⊆⊢)⍵} +⍝ More performant on long input strings + WL2←',. '∘{≢⍺~⍨⍵{(-⊃⌽⍵)↓(⊃⍵)↓⍺}⊃⍤⍸⍤1⊢(⊢,[0.5]⌽)⍺∊⍨⍵} +⍝ Regex + WL3←{≢⍤('^\w+' '\w+$' '\W'⎕R'')⍵} + WL2 ⍵ + } diff --git a/challenge-083/richard-park/apl/ch-2.aplf b/challenge-083/richard-park/apl/ch-2.aplf new file mode 100644 index 0000000000..9aebc9b1e7 --- /dev/null +++ b/challenge-083/richard-park/apl/ch-2.aplf @@ -0,0 +1,8 @@ + FlipArray←{ + ⍝ Number of elements in ⍵ to flip sign, resulting in minimum non-negative sum + flips←⍉¯1+2×2⊥⍣¯1⊢¯1+⍳2*≢⍵ ⍝ All possible sign flips + sums←+/⍵×⍤1⊢flips ⍝ Sums of all flips + pos←0≤sums ⍝ Positive number mask + minflip←(pos⌿flips)⌷⍨(⊢⍳⌊/)pos⌿sums ⍝ Flip resulting in min non-neg sum + +/¯1=minflip ⍝ How many elements to flip? + } |
