Back to General

Operations on Lists (Iterators & Amend) · General · Practice Question

Drop Every Third Letter from a List

Sign in to track your progress

Question

Given .Q.A, the built-in list of alphabet characters:

q).Q.A
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Drop every 3rd letter.

Expected result:

"ABDEGHJKMNPQSTVWYZ"

Solution

.Q.A where ((til count .Q.A) mod 3)<2

Alternative Solution

raze (2#) each 3 cut .Q.A

Alternative Solution

raze 2#'3 cut .Q.A

Explanation

q)(til count .Q.A) mod 3
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1

labels every position with its position-in-group-of-3 — 0, 1, or 2. Every 3rd letter (positions 2, 5, 8, ...) is the one labelled 2, so keeping only the positions where that label is <2 drops exactly those letters.

The alternatives take a different route: 3 cut .Q.A first splits the alphabet into consecutive groups of 3 ("ABC", "DEF", "GHI", ...), then 2# takes just the first 2 characters of each group — dropping the 3rd — before raze flattens the groups back into one list. (2#) each and 2#' are equivalent here, since the "take 2" operation doesn't depend on anything other than the group it's applied to.