matrixlookup-tablesapldyalog

Get the day(s) with the highest temperature from a table


I'm trying to get the highest temperature day(s) in APL.

This is my code:

      days ← 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday' 'Sunday'
      temp ← 7 1 1 ⍴78 80 89 82 79 89 73
      Centigrade ← (5÷9) × {(⍵ - 32)} 
      AverageTemp ← days,⍪(+/÷≢)⍤1⊢Centigrade temp
      AverageTemp
┌─────────┬───────────┐
│Monday   │25.55555556│
├─────────┼───────────┤
│Tuesday  │26.66666667│
├─────────┼───────────┤
│Wednesday│31.66666667│
├─────────┼───────────┤
│Thursday │27.77777778│
├─────────┼───────────┤
│Friday   │26.11111111│
├─────────┼───────────┤
│Saturday │31.66666667│
├─────────┼───────────┤
│Sunday   │22.77777778│
└─────────┴───────────┘

Based on this sample, it should return Wednesday and Saturday

link for quick test


Solution

  • First, let's find what the maximum average temperature is:

          ⌈/AverageTemp[;2]
    31.66666667
    

    Now we want to find which rows have that value, as a mask:

          AverageTemp[;2] = ⌈/AverageTemp[;2]
    0 0 1 0 0 1 0
    

    We can then use that mask to filter the day names:

          days /⍨ AverageTemp[;2] = ⌈/AverageTemp[;2]
    ┌─────────┬────────┐
    │Wednesday│Saturday│
    └─────────┴────────┘
    

    You code could be improved a bit, but that is off-topic here. If you want such feedback, please re-post on Code Review.