diff options
| -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? + } |
