Skip to content

Instantly share code, notes, and snippets.

@178inaba
Last active January 17, 2021 05:44
Show Gist options
  • Save 178inaba/1cccb64f5e7fc4882f085c25661a0937 to your computer and use it in GitHub Desktop.
Save 178inaba/1cccb64f5e7fc4882f085c25661a0937 to your computer and use it in GitHub Desktop.
Separate slice percentage.
package main
import (
"fmt"
"math"
)
func main() {
a := 51
b := 35
c := 32
total := float64(a) + float64(b) + float64(c)
//total :=a+b+c
//if a+b+c != 100 {
// log.Fatal("a + b + c not 100")
//}
s := make([]int, 2461)
for i := 0; i < len(s); i++ {
s[i] = i + 1
}
l := float64(len(s))
fmt.Println("----------")
fmt.Println(float64(a) / total)
fmt.Println(l * float64(a) / total)
fmt.Println(int(l * float64(a) / total))
fmt.Println(float64(b) / total)
fmt.Println(l * float64(b) / total)
fmt.Println(int(l * float64(b) / total))
fmt.Println(float64(c) / total)
fmt.Println(l * float64(c) / total)
fmt.Println(int(l * float64(c) / total))
fmt.Println("----------")
fmt.Println(int(l*float64(a)/100) + int(l*float64(b)/100) + int(l*float64(c)/100))
fmt.Println("----------")
fmt.Println(len(s))
as, s := s[:int(math.Round(l*float64(a)/total))], s[int(math.Round(l*float64(a)/total)):]
fmt.Println(len(s))
bs, s := s[:int(math.Round(l*float64(b)/total))], s[int(math.Round(l*float64(b)/total)):]
fmt.Println(len(s))
cs, s := s[:int(math.Round(l*float64(c)/total))], s[int(math.Round(l*float64(c)/total)):]
fmt.Println(len(s))
fmt.Println("----------")
fmt.Println(s)
fmt.Println(as)
fmt.Println(bs)
fmt.Println(cs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment