寻找符合特定要求的字符串

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

Finding string that matches specific requirements

问题

有一个函数应该返回true

func accessible(agent string) bool {
  a := strings.Split(agent, " ")
  if len(a) != 3 { return false }
  b := a[0]
  c := a[1]
  d := a[2]
  x := strings.EqualFold(b, c)
  y := b != strings.ToLower(c)
  z := strings.Index(d, b+c) == 1 && len(d) == 5
  return x && y && z
}

然而,我无法找出哪个string输入符合这些要求。我是否漏掉了什么?

PS:这是来自gocode.io的任务#3。

英文:

There is a function that should return true:

func accessible(agent string) bool {
  a := strings.Split(agent, " ")
  if len(a) != 3 { return false }
  b := a[0]
  c := a[1]
  d := a[2]
  x := strings.EqualFold(b, c)
  y := b != strings.ToLower(c)
  z := strings.Index(d, b+c) == 1 && len(d) == 5
  return x && y && z
}

However I can't figure out which string input will match these requirements. Am I missing something?

PS: This is task #3 from gocode.io

答案1

得分: 2

agent必须是由3个“单词”组成的,3个部分由空格分隔:

a := strings.Split(agent, " ")
if len(a) != 3 { return false }

第一个和第二个单词必须不区分大小写匹配:

x := strings.EqualFold(b, c)

但不区分大小写:

y := b != strings.ToLower(c)

第三个单词必须包含前两个单词的连接:

z := strings.Index(d, b+c) == 1 && len(d) == 5

从索引1开始(前面添加任何字符),并且必须包含5个字符(5个字节)(后面添加以确保有5个字符/字节)。

示例:

fmt.Println(accessible("A a _Aa__"))

输出:

true
英文:

agent must be 3 "words", 3 parts separated by spaces:

a := strings.Split(agent, " ")
if len(a) != 3 { return false }

1st and 2nd words must match case insensitive:

x := strings.EqualFold(b, c)

But not case sensitive:

y := b != strings.ToLower(c)

And 3rd word must contain the first 2 concatenated:

z := strings.Index(d, b+c) == 1 && len(d) == 5

Starting at index 1 (prepend with any character) and must contain 5 chars (5 bytes) (postpend to have 5 chars/bytes).

Example:

fmt.Println(accessible("A a _Aa__"))

Prints:

true

huangapple
  • 本文由 发表于 2015年5月20日 19:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/30348420.html
匿名

发表评论

匿名网友

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

确定