Thursday, September 21, 2023
HomeSoftware EngineeringAll of the Methods to Divide an Array in Two in Golang

All of the Methods to Divide an Array in Two in Golang


The problem

Write a operate partlist that provides all of the methods to divide an array of a minimum of two components into two non-empty elements.

  • Every two non empty elements can be in a pair
  • Every half can be in a string
  • Parts of a pair have to be in the identical order as within the authentic array.

Examples:

a = ["az", "toto", "picaro", "zone", "kiwi"] -->
[("az", "toto picaro zone kiwi"), ("az toto", "picaro zone kiwi"), ("az toto picaro", "zone kiwi"), ("az toto picaro zone", "kiwi")]

The answer in Golang

Possibility 1:

bundle resolution
import ("strings"; "fmt")
func PartList(l []string) (r string) {
  for i := 1; i < len(l); i++ {
    a := strings.Be a part of(l[0:i], " ")
    b := strings.Be a part of(l[i:], " ")
    r += fmt.Sprintf("(%s, %s)", a, b)
  }
  return
}

Possibility 2:

bundle resolution
import . "strings"
func PartList(arr []string) string {
  var sb Builder
  n := len(arr)
  for x := 1; x < n; x++ {
    sb.WriteString("(" + Be a part of(arr[:x], " ") + ", " + Be a part of(arr[x:], " ") + ")")
  }
  return sb.String()
}

Possibility 3:

bundle resolution
import (
"fmt"
"strings"
)
func PartList(arr []string) string {
  str := ""
  for i := 0 ; i < len(arr) - 1 ; i++ {
    str += appended(arr, i)
  }
  fmt.Println(str)
  return str
}
func appended(strs []string, index int) string {
  tmp := make([]string, len(strs))
  copy(tmp, strs)
  tmp[index] += ","
  return fmt.Sprintf("(%s)", strings.Be a part of(tmp, " "))
}

Take a look at circumstances to validate our resolution

bundle solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
  "strings"
  "math/rand"
  "time"
)

func dotest(arr []string, exp string) {
    var ans = PartList(arr)
    Count on(ans).To(Equal(exp))
}

func randStringBytes(n int) string {
    const letterBytes = "abcde1fghij2klmno3pqrs4tuvwxyz5ABCDEF6GHIJKL7MNOPQR8STUV9WXYZ"
    b := make([]byte, n)
    for i := vary b {
        b[i] = letterBytes[rand.Intn(len(letterBytes))]
    }
    return string(b)
}
func random(min, max int) int {
    return rand.Intn(max - min) + min
}
func doStringArray(n int) []string {
    var res []string
    for i := 0; i < n; i++ {
        res = append(res, randStringBytes(random(5, 15)))
    }
    return res
}
func PartListPVX(arr []string) string {
    res := ""
    for i := 1; i <= len(arr) - 1; i++ {
        res += "(" + strings.Be a part of(arr[0:i], " ") + ", " + strings.Be a part of(arr[i:len(arr)], " ") + ")"
    }
    return res
}

func randomtest() {
    rand.Seed(time.Now().UTC().UnixNano())
    for i := 0; i < 100; i++ {
        ok := random(8, 12) 
        s := doStringArray(ok)
        sol := PartListPVX(s)
        dotest(s, sol)
    }
}

var _ = Describe("Checks PartList", func() {

    It("ought to deal with fundamental circumstances", func() {
        dotest([]string{"I", "want", "I", "hadn't", "come"},
            "(I, want I hadn't come)(I want, I hadn't come)(I want I, hadn't come)(I want I hadn't, come)")
        dotest([]string{"cdIw", "tzIy", "xDu", "rThG"}, 
            "(cdIw, tzIy xDu rThG)(cdIw tzIy, xDu rThG)(cdIw tzIy xDu, rThG)")
        dotest([]string{"vJQ", "anj", "mQDq", "sOZ"}, 
            "(vJQ, anj mQDq sOZ)(vJQ anj, mQDq sOZ)(vJQ anj mQDq, sOZ)")
        dotest([]string{"mkC", "WoiP", "pCHh", "mkv"}, "(mkC, WoiP pCHh mkv)(mkC WoiP, pCHh mkv)(mkC WoiP pCHh, mkv)")
        dotest([]string{"vHW", "bPq", "pme", "jJr", "HGHV"}, 
            "(vHW, bPq pme jJr HGHV)(vHW bPq, pme jJr HGHV)(vHW bPq pme, jJr HGHV)(vHW bPq pme jJr, HGHV)")
        dotest([]string{"YZd", "ptUD", "IXr"}, "(YZd, ptUD IXr)(YZd ptUD, IXr)")

        dotest([]string{"dOXj", "nMlK", "QGT", "LSt", "BHNR"}, 
            "(dOXj, nMlK QGT LSt BHNR)(dOXj nMlK, QGT LSt BHNR)(dOXj nMlK QGT, LSt BHNR)(dOXj nMlK QGT LSt, BHNR)")
    })
    It("ought to deal with random circumstances", func() {
        randomtest()
    })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments