【发布时间】:2020-12-22 17:45:59
【问题描述】:
我刚开始学习 Ruby,所以请多多包涵。我正在处理一个 7 kyu Ruby 编码挑战,我的任务是找出公共汽车上剩下多少人(第一个值代表人们在,第二个值,人们离开)请查看代码中的 cmets 以获取更多详细信息。
下面是一个测试示例:
([[10, 0], [3, 5], [5, 8]]), # => should return 5"
这是我目前的解决方案:
def number(bus_stops)
bus_stops.each{ | on, off | on[0] -= off[1] }
end
bus_stops
# loop through the array
# for the first array in the nested array subtract second value from first
# add the sum of last nested array to first value of second array and repeat
# subtract value of last element in nested array and repeat
我该如何处理?你有什么推荐的资源吗?
【问题讨论】:
-
当你给出一个例子时,为所有输入分配一个变量是有帮助的(例如,
arr = [[10, 0], [3, 5], [5, 8]])。这样,读者可以在没有定义它们的情况下引用答案和 cmets 中的变量(例如,arr)。此外,输入都应该是有效的 Ruby 对象。你的无效,因为逗号,括号是多余的。应始终显示预期的结果,您已在此处完成。 -
谢谢,下次再做。