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 only that returns the elements of its left argument ⍺ that do not occur in its right argument ⍵, without duplicates.

      'MISSISSIPPI' only 'SIP'
M
      2 4 4 6 8 only 4 5
2 6 8
only←


Write problem 2

Write a dfn cheat that takes a name as left argument ⍺ and the leaderboard below as right argument ⍵, and adds a new entry at the top with that name and a score of 1000000.

      leaderboard
┌────────────┬──────┐
SpaceTornado999905
├────────────┼──────┤
StarSpangler999875
├────────────┼──────┤
MoonJuice   999870
├────────────┼──────┤
RocketRanger894900
├────────────┼──────┤
AlienNation 545990
└────────────┴──────┘
      'PhotonFury' cheat leaderboard
┌────────────┬───────┐
PhotonFury  1000000
├────────────┼───────┤
SpaceTornado999905 
├────────────┼───────┤
StarSpangler999875 
├────────────┼───────┤
MoonJuice   999870 
├────────────┼───────┤
RocketRanger894900 
├────────────┼───────┤
AlienNation 545990 
└────────────┴───────┘

cheat←


Write problem 3

Write a dfn that takes the flight table below as left argument ⍺ and a date as right argument ⍵, and returns the rows of the flights on that date

      flights
┌─────────┬─────────┬───────────┬──────────┬─────┐
Jari N.  Helsinki Tallinn    2024-12-0108:00
├─────────┼─────────┼───────────┼──────────┼─────┤
Erik O.  StockholmGothenburg 2024-12-0114:00
├─────────┼─────────┼───────────┼──────────┼─────┤
Michel A.London   Los Angeles2024-12-0311:30
└─────────┴─────────┴───────────┴──────────┴─────┘
      flights on_date '2024-12-01'
┌───────┬─────────┬──────────┬──────────┬─────┐
Jari N.Helsinki Tallinn   2024-12-0108:00
├───────┼─────────┼──────────┼──────────┼─────┤
Erik O.StockholmGothenburg2024-12-0114:00
└───────┴─────────┴──────────┴──────────┴─────┘

on_date←


Write problem 4

Given an array of cities with their latitudes and longitudes as right argument ⍵, create a function to return a matrix of the absolute differences in latitude and longitude between each pair of cities (not great-circle distances), in the following format

      cities
┌────────┬───────┬─────────┐
Helsinki60.169524.9354  
├────────┼───────┼─────────┤
Juneau  58.3019¯134.4197
├────────┼───────┼─────────┤
Beirut  33.888635.4955  
├────────┼───────┼─────────┤
Havana  23.1136¯82.3666 
└────────┴───────┴─────────┘
      distance cities
┌────────┬───────────────┬────────────────┬────────────────┬───────────────┐
X       Helsinki       Juneau          Beirut          Havana         
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
Helsinki0 0            1.8676 159.3551 26.2809 10.5601 37.0559 107.302
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
Juneau  1.8676 159.35510 0             24.4133 169.915235.1883 52.0531
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
Beirut  26.2809 10.560124.4133 169.91520 0             10.775 117.8621
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
Havana  37.0559 107.30235.1883 52.0531 10.775 117.8621 0 0            
└────────┴───────────────┴────────────────┴────────────────┴───────────────┘

Hint: Remove the labels from the matrix before computing the distances, then add them afterwards. Use the rank operator ⍤1 2.

distance←


Write problem 5

Write a function to add an extra floor number column to a hotel reservation array. The floor number is always the first digit of the room number, which is stored as a character vector.

      bookings
┌─────────┬──────────┬───┐
Jari N.  2024-12-01427
├─────────┼──────────┼───┤
Erik O.  2024-12-01506
├─────────┼──────────┼───┤
Michel A.2024-12-03415
└─────────┴──────────┴───┘
      floor bookings
┌─────────┬──────────┬───┬─┐
Jari N.  2024-12-014274
├─────────┼──────────┼───┼─┤
Erik O.  2024-12-015065
├─────────┼──────────┼───┼─┤
Michel A.2024-12-034154
└─────────┴──────────┴───┴─┘

floor←


Write problem 6

Write a function to group an array of hotel bookings by floor (the fourth column), returning one matrix per floor, in order of first appearance.

      bookings2
┌─────────┬──────────┬───┬─┐
Jari N.  2024-12-014274
├─────────┼──────────┼───┼─┤
Erik O.  2024-12-015065
├─────────┼──────────┼───┼─┤
Michel A.2024-12-034154
└─────────┴──────────┴───┴─┘
      group bookings2
┌────────────────────────────┬──────────────────────────┐
│┌─────────┬──────────┬───┬─┐│┌───────┬──────────┬───┬─┐│
││Jari N.  2024-12-014274│││Erik O.2024-12-015065││
│├─────────┼──────────┼───┼─┤│└───────┴──────────┴───┴─┘│
││Michel A.2024-12-034154││                          
│└─────────┴──────────┴───┴─┘│                          
└────────────────────────────┴──────────────────────────┘

group←


Write problem 7

Write a function to turn the elements of a matrix into proportions of the sum of their row.

      matrix
0 0 0 0 1
0 0 0 1 1
1 1 1 1 1
0 0 0 3 1
0 1 1 1 1
      proportion matrix
0   0    0    0    1   
0   0    0    0.5  0.5 
0.2 0.2  0.2  0.2  0.2 
0   0    0    0.75 0.25
0   0.25 0.25 0.25 0.25

proportion←


Write problem 8

Write a function to group runs of elements which are equal.

      runs 1 1 9 9 9 2 2 3 4 4
┌───┬─────┬───┬─┬───┐
1 19 9 92 234 4
└───┴─────┴───┴─┴───┘

runs←


Write problem 9

Write a function that generates a table of values for sin and cos from 0 to 2pi. The right argument ⍵ is the number of interior points, that is, the number of points strictly between 0 and 2pi. The step between consecutive points is 2pi÷⍵+1. The table has ⍵+2 rows of values, under a first row of the labels 'x', 'sin x' and 'cos x'.

      trig_table 4
┌───────────┬────────────────┬─────────────┐
x          sin x           cos x        
├───────────┼────────────────┼─────────────┤
0          0               1            
├───────────┼────────────────┼─────────────┤
1.2566370610.9510565163    0.3090169944 
├───────────┼────────────────┼─────────────┤
2.5132741230.5877852523    ¯0.8090169944
├───────────┼────────────────┼─────────────┤
3.769911184¯0.5877852523   ¯0.8090169944
├───────────┼────────────────┼─────────────┤
5.026548246¯0.9510565163   0.3090169944 
├───────────┼────────────────┼─────────────┤
6.283185307¯2.449293598E¯161            
└───────────┴────────────────┴─────────────┘
      trig_table 1
┌───────────┬────────────────┬─────┐
x          sin x           cos x
├───────────┼────────────────┼─────┤
0          0               1    
├───────────┼────────────────┼─────┤
3.1415926541.224646799E¯16 ¯1   
├───────────┼────────────────┼─────┤
6.283185307¯2.449293598E¯161    
└───────────┴────────────────┴─────┘
      trig_table 0
┌───────────┬────────────────┬─────┐
x          sin x           cos x
├───────────┼────────────────┼─────┤
0          0               1    
├───────────┼────────────────┼─────┤
6.283185307¯2.449293598E¯161    
└───────────┴────────────────┴─────┘

trig_table←


Write problem 10

Write a function to remove all zero columns from a matrix.

      M
0 1 0 2
0 3 0 0
0 4 0 5
      nonzero M
1 2
3 0
4 5

nonzero←


Write problem 11

Write a function to remove all 2-cells of a rank-3 array with sum less than 10.

      M
 1  1  1
 1  1  1
 1  1  1

 1  2  3
 4  5  6
 7  8  9

10 11 12
13 14 15
16 17 18

      significant M
 1  2  3
 4  5  6
 7  8  9

10 11 12
13 14 15
16 17 18

significant←


Write problem 12

Write a function that takes in a right argument character vector and returns the array that the character vector represents.

In the character vector, commas separate the elements of a 1-cell, semicolons separate 1-cells, and colons separate 2-cells.

      a
a,b,c,d;e,f,g,h:1,2,3,4;5,6,7,8

      array a
┌─┬─┬─┬─┐
abcd
├─┼─┼─┼─┤
efgh
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
1234
├─┼─┼─┼─┤
5678
└─┴─┴─┴─┘

Hint: Use the partition ⊆ function

array←