在一个字符串数组中找到最常见的元素。

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

Find the most common element in a string array GO

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我对Go语言还比较新手,我在一个数组(Windrichting)中找不到最常见的字符串。应该是"N",但我的输出却是"W"(它总是给我最后一个字符串Windrichting[6])。有人可以帮忙吗?

这是我的代码:

  1. package main
  2. import "fmt"
  3. func main() {
  4. Windrichting := [7]string{"N", "N", "N", "N", "O", "Z", "W"}
  5. windEL, winner := Mostcommon(Windrichting)
  6. fmt.Printf("Mostcommon windrichting: %s\n", windEL)
  7. fmt.Printf("Komt %d x voor\n", winner)
  8. }
  9. func Mostcommon(Windrichting [7]string) (windEL string, winner int) {
  10. var N int
  11. var O int
  12. var Z int
  13. var W int
  14. Windrichtingbase := [4]string{"N", "O", "Z", "W"}
  15. for _, v := range Windrichting {
  16. switch v {
  17. case Windrichtingbase[0]:
  18. N++
  19. if N > winner {
  20. winner = N
  21. windEL = "Noord"
  22. }
  23. case Windrichtingbase[1]:
  24. O++
  25. if O > winner {
  26. winner = O
  27. windEL = "Oost"
  28. }
  29. case Windrichtingbase[2]:
  30. Z++
  31. if Z > winner {
  32. winner = Z
  33. windEL = "Zuid"
  34. }
  35. case Windrichtingbase[3]:
  36. W++
  37. if W > winner {
  38. winner = W
  39. windEL = "West"
  40. }
  41. }
  42. }
  43. return windEL, winner
  44. }

输出截图

英文:

i'm fairly new to golang and im having trouble finding the most common string in an array (Windrichting). it should be N but my output gives me W (it always gives me the last string so Windrichting[6]. Can someone help?

this is my code:

package main

  1. import "fmt"
  2. func main() {
  3. Windrichting := [7]string{"N", "N", "N", "N", "O", "Z", "W"}
  4. windEL, winner := Mostcommon(Windrichting)
  5. fmt.Printf("Mostcommon windrichting: %s\n", windEL)
  6. fmt.Printf("Komt %d x voor\n", winner)
  7. }
  8. func Mostcommon(Windrichting [7]string) (windEL string, winner int) {
  9. var N int
  10. var O int
  11. var Z int
  12. var W int
  13. Windrichtingbase := [4]string{"N", "O", "Z", "W"}
  14. for _, v := range Windrichting {
  15. switch v {
  16. case Windrichtingbase[0]:
  17. N++
  18. if N > winner {
  19. N = winner
  20. windEL = "Noord"
  21. }
  22. case Windrichtingbase[1]:
  23. O++
  24. if O > winner {
  25. O = winner
  26. windEL = "Oost"
  27. }
  28. case Windrichtingbase[2]:
  29. Z++
  30. if Z > winner {
  31. Z = winner
  32. windEL = "Zuid"
  33. }
  34. case Windrichtingbase[3]:
  35. W++
  36. if W > winner {
  37. W = winner
  38. windEL = "West"
  39. }
  40. }
  41. }
  42. return windEL, winner
  43. }

output

答案1

得分: 1

赢家始终为0,你从未更新它。然后,在增加方向变量(N、O、Z和W)之后,你立即用存储在赢家中的零值覆盖它们。你需要改变赋值的顺序。

就像这个变化一样:https://go.dev/play/p/VaJgZcijFdh

还要注意,Go中的大写变量表示它们是可导出的。

英文:

winner is always 0 and you never update it. then after incrementing your direction variables (N, O, Z and W), you immediately overwrite them with the zero value stored in winner. You need to reverse the order of the assignment.

Like in this change: https://go.dev/play/p/VaJgZcijFdh

Note also, that capitalized variables in Go mean they're exported

答案2

得分: 0

这是一个替代实现。它使用直方图来收集单词出现的次数。然后通过直方图遍历来找到最常见的单词。没有硬编码。

https://go.dev/play/p/wTFvNaPRP6B

英文:

Here's an alternate implementation. It uses a histogram to collect the number of times a word appears. It then steps through the histogram to find the most common word. Nothing is hard-coded.

https://go.dev/play/p/wTFvNaPRP6B

huangapple
  • 本文由 发表于 2021年12月23日 07:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/70456107.html
匿名

发表评论

匿名网友

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

确定