The problem
Write perform MaxRot(n)
which given a optimistic integer n
returns the utmost quantity you bought doing rotations just like the above instance.
So MaxRot
is resembling:
MaxRot(56789) ought to return 68957
MaxRot(38458215) ought to return 85821534
Examples:
Take a quantity: 56789
. Rotate left, you get 67895
.
Preserve the primary digit in place and rotate left the opposite digits: 68957
.
Preserve the primary two digits in place and rotate the opposite ones: 68579
.
Preserve the primary three digits and rotate left the remaining: 68597
. Now it’s over since retaining the primary 4 it stays just one digit which rotated is itself.
You have got the next sequence of numbers:
56789 -> 67895 -> 68957 -> 68579 -> 68597
..and you could return the best: 68957
.
The answer in Golang
Possibility 1:
package deal resolution
import "strconv"
func MaxRot(n int64) int64 {
str := strconv.FormatInt(n, 10)
max := n
for i := 0; i<len(str)-1 ; i++ {
str = str[:i]+str[i+1:]+string(str[i])
num, _:= strconv.ParseInt(str, 10, 64)
if max < num { max = num }
}
return max
}
Possibility 2:
package deal resolution
import . "strconv"
func MaxRot(n int64) int64 {
s, max := FormatInt(n,10), n
for i := 0; i < len(s); i++ {
s = s[:i] + s[i:][1:] + s[i:][:1]
v,_ := ParseInt(s,10,64)
if max < v {
max = v
}
}
return max
}
Possibility 3:
package deal resolution
import "strconv"
func MaxRot(rot int64) int64 {
rotRunes := []rune(strconv.FormatInt(rot, 10))
for index := 0; index < len(rotRunes) - 1; index++ {
rightPart := append(rotRunes[index + 1:], rotRunes[index])
rotRunes = append(rotRunes[:index], rightPart...)
rotVariation, err := strconv.ParseInt(string(rotRunes), 10, 64)
if err == nil && rotVariation > rot {
rot = rotVariation
}
}
return rot
}
Check circumstances to validate our resolution
package deal solution_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func dotest(n int64, exp int64) {
var ans = MaxRot(n)
Anticipate(ans).To(Equal(exp))
}
var _ = Describe("Checks MaxRot", func() {
It("ought to deal with fundamental circumstances", func() {
dotest(38458215, 85821534)
dotest(195881031, 988103115)
dotest(896219342, 962193428)
})
})