Skip to content

Commit

Permalink
Add Maximum Array Rotation in Go (#4514)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangrabo authored Feb 18, 2025
1 parent 4620949 commit e27f07f
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions archive/g/go/maximum-array-rotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"fmt"
"os"
"unicode"
)

func main() {
if len(os.Args) != 2 {
printError()
} else {
if len(createSlice()) == 0 {
printError()
} else {
fmt.Println(getMaxWeightedSum(createSlice()))
}
}
}

func createSlice() (slice []int) {
stringSlice := os.Args[1]
for _, char := range stringSlice {
if unicode.IsDigit(char) {
slice = append(slice, int(char)-'0')
}
}
return
}

func getWeightedSum(slice []int) (sum int) {
for i, num := range slice {
sum += i * num
}
return
}

func getMaxWeightedSum(slice []int) (max int) {
max = 0
for i := 0; i < len(slice); i++ {
if getWeightedSum(slice) > max {
max = getWeightedSum(slice)
}
slice = rotateSlice(slice)
}
return
}

func rotateSlice(slice []int) (rotatedSlice []int) {
first := slice[0]
slice = slice[1:]
slice = append(slice, first)
rotatedSlice = slice
return
}

func printError() {
fmt.Println("Usage: please provide a list of integers (e.g. \"8, 3, 1, 2\")")
}

0 comments on commit e27f07f

Please sign in to comment.