小时转换的Go算法无法正确转换。

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

Go algorithm for hour convertion not converting correctly

问题

我遇到了一个HackerRank的挑战,需要编写一个函数,将给定格式的12小时制时间字符串转换为24小时制。

我使用下面的代码完成了这个任务,但是你可能注意到,对于下午09点这个特殊情况,我需要单独处理。每当我输入以09开头的小时数(只有这一个特殊情况),它会将09转换为12。所以我不得不创建一个特殊情况来处理它,虽然这样可以工作,但我想知道为什么会发生这种情况。你知道可能的问题是什么吗?

package main

import (
    "fmt"
    "strconv"
    "strings"
)

type Conversion struct {
    conversion string
}

func timeConversion(s string) string {
    var conversion Conversion
    firstValue := fmt.Sprintf("%s%s", string(s[0]), string(s[1]))
    secondValue := fmt.Sprintf("%s%s", string(s[3]), string(s[4]))
    firstValueNumber, _ := strconv.ParseInt(firstValue, 0, 16)

    fmt.Print()

    if strings.Contains(s, "A") {
        if firstValue == "12" {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "00", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        } else {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", firstValue, secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        }
    } else if strings.Contains(s, "P") {
        if firstValue == "12" {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "12", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        } else if firstValue == "09" {
            conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "21", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        } else {
            conversion.conversion = fmt.Sprintf("%d:%s:%s%s", firstValueNumber+12, secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
        }
    }

    return conversion.conversion
}

func main() {
    fmt.Print(timeConversion("09:08:23PM"))
}
英文:

I've come across a HackerRank challenge where I should build a function which takes a string in a given format of the time in the 12-hour format to the 24-hour format.

I managed to do that with the code below, but as you can notice, there's a special case for the 09 pm. Whenever I'd type in an hour starting with 09 (only this damn one) it'd convert the 09 to 12. So I had to actually create a specific case to treat that, which worked, but I'd like to understand why it happened. Do you know what the problem may be?

package main
import (
"fmt"
"strconv"
"strings"
)
type Conversion struct {
conversion string
}
func timeConversion(s string) string {
var conversion Conversion
firstValue := fmt.Sprintf("%s%s", string(s[0]), string(s[1]))
secondValue := fmt.Sprintf("%s%s", string(s[3]), string(s[4]))
firstValueNumber, _ := strconv.ParseInt(firstValue, 0, 16)
fmt.Print()
if strings.Contains(s, "A") {
if firstValue == "12" {
conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "00", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
} else {
conversion.conversion = fmt.Sprintf("%s:%s:%s%s", firstValue, secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
}
} else if strings.Contains(s, "P") {
if firstValue == "12" {
conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "12", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
} else if firstValue == "09" {
conversion.conversion = fmt.Sprintf("%s:%s:%s%s", "21", secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
} else {
conversion.conversion = fmt.Sprintf("%d:%s:%s%s", firstValueNumber+12, secondValue, string(s[len(s)-4]), string(s[len(s)-3]))
}
}
return conversion.conversion
}
func main() {
fmt.Print(timeConversion("09:08:23PM"))
}

答案1

得分: 2

如果在将字符串转换为整数的过程中捕获到错误,你会注意到对于大于7的值,转换会失败

firstValueNumber, err := strconv.ParseInt(firstValue, 0, 64)
if err != nil {
     fmt.Printf("%s", err.Error())
}
// "08"导致无效语法firstValueNumber被打印

这是因为你将0作为基数传递给parseInt,告诉它从字符串的第一个字符推断基数。以0开头意味着八进制基数,因此大于7的数字将是无效的。改为传递10,你就不需要特殊处理了。

英文:

If you catch the error during your string to int conversion, you'll notice that for values >7, the conversion fails

firstValueNumber, err := strconv.ParseInt(firstValue, 0, 64)
if err != nil {
fmt.Printf("%s", err.Error())
}
// "08" results in invalid syntaxfirstValueNumber being printed

It happens because you pass 0 as the base, telling parseInt to infer the base from the first character of the string. A leading '0' implies octal base, and so digits greater than 7 would be invalid. Pass 10 instead and you won't need special handling.

huangapple
  • 本文由 发表于 2023年3月31日 04:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892938.html
匿名

发表评论

匿名网友

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

确定