我正在学习编码,其中一个任务是返回键是返回喜欢同一个电视节目的人的名字。如何检查红宝石哈希中的匹配键?
我设法得到它的工作,并通过TDD,但我想知道如果我采取了“很长的路要走围绕”那也许有一个简单的解决方案?
这里的设置和测试:
class TestFriends < MiniTest::Test
def setup
@person1 = {
name: "Rick",
age: 12,
monies: 1,
friends: ["Jay","Keith","Dave", "Val"],
favourites: {
tv_show: "Friends",
things_to_eat: ["charcuterie"]
}
}
@person2 = {
name: "Jay",
age: 15,
monies: 2,
friends: ["Keith"],
favourites: {
tv_show: "Friends",
things_to_eat: ["soup","bread"]
}
}
@person3 = {
name: "Val",
age: 18,
monies: 20,
friends: ["Rick", "Jay"],
favourites: {
tv_show: "Pokemon",
things_to_eat: ["ratatouille", "stew"]
}
}
@people = [@person1, @person2, @person3]
end
def test_shared_tv_shows
expected = ["Rick", "Jay"]
actual = tv_show(@people)
assert_equal(expected, actual)
end
end
这里是我找到了解决办法:
def tv_show(people_list)
tv_friends = {}
for person in people_list
if tv_friends.key?(person[:favourites][:tv_show]) == false
tv_friends[person[:favourites][:tv_show]] = [person[:name]]
else
tv_friends[person[:favourites][:tv_show]] << person[:name]
end
end
for array in tv_friends.values()
if array.length() > 1
return array
end
end
end
它通过,但有这样做的更好的办法?
哇感谢的人,这是我们班迄今看到最整洁的解决方案! –
我敢肯定,有更好的解决方案,但如果这可以帮助你,我很高兴。 –