Magic Number
Calculator as a Tool of Thought
Array magic for everyone.
Algorithms for All Equal
Solution #1
⋀/⊃=⊢
Solution #2
(1=≢)∪
Solution #3
⋀/2=/⊢
Solution #4
⌊/=⌈/
Using the prefix variants of Maximum and Minimum:
Using MinMax function:
Solution #5
Convenience is essential for a calculator. While the above solutions are simple, they are not obvious for casual users.
So there is dedicated ‘All Equal’ function.
It is the prefix form of ‘=’.
By default, this form is opted in to avoid confusion for the beginners.
Why the glyph ‘=̂’
For three numbers a, b, and c to be equal, they have to satisfy the condition:
a = b and b = c and a = c
Writing using the logical ‘and’ symbol:
a = b ∧ b = c ∧ a = c
So the glyph is a reflection of the condition.
Any Equal
If any pair of a, b, and c are equal, they satisfy:
a = b or b = c or a = c
Using the logical ‘or’ symbol:
a = b ∨ b = c ∨ a = c
So the glyph for ‘Any Equal’ is =̌
Example
Duplicate Zeros
From Weekly Challenge 333 – Task 2
Video Commentary
Leap Year
From Exercism
Video Commentary
First Missing Positive
From LeetCode
Video Commentary
The Count function (⧻) gives you the number of elements in a list.
The Remove function (~) removes a particular element from a list.
Later you will find it useful to think of Remove as ‘Without’.
Putting it altogether:
Count Negative Numbers in a Matrix
From LeetCode
Video Commentary
The Count function (⧻) gives you the number of elements in a matrix.
What’s more, it can count the occurrence of a particular number:
here 4 only appears once in the matrix.
To find the negative numbers, we start by setting the condition
The 1s in the above conditional matrix are the locations of the negative numbers.
All we need to do is to count the 1s.
So there are 4 negative numbers.
Sign of the Product of an Array
From LeetCode
Video Commentary
Minimum Average of Smallest and Largest
From LeetCode
Video Commentaries: Original, Follow Up
We are going for a simple solution — choosing elegance over efficiency.
•⃒ is the median function. The median of two numbers is same as the average.
Richest Customer Wealth
From LeetCode
Video Commentary
For illustration, we will use a simpler example than the one in LeetCode.
First we will need to sum up the numbers for each row.
For the first row, it will be 1 + 2 + 3, and 5 + 5 + 5 for the second row, etc.
In other words we need to inject (▷) addition (+) between each number.
By default, inject works horizontally along the x-axis, also known as axis 1. You can specify axis 1 for inject, it will appear as a subscript. You get the same result.
As an aside, to get the sum for each column, specify axis 2 for inject.
To find the richest customer, we use the maximum operator |̊
However, it has no effect.
Like inject, maximum also works horizontally. To find the maximum column wise, we specify 2 for the axis:
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:
Optimum Solution
Maximize Prefix Score
LeetCode
Video: Why I Love BQN So Much! (vs Python)
As a multi-line solution:
Alternative without variable assignment:
R⃣ can be used to break up longer statements into shorter ones.
While R⃣ makes complex expressions easier to write, it sometimes makes the expression harder to understand. To understand line 3, you have know line 2. And R⃣ from line 2 has a difference meaning to R⃣ from line 3. So use sparingly.
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 a different style of bracket for legibility:
Elimination Sort
From APL Quest
Video commentary
Multiples of 3 or 5
From Project Euler
Video commentary
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
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
We can make the solution easier to read with fewer nested parentheses.
The above makes use of the absolute value of the difference in the form of:
When use in the infix form, it becomes the absolute difference:
Now we can remove the outer parentheses:
We can go one step further us of the fact that nested list is ignored, that is
[ [ 1 2 3 ] ] evaluates to [ 1 2 3 ] …
Let’s use square brackets for clarity:
‘What sums up 2020?’
From Advent of Code
Video commentary
The result of line 1, 𝗥, is the coordinate of 2020, expressed as a 2-dimensional complex number. The column is the real part ℜ while row is the imaginary part ℑ.
Splitting coordinates into separate ‘normal’ numbers can be done with the handy function Complex Number to List — ‘ℂ › 𝕃’:
Check if Matrix is X-Matrix
From LeetCode
Video commentary
Minimum Value of Merged Integers
From LeetCode
Video commentary
Let’s store the list from example 1 in the variable l:
To make merging easier, we will reshape l into a matrix with 2 columns. Notice we use ‘?’ as the 2nd argument for row, which in effect asking the computer to calculate the number of rows needed to accommodate all values of l.
We will store the first column of m into a:
And the last column into b:
How do we merge numbers?
If we take the 1st row of a and b, the numbers are 23 and 55, and we want them combined into 2355.
Mathematically this is 23 × 100 + 55.
On the other hand if we want to merge 23 and 5 to become 235, this would be:
23 × 10 + 5
Notice we use 10 for 5 (which has 1 digit) and 100 for 55 (which has 2 digits)
We can write the merging of 23 and 55 as:
23 × 102 + 55
Or in caret notation for legibility:
23 × 10^2 + 55
The highlighted 2 is the number digits of 55.
This leads to the formula for merging:
A × 10^Digits of B + B
We can use logarithm to calculate the number of digits for a number but it is easier to use the Bit Count function.
By default Bit Count calculates the number of binary (base 2) digits of a value. But we can specify base 10 for decimal:
Let’s calculate the digit count for column b and store it in variable d:
Time to merge the digits of columns a and b:
The Minimum function returns the minimum value of a list.
By default, it returns the minimum of a horizontal list. That is, it acts along the x-axis or axis 1.
For the above example, we can be specific by indicating the axis as subscript:
Earlier when merging a and b, we get a vertical list. So to find the minimum, we need to use 2 for the axis:
Recap:
We used variables to breakdown the problem. We learned about Bit Count and Minimum functions with their optional subscript arguments.