Magic Number

Icon

Fun with Coding

Let’s use a calculator to tackle
some programming exercise.

Count Negative Numbers in a Matrix

From LeetCode
Video Commentary

Count Negative Number In Matrix Assign

The Count function (⧻) gives you the number of elements in a matrix.

Count Negative Number In Matrix Count

What’s more, it can count the occurrence of a particular number:
here 4 only appears once in the matrix.

Count Negative Number In Matrix Count 4

To find the negative numbers, we start by setting the condition

Count Negative Number In Matrix 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.

Count Negative Number In Matrix Count True

So there are 4 negative numbers.

Sign of the Product of an Array

From LeetCode
Video Commentary

Sign Product Assign
Sign Product 1
Sign Product 2

Richest Customer Wealth

From LeetCode
Video Commentary

For illustration, we will use a simpler example than the one in LeetCode.

Richest Customer Assign

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.

Richest Customer Sum

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.

Richest Customer Sum 1

As an aside, to get the sum for each column, specify axis 2 for inject.

Richest Customer Sum 2

To find the richest customer, we use the maximum operator |̊

Richest Customer Sum Max

However, it has no effect.
Like inject, maximum also works horizontally. To find the maximum column wise, we specify 2 for the axis:

Richest Customer Sum Max 2

Prime Numbers

Finding primes between 2 to 50.

Prime Assign

(Excluding 1 for simplicity.)

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.

Prime Iverson

The ‘multiplication table’ APL solution.

(~ m ∊ m ∘.× m) / m ← 1↓⍳50

It’s short and sweet. Definitely not fast.

Prime Shortest

Sort by Parity

From LeetCode
Video Commentary

Sort By Parity Assign
Sort By Parity Main

Maximum Count of Positive and Negative Integer

From LeetCode
Video: 1 Problem, 24 Languages

Maximum Count Positives Negatives Assign

Spaces are used to separate items in the list.
Optionally we can use comma or semicolon for the sake of clarity.

Maximum Count Positives Negatives Main

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:

Maximum Count Positives Negatives Infix

Rearrange Array to Maximize Prefix Score

LeetCode
Video: Why I Love BQN So Much! (vs Python)

Maximize Prefix Score Step 0
Maximize Prefix Score Step 1
Maximize Prefix Score Step 2
Maximize Prefix Score Step 3
Maximize Prefix Score Step 4

Height Checker

From LeetCode
Video commentary

Height Order 1

It’s easier to read in 2 lines.

Height Order 2

Let’s use ⧻₁ to show our intention better.

Height Order 3

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

Height Order 4

R allows us to break down a complex expression into smaller simpler chunks.

Height Order 5

Max Difference Between Increasing Elements

From LeetCode
Video: Functional vs Array Programming

Max Diff Between Increasing Elements Assign
Max Diff Between Increasing Elements Single

Using R to construct the solution:

Max Diff Between Increasing Elements Multi

Elimination Sort

From APL Quest
Video commentary

t ≔ [1 3 7 3 5 8 5 8 1 6 1 8 1 10 8 4 3 4 1 4]
Elimination Sort Main

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).

Multiple Of 3 And 5 R1

Similar expression but factoring out 3 and 5.

Multiple Of 3 And 5 R2

Moment of truth — let’s find all the multiples under 1000:

Multiple Of 3 And 5 R2 B

Sum of Squares

Video commentary

Sum Of Squares Assign
Sum Of Squares Main

Difference Between Ones and Zeros

From LeetCode
Video commentary

Difference Of 1 And 0 Assign
Difference Of 1 And 0 Main

Left and Right Sum Differences

From LeetCode
Video commentary

Left And Right Sum Differences Assign
Left And Right Sum Differences Format 1

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:

Left And Right Sum Differences Format 2

‘What sums up 2020?’

From Advent of Code
Video commentary

Advent Of Code 2020 Day 1 Assign
Advent Of Code 2020 Day 1 Main

Check if Matrix is X-Matrix

From LeetCode
Video commentary

Cross Matrix Assign
Cross Matrix Main