Magic Number
Fun with Coding
Let’s use a calculator to tackle
some programming exercise.
Prime Numbers
Finding primes between 2 to 50.
Ken Iverson’s APL solution on Wikipedia
(2 = +⌿ 0 = (⍳N) ∘.| ⍳N) / ⍳N
Using the fact that for numbers 1 to N, a prime only has 2 factors — itself and 1.
In our case, for numbers 2 to N, a prime has only one factor.
The ‘multiplication table’ APL solution.
(~ m ∊ m ∘.× m) / m ← 1↓⍳50
It’s short and sweet. Definitely not fast.
Sort by Parity
From LeetCode
Video Commentary
Maximum Count of Positive and Negative Integer
From LeetCode
Video: 1 Problem, 24 Languages
Spaces are used to separate items in the list.
Optionally we can use comma or semicolon for the sake of clarity.
Usually ⧻ counts how many elements in a list.
Here ⧻₁ counts how many elements satisfied the condition.
And you can use ⧻₀ to count what’s false.
Alternatively we can use the infix form of the maximum operator:
Rearrange Array to Maximize Prefix Score
Height Checker
From LeetCode
Video commentary
It’s easier to read in 2 lines.
Let’s use ⧻₁ to show our intention better.
We can use R instead of a normal variable.
R stands for result from a previous statement.
So if you have ‘1 + 2 ; R + 6’, you get 9 as R = 1 + 2 = 3
R allows us to break down a complex expression into smaller simpler chunks.
Max Difference Between Increasing Elements
From LeetCode
Video: Functional vs Array Programming
Using R to construct the solution:
Elimination Sort
Multiples of 3 or 5
From Project Euler
Video commentary
Introducing ·❘·
While ÷ means division, ·❘· means divisible.
So 6 ·❘· 3 = 1 (true) and 6 ·❘· 4 = 0 (false).
Similar expression but factoring out 3 and 5.
Moment of truth — let’s find all the multiples under 1000:
Sum of Squares
Difference Between Ones and Zeros
From LeetCode
Video commentary
Left and Right Sum Differences
From LeetCode
Video commentary
Making use of the weakness that nested list is ignored, that is
[ [ 1 2 3 ] ] evaluates to [ 1 2 3 ] …
Let’s tidy up the parentheses:
‘What sums up 2020?’
Check if Matrix is X-Matrix
From LeetCode
Video commentary