Wednesday, May 31, 2023
HomeSoftware EngineeringHow you can Sum all of the Integers in a String in...

How you can Sum all of the Integers in a String in Golang


The problem

Implement a operate that calculates the sum of the integers inside a string. For instance, within the string "The30quick20brown10f0x1203jumps914ov3r1349the102l4zy canine", the sum of the integers is 3635.

Observe: solely optimistic integers shall be examined.

The answer in Golang

Choice 1:

package deal answer
import (
  "fmt"
  "regexp"
  "strconv"
)
func SumOfIntegersInString(strng string) int {
  re := regexp.MustCompile("[0-9]+")
  fmt.Println(re.FindAllString(strng, -1))
  sum := 0
  for _, i := vary re.FindAllString(strng, -1) {
    quantity, _ := strconv.Atoi(i)
    sum += quantity
  }
  return sum
}

Choice 2:

package deal answer
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
}

Choice 3:

package deal answer
import (
  "fmt"
  "strconv"
)
func SumOfIntegersInString(a string) int {
  n := ""
  s := []int{}
  set := 0
  r := 0
  for i, aR := vary a {
    if aR >= 48 && aR <= 58 {
      n += fmt.Dash(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 := vary s {
    r += sR
  }
  return r
}

Take a look at instances to validate our answer

package deal 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 := vary re.FindAllString(strng, -1) {
    val, _ := strconv.Atoi(match)
    sum += val
  }
  
  return sum
}
  
func StringWithCharset(size int, charset string) string {
  b := make([]byte, size)
  for i := vary b {
    b[i] = charset[seededRand.Intn(len(charset))]
  }
  return string(b)
}

sort Case struct {
  str string
  consequence int
}

var instances = []Case{
  {"12.4", 16},
  {"h3ll0w0rld", 3},
  {"2 + 3 = ", 5},
  {"Our firm made roughly 1 million in gross income final quarter.", 1},
  {"The Nice Despair lasted from 1929 to 1939.", 3868},
  {"Canine are our greatest pals.", 0},
  {"The30quick20brown10f0x1203jumps914ov3r1349the102l4zy canine", 3635},
}

var _ = Describe("SumOfIntegers functionallity", func() {
  It("ought to cowl primary instances", func() {
      for _, testCase := vary instances {
        Count on(SumOfIntegersInString(testCase.str)).To(Equal(testCase.consequence), "The sum of Integers in '%s' needs to be equal to %d", testCase.str, testCase.consequence)
      }
  })
  
  It("ought to cowl random instances", func() {
    for i:=0; i<100; i++ {
      testCase := Case{StringWithCharset(100, "-.ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), 0}
      testCase.consequence = SumOfIntegersInternalSolution(testCase.str)
      Count on(SumOfIntegersInString(testCase.str)).To(Equal(testCase.consequence), "The sum of Integers in '%s' needs to be equal to %d", testCase.str, testCase.consequence)
    }
  })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments