获取表格中最高温度的日期(日期)。

huangapple go评论103阅读模式
英文:

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

问题

我正在尝试获取APL中最高温度的日期。

这是我的代码:

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,⍪(+/÷≢)1Centigrade temp
AverageTemp

基于这个示例,它应该返回星期三和星期六。

英文:

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

答案1

得分: 2

首先,让我们找出最大平均温度是多少:

      /AverageTemp[;2]
31.66666667

现在我们想找出哪些行具有该值,作为一个掩码:

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

然后,我们可以使用该掩码来过滤日期名称:

      days /⍨ AverageTemp[;2] = /AverageTemp[;2]
┌─────────┬────────┐
WednesdaySaturday
└─────────┴────────┘
英文:

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]
┌─────────┬────────┐
WednesdaySaturday
└─────────┴────────┘

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

huangapple
  • 本文由 发表于 2023年6月2日 13:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定