Skip to content

Write exercises

Remember, you can test your code out on TryAPL before submitting it here! You can submit as many solutions as you like.

Since this course is still a work-in-progress, solving the write exercises will currently not lead to any credits.


Write problem 1

Write a dfn that calculates the harmonic mean of its left and right arguments, mathematically defined as \(H(\alpha,\omega) = \frac{2}{\frac{1}{\alpha}+\frac{1}{\omega}}\)

H←


Write problem 2

Write a dfn that calculates the Heronian mean of its left and right arguments, mathematically defined as \(H(\alpha,\omega) = \frac{1}{3} \cdot (\alpha+\sqrt{\alpha\omega}+\omega)\)

H←


Write problem 3

Write a dfn that calculates the geometric mean of its left and right arguments, mathematically defined as \(H(\alpha,\omega) = \sqrt{\alpha\omega}\)

H←


Write problem 4

Write a dfn that calculates the arithmetic geometric mean of its left and right arguments, which is defined recursively. Recurse with the arithmetic mean of and as the new left argument and their geometric mean as the new right argument, until the two are equal. Mathematically, $$ H(\alpha, \omega) = \left\{ \begin{array}{ll} \omega & \alpha=\omega \\ H(\frac{\alpha + \omega}{2}, \sqrt{\alpha \omega}) & \alpha≠\omega \\ \end{array} \right. $$

H←


Write problem 5

Write a dfn that allows the user to choose between three means. The function should take in a right argument array, where the first element is an integer representing which mean to use (see table below), and that particular mean is calculated for the next two elements. \[ \begin{array}{cc} 1 & \text{Arithmetic mean} \\ 2 & \text{Geometric mean} \\ 3 & \text{Arithmetic-Geometric mean} \\ \end{array} \]

H←


Write problem 6

Write a dfn called My that displays personal information. The My function should take a string right argument and return the following

      My 'Name'
DASH
      My 'Age'
20
      My 'Profession'
Engineer
      My 'Company'
Future Gadget Lab LLC

Hint: Use the match ≡ function.

My←


Write problem 7

Recall the table of rock, paper, scissors outcomes given in the Chapter 2 Write problem 12.

      ROUND  3 3  'DRAW' 'P2 COVERS' 'P1 BLUNTS' 'P1 COVERS' 'DRAW' 'P2 CUTS' 'P2 BLUNTS' 'P1 CUTS' 'DRAW'
      ROUND
┌─────────┬─────────┬─────────┐
DRAW     P2 COVERSP1 BLUNTS
├─────────┼─────────┼─────────┤
P1 COVERSDRAW     P2 CUTS  
├─────────┼─────────┼─────────┤
P2 BLUNTSP1 CUTS  DRAW     
└─────────┴─────────┴─────────┘

The rows specify the move made by player 1, and the columns the move made by player 2, both in the order ROCK, PAPER, SCISSORS.

Create a dfn PLAY that takes the move made by player 1 as its left argument and the move made by player 2 as its right argument , each a number from 1 to 3, and returns the outcome of the round.

PLAY←


Write problem 8

The Caesar cipher encrypts a piece of text by shifting all its letters a fixed amount of places along the alphabet. For example, 'BUNNY' (with indices 2 21 14 14 25) under a shift of 17 places (26 | 2 21 14 14 25 + 17 -> 19 12 5 5 16) becomes 'SLEEP'.

Create a dfn that does the intermediate operation of this cipher by shifting an integer right argument vector by an integer left argument, modulo 26.

SHIFT26←


Write problem 9

Create a dfn that returns a matrix of zeroes of size by , with a 1 in a position specified by the vector right argument .

      2 SPARSE 2 1
0 0
1 0
      10 SPARSE 5 4
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
SPARSE←


Write problem 10

Create a dfn that plots a heatmap of a matrix, that is, a * where the matrix is above a certain value and an underscore _ otherwise. For example,

      M  10 10⍴⍳9
      M
1 2 3 4 5 6 7 8 9 1
2 3 4 5 6 7 8 9 1 2
3 4 5 6 7 8 9 1 2 3
4 5 6 7 8 9 1 2 3 4
5 6 7 8 9 1 2 3 4 5
6 7 8 9 1 2 3 4 5 6
7 8 9 1 2 3 4 5 6 7
8 9 1 2 3 4 5 6 7 8
9 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 1

      7 PLOT M
_______**_
______**__
_____**___
____**____
___**_____
__**______
_**_______
**_______*
*_______**
_______**_

Hint: Index the vector '_*' by a matrix

PLOT←


Write problem 11

Create a dfn that returns the singular form of a noun given as a vector right argument , returning 'AN' with the noun when the noun begins with a vowel, and 'A' with the noun otherwise. In this case, 'Y' is not considered a vowel. The nouns will be given in UPPERCASE.

      SINGULAR 'DREAM'
A DREAM
      SINGULAR 'SNOOZE'
A SNOOZE
      SINGULAR 'AERODROME'
AN AERODROME

SINGULAR←


Write problem 12

Create a dfn that determines an APL MOOC student's final grade. The right argument is the number of exercises completed. For the sake of this problem, you can assume that the APL MOOC has 72 exercises. The grade boundaries and expected results are given below

      ⍝ Less than 60%
      GRADE 42
0
      ⍝ Between 60% and 70%
      GRADE 44
1
      GRADE 50
1

      ⍝ Between 70% and 80%
      GRADE 51
2
      GRADE 57
2

      ⍝ Between 80% and 90%
      GRADE 58
3
      GRADE 64
3

      ⍝ Between 90% and 99%
      GRADE 65
4
      GRADE 71
4

      ⍝ Between 99% and 100%
      GRADE 72
5

      ⍝ Any higher returns nothing
      GRADE 73

GRADE←