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
┌────────────┬──────┐
│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 function to filter for all flights on the date given as a right function argument ⍵
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 absolute differences in their latitude and longitudes 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│315│
└─────────┴──────────┴───┘
floor bookings
┌─────────┬──────────┬───┬─┐
│Jari N. │2024-12-01│427│4│
├─────────┼──────────┼───┼─┤
│Erik O. │2024-12-01│506│5│
├─────────┼──────────┼───┼─┤
│Michel A.│2024-12-03│315│3│
└─────────┴──────────┴───┴─┘
Write problem 6
Write a function to group an array of hotel bookings by floor.
bookings
┌─────────┬──────────┬───┬─┐
│Jari N. │2024-12-01│427│4│
├─────────┼──────────┼───┼─┤
│Erik O. │2024-12-01│506│5│
├─────────┼──────────┼───┼─┤
│Michel A.│2024-12-03│415│4│
└─────────┴──────────┴───┴─┘
group bookings
┌────────────────────────────┐
│┌─────────┬──────────┬───┬─┐│
││Jari N. │2024-12-01│427│4││
│├─────────┼──────────┼───┼─┤│
││Michel A.│2024-12-03│415│4││
│└─────────┴──────────┴───┴─┘│
├────────────────────────────┤
│┌───────┬──────────┬───┬─┐ │
││Erik O.│2024-12-01│506│5│ │
│└───────┴──────────┴───┴─┘ │
└────────────────────────────┘
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, 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.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.
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
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
┌─┬─┬─┬─┐
│a│b│c│d│
├─┼─┼─┼─┤
│e│f│g│h│
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│1│2│3│4│
├─┼─┼─┼─┤
│5│6│7│8│
└─┴─┴─┴─┘
Hint: Use the partition ⊆ function