英文:
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,⍪(+/÷≢)⍤1⊢Centigrade 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
答案1
得分: 2
首先,让我们找出最大平均温度是多少:
⌈/AverageTemp[;2]
31.66666667
现在我们想找出哪些行具有该值,作为一个掩码:
AverageTemp[;2] = ⌈/AverageTemp[;2]
0 0 1 0 0 1 0
然后,我们可以使用该掩码来过滤日期名称:
days /⍨ AverageTemp[;2] = ⌈/AverageTemp[;2]
┌─────────┬────────┐
│Wednesday│Saturday│
└─────────┴────────┘
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论