Solutions to some APL problems

Table of contents

Dyalog APL challengeπŸ”—

2026.2 - 9πŸ”—

Write a function that takes an uppercase word consisting of English letters and counts how many leading consonants it has. For the purposes of this problem, use 'AEIOU' as your list of vowels (non-consonants).

{+/∧\1-∨⌿'AEIOU'∘.=⍡}

We build a table of vowel occurrences and do a logical OR along the columns to get an array of vowel occurrences (0 for consonants and 1 for vowels). Then we NOT the array and use +/∧\ to count how many leading 1s it has.

2026.2 - 10πŸ”—

Write a function that takes an uppercase word consisting of English letters and returns the first vowel followed by the first consonant. The word is guaranteed to contain at least one vowel and one consonant.

{⍡[1 0⍳⍨∨⌿'AEIOU'∘.=⍡]}

As before ∨⌿'AEIOU'∘.=⍡ generates an array of vowel occurrences. Call the ⌈result v. v⍳1 0 gives us the indices of the first vowel and first consonant, and we pick those indices from ⍡.

APL quest problemsπŸ”—

2018.1πŸ”—

Problem.

{β‰’ βˆͺ ⌈\ ⍡}
{+/ β‰  ⌈\ ⍡}

Compute the max-scan, then ask how many unique elements there are.

Another nice idea is to compare pairs of adjacent numbers in the max-scan and add up the points of difference. We'll need add 1 to get the right answer, and also special-case the empty array case.

{⍬≑⍡: 0 β‹„ 1+ +/ 2</ ⌈\ ⍡}

This is more efficient because there's no hashing involved. We're making use of the structure of the max-scan (strictly increasing). We can use cmpx to profile these implementations.

2018.2πŸ”—

Problem

The fractional part is the remainder modulo 1, and we can subtract that from the input to get the integral part.

{(⍡-rem),rem←1|⍡}

We can use ⌊ to compute the integral part, and then subtract that from the input to get the fractional part.

{⌊ , ⍡-⌊⍡}

We can rewrite the right argument to , as a 3-train ⊒-⌊ and then go one step further and turn the entire thing into a 5-train.

⌊ , ⊒ - ⌊

Combining with the previous idea:

⌊ , 1∘|

Or just use the encode function:

0 1∘⊀

2018.3πŸ”—

Problem

This uses the idiom ⍺⍨¨⍡ which creates an array in the "shape" of ⍡, with each element being ⍺. The actual shape of the result will be different if ⍺ is itself an array.

{{⍺ ('*'⍨¨⍡)} ⌸ , +/¨ ⍳⍡}

For example, try

(2 4 ⍴ ⍳ 8) ⍨¨ ⍳ 3

To avoid the problem of generating high-rank matrices (which caps us at inputs of length 15 or less), we can use encode:

{{⍺ ('*'⍨¨⍡)}⌸+⌿1+(,⍡)⊀¯1+⍳×/⍡}

Notice how we have to use ,⍡ instead of ⍡ as the left arugment to ⊀ (encode). This handles the case of when ⍡ is a scalar.