# How to Reverse or Rotate in Golang

## The challenge

The input is a string `str`

of digits. Cut the string into chunks (a chunk here is a substring of the initial string) of size `sz`

(ignore the last chunk if its size is less than `sz`

).

If a chunk represents an integer such as the **sum of the cubes of its digits is divisible by 2**, reverse that chunk; otherwise rotate it to the left by one position. Put together these modified chunks and return the result as a string.

If

`sz`

is`<= 0`

or if`str`

is`empty`

return “”`sz`

is greater`(>)`

than the length of`str`

it is impossible to take a chunk of size`sz`

hence return “”.

```
Examples:
revrot("123456987654", 6) --> "234561876549"
revrot("123456987653", 6) --> "234561356789"
revrot("66443875", 4) --> "44668753"
revrot("66443875", 8) --> "64438756"
revrot("664438769", 8) --> "67834466"
revrot("123456779", 8) --> "23456771"
revrot("", 8) --> ""
revrot("123456779", 0) --> ""
revrot("563000655734469485", 4) --> "0365065073456944"
```

```
Example of a string rotated to the left by one position:
s = "123456" gives "234561".
```

## The solution in Golang

```
package solution
func cubeSum(str []rune) int {
sum := 0
for _, dr := range str {
d := int(dr) - 48
sum += d * d * d
}
return sum
}
func reverse(s []rune) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func shift(s []rune) {
first := s[0]
for i := range s[1:] {
s[i] = s[i+1]
}
s[len(s)-1] = first
}
func Revrot(s string, n int) string {
if n <= 0 || n > len(s) {
return ""
}
sr := []rune(s)
i := 0
for ; i+n <= len(sr); i += n {
chunk := sr[i:i+n]
if cubeSum(chunk)%2 == 0 {
reverse(chunk)
} else {
shift(chunk)
}
}
return string(sr[:i])
}
```

## Test cases to validate our solution

```
package our_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func dotest(s string, n int, exp string) {
var ans = Revrot(s, n)
Expect(ans).To(Equal(exp))
}
var _ = Describe("Tests Revrot", func() {
It("should handle basic cases", func() {
dotest("1234", 0, "")
dotest("", 0, "")
dotest("1234", 5, "")
var s = "733049910872815764"
dotest(s, 5, "330479108928157")
})
})
```