如何检查红宝石哈希中的匹配键?
问题描述:
我正在学习编码,其中一个任务是返回键是返回喜欢同一个电视节目的人的名字。如何检查红宝石哈希中的匹配键?
我设法得到它的工作,并通过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
它通过,但有这样做的更好的办法?
答
我想你可以用Array#each
替换那些for
循环。但是,在你的情况,因为你正在创建与people_list
值的哈希值,那么你可以使用Enumerable#each_with_object
分配一个新的Hash作为对象的说法,这样你从people_list
,也是一个新的“空自己person
哈希“哈希按照你的需要开始填充。
若要检查您的内部散列是否有一个值为person[:favourites][:tv_show]
的密钥,您可以检查它的值是否为布尔值,则可以跳过与false的比较,该值将通过if语句评估为false或true 。
您可以创建变量tv_show
和name
减少一点点的代码,然后在你的tv_friends
哈希它的值之间选择的长度一大于1。这会给你内部的数组你可以从这个数组中获得第一个元素first
(或[0]
)。
def tv_show(people_list)
tv_friends = people_list.each_with_object(Hash.new({})) do |person, hash|
tv_show = person[:favourites][:tv_show]
name = person[:name]
hash.key?(tv_show) ? hash[tv_show] << name : hash[tv_show] = [name]
end
tv_friends.values.select { |value| value.length > 1 }.first
end
也可以在方法调用没有参数时省略括号。
哇感谢的人,这是我们班迄今看到最整洁的解决方案! –
我敢肯定,有更好的解决方案,但如果这可以帮助你,我很高兴。 –