dousha1831 2019-07-13 17:33
浏览 38
已采纳

即使我什么都没做,我的执行代码仍会返回不同的结果

I'm working through a kata about decoding Roman numerals into base 10 numbers and I'm running into a very strange problem. The problem I'm running into is that the output is not consistent and I have no idea why. I have the following code set up trying to beat the challenge (I know it's not perfect; that's not the problem):

package kata

import "strings"

var numeralsMap = map[string]int{
    "M": 1000,
    "D": 500,
    "C": 100,
    "L": 50,
    "X": 10,
    "V": 5,
    "I": 1,
  }

func Decode(roman string) int {
    sum := 0
    romanCpy := roman
for k := range numeralsMap { //works through romanCpy looking for matching numeralMap members
    for strings.Index(romanCpy, k) != -1 {
        index := strings.Index(romanCpy, k)
        if index == 0 { //if it is the first one in the string, simply add it to sum and remove it from romanCpy
            sum += numeralsMap[k]
            if len(romanCpy) > 1 { //this is necessary to prevent an infinite loop at the last numeral
                romanCpy = romanCpy[1:] 
            } else if len(romanCpy) <= 1 {
                romanCpy = "" //removes last one at the end
            }
        } else if index > 0 { //if it is present but not the first one, subtract all the ones before it from sum
            substr := romanCpy[:index]
            for i := 0; i < len(substr); i++ {
                sum -= numeralsMap[string(substr[i])]
            }
            if len(romanCpy) > 1 {
                romanCpy = romanCpy[index:] 
            }
        }
    }
}
return sum
}

And then I have some tests like this:

t.Run("MDCLXVI", func(t *testing.T) {
    got := Decode("MDCLXVI")
    want := 1666
    if got != want {
        t.Errorf("got %d; want %d", got, want)
    }
})

t.Run("IV", func(t *testing.T) {
    got := Decode("IV")
    want := 4
    if got != want {
        t.Errorf("got %d; want %d", got, want)
    }
})

Then when I run the tests, sometimes they pass, and then sometimes the same test will fail the next time. This is true both on my machine and when I try to run the tests on codewars. I'm not asking for help to solve the kata, I'm just not sure why the output keeps changing. Any help would be greatly appreciated. Thanks in advance!

EDIT: The different outputs do tend to follow a pattern. It seems to loop around every five times.

  • 写回答

1条回答 默认 最新

  • douweng7233 2019-07-13 21:29
    关注

    Thank you @mkopriva for answering my question! If anyone else comes along later, what I hadn't realized was that iteration order is not guaranteed for go maps, so a separate data structure was needed. mkopriva proved a working version here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝