Skip to content

Write exercises

Submit APL code for the following exercises. Your submissions are graded using TMC.

Make sure to log in to be able to submit your work!

Remember, you can test out ideas and develop you solution at TryAPL before submitting it here.

You can submit as many solutions as you like. If you submit a correct solution at least once, you will receive points on the TMC server and be able to see the model solution.

If the write problem is red, it is unsolved. If it it green, you have solved it and received points for it on the server.


Write problem 2

Write a function to add a new entry at the top of the following leaderboard array as right argument ⍵ with a score of 1000000 with a name given by a left argument ⍺.

      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 function to filter for all flights on the date given as a right function argument ⍵

      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.  StockholmGothenburg 2024-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 absolute differences in their latitude and longitudes 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-03315
└─────────┴──────────┴───┘
      floor bookings
┌─────────┬──────────┬───┬─┐
Jari N.  2024-12-014274
├─────────┼──────────┼───┼─┤
Erik O.  2024-12-015065
├─────────┼──────────┼───┼─┤
Michel A.2024-12-033153
└─────────┴──────────┴───┴─┘

floor←


Write problem 6

Write a function to group an array of hotel bookings by floor.

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

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, with number of values from 0 to 2pi determined by the right argument ⍵.

      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.

nonzero←


Write problem 11

Write a function to remove all 2-cells of a matrix 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 matrix that the character vector represents.

The character vector will have elements seprated by commas, 1-cells separated by semicolons, and 2-cells seprated by colons.

      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←