Monday, February 20, 2023
HomeSoftware EngineeringThe way to Sum all of the Integers in a String in...

The way to 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.

Be aware: solely constructive integers might be examined.

The answer in Golang

Possibility 1:

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

Possibility 2:

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

Possibility 3:

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

Check circumstances to validate our resolution

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)
}

kind Case struct {
  str string
  end result int
}

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

var _ = Describe("SumOfIntegers functionallity", func() {
  It("ought to cowl fundamental circumstances", func() {
      for _, testCase := vary circumstances {
        Anticipate(SumOfIntegersInString(testCase.str)).To(Equal(testCase.end result), "The sum of Integers in '%s' must be equal to %d", testCase.str, testCase.end result)
      }
  })
  
  It("ought to cowl random circumstances", func() {
    for i:=0; i<100; i++ {
      testCase := Case{StringWithCharset(100, "-.ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), 0}
      testCase.end result = SumOfIntegersInternalSolution(testCase.str)
      Anticipate(SumOfIntegersInString(testCase.str)).To(Equal(testCase.end result), "The sum of Integers in '%s' must be equal to %d", testCase.str, testCase.end result)
    }
  })
})

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments