Skip to content

Instantly share code, notes, and snippets.

@monkrus
Last active November 23, 2022 18:23
Show Gist options
  • Save monkrus/ad53c3e48dc45192bc562c3c89ea927d to your computer and use it in GitHub Desktop.
Save monkrus/ad53c3e48dc45192bc562c3c89ea927d to your computer and use it in GitHub Desktop.
Extract string values
func getProduct(digits string) (product int64, e error) {
product = 1
for _, digit := range digits {
d, err := strconv.Atoi(string(digit))
if err != nil {
return -1, fmt.Errorf("cannot convert %v to int", digit)
}
product *= int64(d)
}
return product, nil
}
for i := 0; i <= len(digits)-span; i++ {
product, err := getProduct(digits[i : i+span])
if err != nil {
return -1, err
}
if product > largestProduct {
largestProduct = product
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment