add: slices advanced ops
This commit is contained in:
33
16-slices/15-multi-dimensional-slices/version-1/main.go
Normal file
33
16-slices/15-multi-dimensional-slices/version-1/main.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1st day: $200, $100
|
||||
// 2nd day: $500
|
||||
// 3rd day: $50, $25, and $75
|
||||
|
||||
spendings := [][]int{
|
||||
{200, 100}, // 1st day
|
||||
{500}, // 2nd day
|
||||
{50, 25, 75}, // 3rd day
|
||||
}
|
||||
|
||||
for i, daily := range spendings {
|
||||
var total int
|
||||
for _, spending := range daily {
|
||||
total += spending
|
||||
}
|
||||
|
||||
fmt.Printf("Day %d: %d\n", i+1, total)
|
||||
}
|
||||
}
|
30
16-slices/15-multi-dimensional-slices/version-2/main.go
Normal file
30
16-slices/15-multi-dimensional-slices/version-2/main.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
spendings := make([][]int, 0, 5)
|
||||
|
||||
spendings = append(spendings, []int{200, 100})
|
||||
spendings = append(spendings, []int{25, 10, 45, 60})
|
||||
spendings = append(spendings, []int{5, 15, 35})
|
||||
spendings = append(spendings, []int{95, 10}, []int{50, 25})
|
||||
|
||||
for i, daily := range spendings {
|
||||
var total int
|
||||
for _, spending := range daily {
|
||||
total += spending
|
||||
}
|
||||
|
||||
fmt.Printf("Day %d: %d\n", i+1, total)
|
||||
}
|
||||
}
|
52
16-slices/15-multi-dimensional-slices/version-3/main.go
Normal file
52
16-slices/15-multi-dimensional-slices/version-3/main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
spendings := fetch()
|
||||
|
||||
for i, daily := range spendings {
|
||||
var total int
|
||||
for _, spending := range daily {
|
||||
total += spending
|
||||
}
|
||||
|
||||
fmt.Printf("Day %d: %d\n", i+1, total)
|
||||
}
|
||||
}
|
||||
|
||||
func fetch() [][]int {
|
||||
content := `200 100
|
||||
25 10 45 60
|
||||
5 15 35
|
||||
95 10
|
||||
50 25`
|
||||
|
||||
lines := strings.Split(content, "\n")
|
||||
|
||||
spendings := make([][]int, len(lines))
|
||||
|
||||
for i, line := range lines {
|
||||
fields := strings.Fields(line)
|
||||
|
||||
spendings[i] = make([]int, len(fields))
|
||||
|
||||
for j, field := range fields {
|
||||
spending, _ := strconv.Atoi(field)
|
||||
spendings[i][j] = spending
|
||||
}
|
||||
}
|
||||
|
||||
return spendings
|
||||
}
|
Reference in New Issue
Block a user