The challenge
Implement a function that calculates the sum of the integers inside a string. For example, in the string "The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog"
, the sum of the integers is 3635
.
Note: only positive integers will be tested.
The solution in Golang
Option 1:
package solution
import (
"fmt"
"regexp"
"strconv"
)
func SumOfIntegersInString(strng string) int {
re := regexp.MustCompile("[0-9]+")
fmt.Println(re.FindAllString(strng, -1))
sum := 0
for _, i := range re.FindAllString(strng, -1) {
number, _ := strconv.Atoi(i)
sum += number
}
return sum
}
Option 2:
package solution
import (
"strconv"
)
func SumOfIntegersInString(strng string) int {
var sum, cnt, num int
var tmpstr string
for pos := 0; pos < len(strng); pos++ {
if strng[pos] >= '0' && strng[pos] <= '9' {
for i := pos; i < len(strng) && strng[i] >= '0' && strng[i] <= '9'; i++ {
cnt++
}
tmpstr = strng[pos : pos+cnt]
num, _ = strconv.Atoi(tmpstr)
sum += num
pos += cnt
cnt = 0
}
}
return sum
}
Option 3:
package solution
import (
"fmt"
"strconv"
)
func SumOfIntegersInString(a string) int {
n := ""
s := []int{}
set := 0
r := 0
for i, aR := range a {
if aR >= 48 && aR <= 58 {
n += fmt.Sprint(int(aR - 48))
set = 1
} else if set == 1 {
w, _ := strconv.Atoi(n)
fmt.Println(w)
s = append(s, w)
n = ""
set = 0
}
if i == len(a)-1 {
w, _ := strconv.Atoi(n)
fmt.Println(w)
s = append(s, w)
}
}
for _, sR := range s {
r += sR
}
return r
}
Test cases to validate our solution
package solution_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"math/rand"
"time"
"strconv"
"regexp"
)
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func SumOfIntegersInternalSolution(strng string) int {
var re = regexp.MustCompile(`(?m)(\d+)`)
sum := 0
for _, match := range re.FindAllString(strng, -1) {
val, _ := strconv.Atoi(match)
sum += val
}
return sum
}
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
type Case struct {
str string
result int
}
var cases = []Case{
{"12.4", 16},
{"h3ll0w0rld", 3},
{"2 + 3 = ", 5},
{"Our company made approximately 1 million in gross revenue last quarter.", 1},
{"The Great Depression lasted from 1929 to 1939.", 3868},
{"Dogs are our best friends.", 0},
{"The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog", 3635},
}
var _ = Describe("SumOfIntegers functionallity", func() {
It("should cover basic cases", func() {
for _, testCase := range cases {
Expect(SumOfIntegersInString(testCase.str)).To(Equal(testCase.result), "The sum of Integers in '%s' should be equal to %d", testCase.str, testCase.result)
}
})
It("should cover random cases", func() {
for i:=0; i<100; i++ {
testCase := Case{StringWithCharset(100, "-.ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), 0}
testCase.result = SumOfIntegersInternalSolution(testCase.str)
Expect(SumOfIntegersInString(testCase.str)).To(Equal(testCase.result), "The sum of Integers in '%s' should be equal to %d", testCase.str, testCase.result)
}
})
})