// Package lsproduct is for calculating largest series product.
package lsproduct
import (
"fmt"
"log"
"strconv"
"unicode"
)
// LargestSeriesProduct return the largest spoduct in input for a given span or an error.
func LargestSeriesProduct(input string, span int) (int, error) {
if span == 0 {
return 1, nil
}
if span > len(input) || span < 0 {
return -1, fmt.Errorf("negative span %d, or longer than input %d", span, len(input))
}
product := 0
for i := range input {
if i > len(input)-span {
break
}
cand := 1
for j := i; j < i+span; j++ {
if !unicode.IsDigit(rune(input[j])) {
return -1, fmt.Errorf("input must be all digits")
}
tmp, err := strconv.Atoi(string(input[j]))
if err != nil {
log.Panicln(err)
}
cand *= tmp
}
if cand > product {
product = cand
}
}
return product, nil
}