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
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
┌────────────┬──────┐
│SpaceTornado│999905│
├────────────┼──────┤
│StarSpangler│999875│
├────────────┼──────┤
│MoonJuice │999870│
├────────────┼──────┤
│RocketRanger│894900│
├────────────┼──────┤
│AlienNation │545990│
└────────────┴──────┘
'PhotonFury' cheat leaderboard
┌────────────┬───────┐
│PhotonFury │1000000│
├────────────┼───────┤
│SpaceTornado│999905 │
├────────────┼───────┤
│StarSpangler│999875 │
├────────────┼───────┤
│MoonJuice │999870 │
├────────────┼───────┤
│RocketRanger│894900 │
├────────────┼───────┤
│AlienNation │545990 │
└────────────┴───────┘
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-01│08:00│
├─────────┼─────────┼───────────┼──────────┼─────┤
│Erik O. │Stockholm│Gothenburg │2024-12-01│14:00│
├─────────┼─────────┼───────────┼──────────┼─────┤
│Michel A.│London │Los Angeles│2024-12-03│11:30│
└─────────┴─────────┴───────────┴──────────┴─────┘
flights on_date '2024-12-01'
┌───────┬─────────┬──────────┬──────────┬─────┐
│Jari N.│Helsinki │Tallinn │2024-12-01│08:00│
├───────┼─────────┼──────────┼──────────┼─────┤
│Erik O.│Stockholm│Gothenburg│2024-12-01│14:00│
└───────┴─────────┴──────────┴──────────┴─────┘
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
┌────────┬───────┬─────────┐
│Helsinki│60.1695│24.9354 │
├────────┼───────┼─────────┤
│Juneau │58.3019│¯134.4197│
├────────┼───────┼─────────┤
│Beirut │33.8886│35.4955 │
├────────┼───────┼─────────┤
│Havana │23.1136│¯82.3666 │
└────────┴───────┴─────────┘
distance cities
┌────────┬───────────────┬────────────────┬────────────────┬───────────────┐
│X │Helsinki │Juneau │Beirut │Havana │
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
│Helsinki│0 0 │1.8676 159.3551 │26.2809 10.5601 │37.0559 107.302│
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
│Juneau │1.8676 159.3551│0 0 │24.4133 169.9152│35.1883 52.0531│
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
│Beirut │26.2809 10.5601│24.4133 169.9152│0 0 │10.775 117.8621│
├────────┼───────────────┼────────────────┼────────────────┼───────────────┤
│Havana │37.0559 107.302│35.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.
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-01│427│
├─────────┼──────────┼───┤
│Erik O. │2024-12-01│506│
├─────────┼──────────┼───┤
│Michel A.│2024-12-03│415│
└─────────┴──────────┴───┘
floor bookings
┌─────────┬──────────┬───┬─┐
│Jari N. │2024-12-01│427│4│
├─────────┼──────────┼───┼─┤
│Erik O. │2024-12-01│506│5│
├─────────┼──────────┼───┼─┤
│Michel A.│2024-12-03│415│4│
└─────────┴──────────┴───┴─┘
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-01│427│4│
├─────────┼──────────┼───┼─┤
│Erik O. │2024-12-01│506│5│
├─────────┼──────────┼───┼─┤
│Michel A.│2024-12-03│415│4│
└─────────┴──────────┴───┴─┘
group bookings2
┌────────────────────────────┬──────────────────────────┐
│┌─────────┬──────────┬───┬─┐│┌───────┬──────────┬───┬─┐│
││Jari N. │2024-12-01│427│4│││Erik O.│2024-12-01│506│5││
│├─────────┼──────────┼───┼─┤│└───────┴──────────┴───┴─┘│
││Michel A.│2024-12-03│415│4││ │
│└─────────┴──────────┴───┴─┘│ │
└────────────────────────────┴──────────────────────────┘
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
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 1│9 9 9│2 2│3│4 4│
└───┴─────┴───┴─┴───┘
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.256637061│0.9510565163 │0.3090169944 │
├───────────┼────────────────┼─────────────┤
│2.513274123│0.5877852523 │¯0.8090169944│
├───────────┼────────────────┼─────────────┤
│3.769911184│¯0.5877852523 │¯0.8090169944│
├───────────┼────────────────┼─────────────┤
│5.026548246│¯0.9510565163 │0.3090169944 │
├───────────┼────────────────┼─────────────┤
│6.283185307│¯2.449293598E¯16│1 │
└───────────┴────────────────┴─────────────┘
trig_table 1
┌───────────┬────────────────┬─────┐
│x │sin x │cos x│
├───────────┼────────────────┼─────┤
│0 │0 │1 │
├───────────┼────────────────┼─────┤
│3.141592654│1.224646799E¯16 │¯1 │
├───────────┼────────────────┼─────┤
│6.283185307│¯2.449293598E¯16│1 │
└───────────┴────────────────┴─────┘
trig_table 0
┌───────────┬────────────────┬─────┐
│x │sin x │cos x│
├───────────┼────────────────┼─────┤
│0 │0 │1 │
├───────────┼────────────────┼─────┤
│6.283185307│¯2.449293598E¯16│1 │
└───────────┴────────────────┴─────┘
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
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
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
┌─┬─┬─┬─┐
│a│b│c│d│
├─┼─┼─┼─┤
│e│f│g│h│
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│1│2│3│4│
├─┼─┼─┼─┤
│5│6│7│8│
└─┴─┴─┴─┘
Hint: Use the partition ⊆ function