Home

Help
2022










2021
2020
2019
2018
2017
2016
2015
2014
2013

APL Practice Problems

from the 2022 APL Problem Solving Competition

1: Counting DNA Nucleotides?

This problem was inspired by Counting DNA Nucleotides found on the excellent bioinformatics website rosalind.info.

Write a function that:

  • takes a right argument that is a character vector or scalar representing a DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T').
  • returns a 4-element numeric vector containing the counts of each symbol 'A', 'C', 'G', and 'T' respectively.

Hint: The key operator f⌸ or the outer product operator ∘.g could be helpful.

Examples

      
      (your_function) 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
20 12 17 21

      (your_function) ''
0 0 0 0

      (your_function) 'G'
0 0 1 0

2: Attack of the Mutations!

This problem is inspired by the Counting Point Mutations problem found on the excellent Bioinformatics education website rosalind.info.

Write a function that:

  • takes right and left arguments that are character vectors or scalars of equal length – these represent DNA strings.
  • returns an integer representing the Hamming distance (the number of differences in corresponding positions) between the arguments.

Hint: The plus function X+Y could be helpful.

Examples

      'GAGCCTACTAACGGGAT' (your_function) 'CATCGTAATGACGGCCT' 
7

      'A' (your_function) 'T'
1

      '' (your_function) ''
0
 
      (your_function)⍨ 'CATCGTAATGACGGCCT'
0

3: Uniquely Qualified

Write a function that:

  • takes right and left arguments that are arrays of arbitrary rank, depth, and value.
  • returns a vector of all elements that appear in either of the two argument arrays but not in both. The order of elements in the result is not significant.

Hint: The without function X~Y could be helpful.

Examples

      'DYALOG' (your_function) 'APL'
DYOGP

      'DYALOG'  (your_function) ⊂'APL'
┌─┬─┬─┬─┬─┬─┬───┐
│D│Y│A│L│O│G│APL│
└─┴─┴─┴─┴─┴─┴───┘

      (2 2⍴'Hello'(⊂'World')(2 2⍴⍳4)42) (your_function) 42 'Have a nice day'
┌─────┬───────┬───┬───────────────┐
│Hello│┌─────┐│1 2│Have a nice day│
│     ││World││3 4│               │
│     │└─────┘│   │               │
└─────┴───────┴───┴───────────────┘

      1 1 1 (your_function) 2 2
1 1 1 2 2

4: In the Long One…

Write a function that:

  • takes a right argument that is a Boolean scalar or vector.
  • returns the length of the longest sequence of consecutive 1s.

Hint: The partition function X⊆fY could be helpful.

Examples

      (your_function) 1 1 1 0 1 1 0 0 1 1 1 1 0
4

      (your_function) ⍬
0

      (your_function) 1
1

      (your_function) 0
0

      (your_function) 12/0 1 0 1
12

5: Stairway to Heaven

(with apologies to Led Zeppelin)

Write APL function that:

  • Given a scalar integer argument, n, in the range 0-100.
  • Returns a character matrix comprised of spaces and ⎕ that resembles an n-level left-to-right ascending stairway.

Hint: The index generator function ⍳Y could help with solving this problem.


Examples

      
      (your_function) 10
         ⎕
        ⎕⎕
       ⎕⎕⎕
      ⎕⎕⎕⎕
     ⎕⎕⎕⎕⎕
    ⎕⎕⎕⎕⎕⎕
   ⎕⎕⎕⎕⎕⎕⎕
  ⎕⎕⎕⎕⎕⎕⎕⎕
 ⎕⎕⎕⎕⎕⎕⎕⎕⎕
⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕

      (your_function) 0 ⍝ returns a 0×0 matrix

6: Pyramid Scheme

Write a monadic function that:

  • takes an argument n that is an integer scalar in the range 0-100.
  • returns a square matrix "pyramid" with 0⌈¯1+2×n rows and columns of n increasing concentric levels.</br> By this we mean that the center element of the matrix will be n, surrounded on all sides by n-1.

Hint: The functions minimum X⌊Y and reverse ⌽Y, and the outer product operator X∘.gY could be helpful.


Examples

      (your_function) 3
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

      (your_function) 5
1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 1
1 2 3 3 3 3 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1
      
      (your_function) 1 ⍝ should return 1 1⍴1
1      

      (your_function) 0 ⍝ should return 0 0⍴0

7: Just Golfing Around

Apologies to the code golfers out there, but this problem has nothing to do with code golf! Instead, it addresses the problem of assigning places in a golf tournament. In regular golf, lower scores place higher – the lowest score places first and the highest score places last.

Write a function that:

  • takes a right argument that is a non-decreasing vector or scalar of strictly positive integers, representing a set of scores.
  • returns a numeric vector of the place for each score; for duplicate scores, it returns the average of the places they hold.

Hint: This problem has several viable approaches including using key f⌸, or partition X⊆Y, or interval index X⍸Y.


Examples

      (your_function) 1 2 3 4 5
1 2 3 4 5
      
      (your_function) 68 71 71 73
1 2.5 2.5 4

      (your_function) 67 68 68 69 70 70 70 71 72
1 2.5 2.5 4 6 6 6 8 9

      (your_function) 6⍴70
3.5 3.5 3.5 3.5 3.5 3.5

      (your_function) ⍬ ⍝ this should return an empty vector


      (your_function) 67 ⍝ should work with a scalar argument
1

8: Let’s Split!

Write a function that:

  • takes a right argument that is a non-empty character vector or scalar.
  • takes a left argument that is a non-empty character vector or scalar.
  • returns a 2-element vector of character vectors in which the right argument is split immediately before the first occurence of any element in the left argument. If no left-argument element occurs in the right argument, then the split should happen after the last element of the right argument.

Hint: The take X↑Y and drop X↓Y functions, or the partitioned enclose function X⊂Y, could be helpful.

Examples

      'do' (your_function) 'Hello World'
┌────┬───────┐
│Hell│o World│
└────┴───────┘

      'KEI' (your_function) ⎕A ⍝ ⎕A is the system constant that contains the characters A-Z 
┌────┬──────────────────────┐
│ABCD│EFGHIJKLMNOPQRSTUVWXYZ│
└────┴──────────────────────┘

      (⌽⎕A) (your_function) ⎕A
┌┬──────────────────────────┐
││ABCDEFGHIJKLMNOPQRSTUVWXYZ│
└┴──────────────────────────┘

      ⎕D (your_function) ⎕A ⍝ ⎕D is the system constant that contains the characters 0-9 
┌──────────────────────────┬┐
│ABCDEFGHIJKLMNOPQRSTUVWXYZ││
└──────────────────────────┴┘

      ⎕D (your_function) 'Q'
┌─┬┐
│Q││
└─┴┘
      ⎕A (your_function) 'Q'
┌┬─┐
││Q│
└┴─┘

9: An Average Window (or a Windowed Average)

Write a function that:

  • takes a right argument Y that is a numeric scalar or non-empty vector.
  • takes a left argument X that represents the number of neighboring elements on either side of each element in Y.
  • returns a numeric vector or scalar where each element is the average (mean) of the corresponding element in Y and its X neighbors on either side. If an element has fewer than X neighbors on either side, replicate the first and last values as necessary to make X neighbors.

Hint: The Reduce N-Wise operator Xf/Y could help with solving this problem.

Examples


      0 (your_function) 1 2 3 4 5 6 ⍝ 0 neighbors on each side
1 2 3 4 5 6

      1 (your_function) 1 2 3 4 5 6 ⍝ 1 neighbors on each side
1.333333333 2 3 4 5 5.666666667

      2 (your_function) 1 2 3 4 5 6 ⍝ 2 neighbors on each side
1.6 2.2 3 4 4.8 5.4

      6 (your_function) 1 2 3 4 5 6
2.538461538 2.923076923 3.307692308 3.692307692 4.076923077 4.461538462

      10 (your_function) 42
42    

10: Separation Anxiety

Write a function that:

  • takes a right argument that is a character vector or scalar representing a valid non-negative integer.
  • takes a left argument that is a character scalar "separator" character.
  • returns a character vector that is a representation of the right argument formatted such that the separator character is found between trailing groups of 3 digits.

Note that the number of digits in the character representation might exceed the number of digits that can be represented as a 32-bit integer.

Hint: The at operator @ could be helpful.


Examples

      ',' (your_function)¨'1' '10' '100' '1000' '10000' '100000' '1000000' '10000000' '100000000' '1000000000' '10000000000'
┌─┬──┬───┬─────┬──────┬───────┬─────────┬──────────┬───────────┬─────────────┬──────────────┐
│1│10│100│1,000│10,000│100,000│1,000,000│10,000,000│100,000,000│1,000,000,000│10,000,000,000│
└─┴──┴───┴─────┴──────┴───────┴─────────┴──────────┴───────────┴─────────────┴──────────────┘
          
      '.' (your_function) 60⍴⌽⎕D
987.654.321.098.765.432.109.876.543.210.987.654.321.098.765.432.109.876.543.210
      
      '/' (your_function) ,'9' ⍝ scalars and 1-element character vectors are equivalent
9