搜索哈希值

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

Searching a Hash

问题

我正在尝试完成这个Codewars挑战,但我对我到底哪里出错感到困惑。有人可以帮助我吗?

这个问题提供了一个“数据库”,用于翻译Welcome,并且说明了以下指令:

  • 想出一种将语言存储为数据库(例如一个对象)的方法。以下是列出的语言,因此你可以复制粘贴!
  • 编写一个名为'welcome'的函数,它接受一个参数'language'(始终是一个字符串),并返回一个问候语 - 如果你的数据库中有它。如果语言不在数据库中,或者出现无效输入,它应默认为英语。

我的尝试:

def greet(language)
  
  greeting = {  'english' => 'Welcome',
    'czech' => 'Vitejte',
    'danish' => 'Velkomst',
    'dutch' => 'Welkom',
    'estonian' => 'Tere tulemast',
    'finnish' => 'Tervetuloa',
    'flemish' => 'Welgekomen',
    'french' => 'Bienvenue',
    'german' => 'Willkommen',
    'irish' => 'Failte',
    'italian' => 'Benvenuto',
    'latvian' => 'Gaidits',
    'lithuanian' => 'Laukiamas',
    'polish' => 'Witamy',
    'spanish' => 'Bienvenido',
    'swedish' => 'Valkommen',
    'welsh' => 'Croeso'
  }

    greeting.key?(language) ? greeting.each { |k, v| return v if language == k } : 'IP_ADDRESS_INVALID';

end

在我的眼中,当我在IDE中运行我的代码时,它似乎按照要求运行,但我想我一定哪里出错了。

它告诉我:

期望的结果是:“Laukiamas”,但实际得到的是:“Welcome”。

但当我键入:

p greet("lithuanian")

我得到 Laukiamas

英文:

I'm trying to complete this Codewars Challenge and I'm confused as to where I'm going wrong. Could someone please give me a hand?

The question provides a "database" of translations for Welcome, and the instructions say:

> - Think of a way to store the languages as a database (eg an object). The languages are listed below so you can copy and paste!
> - Write a 'welcome' function that takes a parameter 'language' (always a string), and returns a greeting - if you have it in your database. It should default to English if the language is not in the database, or in the event of an invalid input.

My attempt:

def greet(language)
  
  greeting = {  'english'=>'Welcome',
    'czech'=>'Vitejte',
    'danish'=>'Velkomst',
    'dutch'=>'Welkom',
    'estonian'=>'Tere tulemast',
    'finnish'=>'Tervetuloa',
    'flemish'=>'Welgekomen',
    'french'=>'Bienvenue',
    'german'=>'Willkommen',
    'irish'=>'Failte',
    'italian'=>'Benvenuto',
    'latvian'=>'Gaidits',
    'lithuanian'=>'Laukiamas',
    'polish'=>'Witamy',
    'spanish'=>'Bienvenido',
    'swedish'=>'Valkommen',
    'welsh'=>'Croeso'
  }

    greeting.key?(language) ? greeting.each { |k, v| return v if language == k } : 'IP_ADDRESS_INVALID'

end

To my eyes when I run my code through the IDE it seems to be working as per request but I guess I must be wrong somehow.

It's telling me it :

> Expected: "Laukiamas", instead got: "Welcome"

But when I type:

 p greet("lithuanian")

I get Laukiamas.

答案1

得分: 1

你可以为你的 greeting 哈希提供一个默认值。这很简单,就像这样:

greeting.default = "欢迎"

这个增强的哈希为你完成所有工作。只需查找键;当它不存在时,你将获得 "欢迎"。

英文:

You can provide you greeting hash with a default value. It is as simple as

greeting.default = "Welcome"

This enhanced hash does all the work for you. Just look up the key; when it is not there you'll get "Welcome".

答案2

得分: 0

以下是翻译好的内容:

前言

首先,请不要发布链接到练习或作业问题。在您的原始问题中引用它们,以避免链接失效或为试图帮助您的人额外创建工作。

理解链接问题定义的问题

其次,您误解了核心问题。要求基本上是如果键存在于散列中,则返回给定语言键的散列值。如果不存在,则返回'english'键的值。在练习中隐含了理解各种类型的不当输入,这些输入会导致找不到匹配的键;下面的解决方案解决了大多数这些问题,即使您的Ruby启用了冻结字符串,它也可以正常工作。

一个有效的解决方案

有很多方法可以做到这一点,但以下是一个处理无效键、nil作为语言参数并将大写字母视为潜在问题的简单示例。

DEFAULT_LANG = 'english'
TRANSLATIONS = {
    'english'    => 'Welcome',
    'czech'      => 'Vitejte',
    'danish'     => 'Velkomst',
    'dutch'      => 'Welkom',
    'estonian'   => 'Tere tulemast',
    'finnish'    => 'Tervetuloa',
    'flemish'    => 'Welgekomen',
    'french'     => 'Bienvenue',
    'german'     => 'Willkommen',
    'irish'      => 'Failte',
    'italian'    => 'Benvenuto',
    'latvian'    => 'Gaidits',
    'lithuanian' => 'Laukiamas',
    'polish'     => 'Witamy',
    'spanish'    => 'Bienvenido',
    'swedish'    => 'Valkommen',
    'welsh'      => 'Croeso'
}

# 返回语言参数的“Welcome”翻译。
#
# @param language [String, #to_s] 任何可以强制转换为字符串的对象,因此可以强制转换为
#   String#downcase
# @return [String] “Welcome”的翻译或字符串字面值+Welcome+(如果找不到翻译)
def greet language
  language = language.to_s.downcase
  TRANSLATIONS.fetch language, TRANSLATIONS[DEFAULT_LANG]
end

# 以下示例数组中的所有内容(除了“Spanish”)应返回+english+的散列值。
['Spanish', 'Español', 123, nil].map { greet(_1) }

这将正确返回:

#=> ["Bienvenido", "Welcome", "Welcome", "Welcome"]

因为只有Spanish(小写后)会与当前在TRANSLATIONS散列中定义的任何键匹配。其余的将使用练习定义的默认值。

测试结果

由于链接问题中包含了一些RSpec测试:

describe "Welcome! Translation" do
  it "should translate input" do
    Test.assert_equals(greet('english'), 'Welcome', "这次没有成功,继续尝试!");
    Test.assert_equals(greet('dutch'), 'Welkom', "这次没有成功,继续尝试!");
    Test.assert_equals(greet('IP_ADDRESS_INVALID'), 'Welcome', "这次没有成功,继续尝试!")
  end
end

提供的代码不仅通过提供的测试,还通过了一些其他单元测试中未定义的边缘情况。当针对定义的测试运行时,上面的代码会干净地通过:

搜索哈希值

如果这是作业,那么您可能希望创建额外的测试来覆盖所有各种边缘情况。您还可以选择重构为不太惯用的代码,以获得更多解释性变量、更明确的中间转换或更明确的键处理。好代码的目标是易于阅读,因此在代码和测试中尽可能明确,以便使调试更容易。

英文:

Preface

First of all, please don't post links to exercises or homework questions. Quote them in your original question to avoid link rot or additional create work for people trying to help you out.

Understanding the Problem Defined by the Linked Question

Secondly, you're misunderstanding the core question. The requirement is basically to return the Hash value for a given language key if the key exists in the Hash. If it doesn't, then return the value of the 'english' key instead. Implicit in the exercise is to understand the various types of improper inputs that would fail to find a matching key; the solution below addresses most of them, and will work even if your Ruby has frozen strings enabled.

A Working Solution

There are lots of ways to do this, but here's a simple example that will handle invalid keys, nil as a language argument, and abstract away capitalization as a potential issue.

DEFAULT_LANG = 'english'
TRANSLATIONS = { 
    'english'    => 'Welcome',
    'czech'      => 'Vitejte',
    'danish'     => 'Velkomst',
    'dutch'      => 'Welkom',
    'estonian'   => 'Tere tulemast',
    'finnish'    => 'Tervetuloa',
    'flemish'    => 'Welgekomen',
    'french'     => 'Bienvenue',
    'german'     => 'Willkommen',
    'irish'      => 'Failte',
    'italian'    => 'Benvenuto',
    'latvian'    => 'Gaidits',
    'lithuanian' => 'Laukiamas',
    'polish'     => 'Witamy',
    'spanish'    => 'Bienvenido',
    'swedish'    => 'Valkommen',
    'welsh'      => 'Croeso'
}

# Return a translation of "Welcome" into the language
# passed as an argument.
#
# @param language [String, #to_s] any object that can
#   be coerced into a String, and therefore to 
#   String#downcase
# @return [String] a translation of "Welcome" or the
#   string-literal +Welcome+ if no translation found
def greet language
  language = language.to_s.downcase
  TRANSLATIONS.fetch language, TRANSLATIONS[DEFAULT_LANG]
end

# Everything in the following Array of examples except
# +Spanish+ should return the Hash value for +english+.                                                                                               
['Spanish', 'Español', 123, nil].map { greet(_1) }

This will correctly return:

> #=> ["Bienvenido", "Welcome", "Welcome", "Welcome"]

because only Spanish (when lower-cased) will match any of the keys currently defined in the TRANSLATIONS Hash. All the rest will use the default value defined for the exercise.

Test Results

Since there are some RSpec tests included with the linked question:

describe "Welcome! Translation" do
  it "should translate input" do
    Test.assert_equals(greet('english'), 'Welcome', "It didn't work out this time, keep trying!");
    Test.assert_equals(greet('dutch'), 'Welkom', "It didn't work out this time, keep trying!");
    Test.assert_equals(greet('IP_ADDRESS_INVALID'), 'Welcome', "It didn't work out this time, keep trying!")
  end
end

The code provided not only passes the provided tests, but it also passes a number of other edge cases not defined in the unit tests. When run against the defined tests, the code above passes cleanly:

搜索哈希值

If this is homework, then you might want to create additional tests to cover all the various edge cases. You might also choose to refactor to less idiomatic code if you want more explanatory variables, more explicit intermediate conversions, or more explicit key handling. The point of good code is to be readable, so be as explicit in your code and as thorough in your tests as you need to be in order to make debugging easier.

huangapple
  • 本文由 发表于 2023年2月14日 22:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449386.html
匿名

发表评论

匿名网友

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

确定