The challenge
Write a function partlist
that gives all the ways to divide an array of at least two elements into two non-empty parts.
- Each two non empty parts will be in a pair
- Each part will be in a string
- Elements of a pair must be in the same order as in the original 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 solution in Golang
Option 1:
package solution
import ("strings"; "fmt")
func PartList(l []string) (r string) {
for i := 1; i < len(l); i++ {
a := strings.Join(l[0:i], " ")
b := strings.Join(l[i:], " ")
r += fmt.Sprintf("(%s, %s)", a, b)
}
return
}
Option 2:
package solution
import . "strings"
func PartList(arr []string) string {
var sb Builder
n := len(arr)
for x := 1; x < n; x++ {
sb.WriteString("(" + Join(arr[:x], " ") + ", " + Join(arr[x:], " ") + ")")
}
return sb.String()
}
Option 3:
package solution
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.Join(tmp, " "))
}
Test cases to validate our solution
package 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)
Expect(ans).To(Equal(exp))
}
func randStringBytes(n int) string {
const letterBytes = "abcde1fghij2klmno3pqrs4tuvwxyz5ABCDEF6GHIJKL7MNOPQR8STUV9WXYZ"
b := make([]byte, n)
for i := range 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.Join(arr[0:i], " ") + ", " + strings.Join(arr[i:len(arr)], " ") + ")"
}
return res
}
func randomtest() {
rand.Seed(time.Now().UTC().UnixNano())
for i := 0; i < 100; i++ {
k := random(8, 12)
s := doStringArray(k)
sol := PartListPVX(s)
dotest(s, sol)
}
}
var _ = Describe("Tests PartList", func() {
It("should handle basic cases", func() {
dotest([]string{"I", "wish", "I", "hadn't", "come"},
"(I, wish I hadn't come)(I wish, I hadn't come)(I wish I, hadn't come)(I wish 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("should handle random cases", func() {
randomtest()
})
})