add: slices advanced ops
This commit is contained in:
28
16-slices/12-full-slice-expressions/1-theory/main.go
Normal file
28
16-slices/12-full-slice-expressions/1-theory/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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 (
|
||||
s "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s.PrintBacking = true
|
||||
|
||||
sliceable := []byte{'f', 'u', 'l', 'l'}
|
||||
|
||||
s.Show("sliceable", sliceable)
|
||||
s.Show("sliceable[0:3]", sliceable[0:3])
|
||||
s.Show("sliceable[0:3:3]", sliceable[0:3:3])
|
||||
s.Show("sliceable[0:2:2]", sliceable[0:2:2])
|
||||
s.Show("sliceable[0:1:1]", sliceable[0:1:1])
|
||||
s.Show("sliceable[1:3:3]", sliceable[1:3:3])
|
||||
s.Show("sliceable[2:3:3]", sliceable[2:3:3])
|
||||
s.Show("sliceable[2:3:4]", sliceable[2:3:4])
|
||||
s.Show("sliceable[4:4:4]", sliceable[4:4:4])
|
||||
}
|
35
16-slices/12-full-slice-expressions/2-example/main.go
Normal file
35
16-slices/12-full-slice-expressions/2-example/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// 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 (
|
||||
s "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s.PrintBacking = true
|
||||
|
||||
nums := []int{1, 3, 2, 4} // #1
|
||||
// odds := nums[:2] // #2
|
||||
// odds := nums[:2:2] // #4
|
||||
// odds = append(odds, 5, 7) // #3
|
||||
|
||||
// odds := append(nums[:2:2], 5, 7) // #5
|
||||
// evens := append(nums[2:4], 6, 8) // #6
|
||||
|
||||
s.Show("nums", nums) // #1
|
||||
// s.Show("odds", odds) // #2
|
||||
// s.Show("evens", evens) // #6
|
||||
}
|
||||
|
||||
// don't mind about these options
|
||||
// they're just for printing the slices nicely
|
||||
func init() {
|
||||
s.MaxPerLine = 10
|
||||
s.Width = 55
|
||||
}
|
25
16-slices/13-make/1-theory/main.go
Normal file
25
16-slices/13-make/1-theory/main.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// 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 (
|
||||
prettyslice "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
prettyslice.PrintBacking = true
|
||||
|
||||
prettyslice.Show("make([]int, 3)", make([]int, 3))
|
||||
prettyslice.Show("make([]int, 3, 5)", make([]int, 3, 5))
|
||||
|
||||
s := make([]int, 0, 5)
|
||||
prettyslice.Show("make([]int, 0, 5)", s)
|
||||
|
||||
s = append(s, 42)
|
||||
prettyslice.Show("s = append(s, 42)", s)
|
||||
}
|
83
16-slices/13-make/2-example/main.go
Normal file
83
16-slices/13-make/2-example/main.go
Normal file
@@ -0,0 +1,83 @@
|
||||
// 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 (
|
||||
"strings"
|
||||
|
||||
s "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s.PrintBacking = true
|
||||
s.MaxPerLine = 10
|
||||
|
||||
// #1: assume that tasks can be a long list
|
||||
// ---------------------------------------------
|
||||
tasks := []string{"jump", "run", "read"}
|
||||
|
||||
// #2: INEFFICIENT WAY
|
||||
// ---------------------------------------------
|
||||
// var upTasks []string
|
||||
// s.Show("upTasks", upTasks)
|
||||
|
||||
// for _, task := range tasks {
|
||||
// upTasks = append(upTasks, strings.ToUpper(task))
|
||||
// s.Show("upTasks", upTasks)
|
||||
// }
|
||||
|
||||
// #3: SO SO WAY:
|
||||
// ---------------------------------------------
|
||||
// upTasks := make([]string, len(tasks))
|
||||
// s.Show("upTasks", upTasks)
|
||||
|
||||
// for _, task := range tasks {
|
||||
// upTasks = append(upTasks, strings.ToUpper(task))
|
||||
// s.Show("upTasks", upTasks)
|
||||
// }
|
||||
|
||||
// #4: SO SO WAY 2:
|
||||
// ---------------------------------------------
|
||||
// upTasks := make([]string, len(tasks))
|
||||
// s.Show("upTasks", upTasks)
|
||||
|
||||
// for i, task := range tasks {
|
||||
// upTasks[i] = strings.ToUpper(task)
|
||||
// s.Show("upTasks", upTasks)
|
||||
// }
|
||||
|
||||
// #5: allocates a new array
|
||||
// upTasks = append(upTasks, "PLAY")
|
||||
// s.Show("upTasks", upTasks)
|
||||
|
||||
// #6: SO SO WAY 3
|
||||
// ---------------------------------------------
|
||||
// upTasks := make([]string, 0, len(tasks))
|
||||
|
||||
// #7
|
||||
// upTasks = upTasks[:cap(upTasks)]
|
||||
|
||||
// #6
|
||||
// s.Show("upTasks", upTasks)
|
||||
|
||||
// for i, task := range tasks {
|
||||
// upTasks[i] = strings.ToUpper(task)
|
||||
// s.Show("upTasks", upTasks)
|
||||
// }
|
||||
|
||||
// #8: THE BEST WAY
|
||||
// ---------------------------------------------
|
||||
upTasks := make([]string, 0, len(tasks))
|
||||
|
||||
s.Show("upTasks", upTasks)
|
||||
|
||||
for _, task := range tasks {
|
||||
upTasks = append(upTasks, strings.ToUpper(task))
|
||||
s.Show("upTasks", upTasks)
|
||||
}
|
||||
}
|
28
16-slices/14-copy/01-usage/main.go
Normal file
28
16-slices/14-copy/01-usage/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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"
|
||||
|
||||
s "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
evens := []int{2, 4}
|
||||
odds := []int{3, 5, 7}
|
||||
|
||||
s.Show("evens [before]", evens)
|
||||
s.Show("odds [before]", odds)
|
||||
|
||||
N := copy(evens, odds)
|
||||
fmt.Printf("%d element(s) are copied.\n", N)
|
||||
|
||||
s.Show("evens [after]", evens)
|
||||
s.Show("odds [after]", odds)
|
||||
}
|
56
16-slices/14-copy/02-hacker-incident/main.go
Normal file
56
16-slices/14-copy/02-hacker-incident/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// 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"
|
||||
|
||||
s "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// #1: received the raining probabilities
|
||||
data := []float64{10, 25, 30, 50}
|
||||
|
||||
// #2: received new data
|
||||
// newData := []float64{80, 90}
|
||||
// for i := 0; i < len(newData); i++ {
|
||||
// data[i] = newData[i]
|
||||
// }
|
||||
|
||||
// #3: use copy
|
||||
// copy(data, []float64{99, 100})
|
||||
|
||||
// #4: received more data than the original
|
||||
// copy(data, []float64{10, 5, 15, 0, 20})
|
||||
|
||||
// #5: returns the # of copied elements
|
||||
// n := copy(data, []float64{10, 5, 15, 0, 20})
|
||||
// fmt.Printf("%d probabilities copied.\n", n)
|
||||
|
||||
// #6: (sometimes) use append instead of copy
|
||||
// data = append(data[:0], []float64{10, 5, 15, 0, 20}...)
|
||||
|
||||
// #7: clone a slice using copy
|
||||
// saved := make([]float64, len(data))
|
||||
// copy(saved, data)
|
||||
|
||||
// #9: clone a slice using append nil (i prefer this)
|
||||
// saved := append([]float64(nil), data...)
|
||||
|
||||
// data[0] = 0 // #8
|
||||
|
||||
// s.Show("Probabilities (saved)", saved) // #7
|
||||
|
||||
// #1: print the probabilities
|
||||
s.Show("Probabilities (data)", data)
|
||||
|
||||
fmt.Printf("Is it gonna rain? %.f%% chance.\n",
|
||||
(data[0]+data[1]+data[2]+data[3])/
|
||||
float64(len(data)))
|
||||
}
|
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
|
||||
}
|
67
16-slices/questions/5-slice-others-wip.md
Normal file
67
16-slices/questions/5-slice-others-wip.md
Normal file
@@ -0,0 +1,67 @@
|
||||
full slice exp
|
||||
make
|
||||
copy
|
||||
|
||||
# Slice Internals Quiz
|
||||
|
||||
## Where does a slice value stores its elements?
|
||||
1. In the global backing array that is shared by all the slices
|
||||
2. In a separate backing array that a slice value points to *CORRECT*
|
||||
3. In the slice value itself (like an array)
|
||||
|
||||
> **1:** There isn't something called the global backing array.
|
||||
>
|
||||
> **2:** That's right. A slice doesn't store any elements direcly. It points to an array that is stored separately on the computer memory, and a slice points to that array, a slice is just a window to that array.
|
||||
>
|
||||
> **3:** A slice values doesn't store any elements by itself. It's just a description of its backing array.
|
||||
|
||||
|
||||
## What's a backing array?
|
||||
1. An array of trustworthy friends that you can count on always
|
||||
2. An array that is stored in the slice value
|
||||
3. A slice value stores its elements in it *CORRECT*
|
||||
|
||||
> **1:** Oh, come on!
|
||||
>
|
||||
> **2:** Nope, a slice value doesn't store its backing array, the slice value just points to it. The backing array is stored separately from the slice value.
|
||||
|
||||
|
||||
## What does this program print?
|
||||
```go
|
||||
nums := []int{10, 15, 10, 2, 3, 4}
|
||||
digits := nums[len(nums)-3:]
|
||||
|
||||
nums[len(nums)-1] = 8
|
||||
digits[0] += nums[3]
|
||||
|
||||
fmt.Println(digits)
|
||||
```
|
||||
1. [2 3 4]
|
||||
2. [4 3 4]
|
||||
3. [4 3 8] *CORRECT*
|
||||
4. [10 15 10]
|
||||
5. [4 15 8]
|
||||
|
||||
> **3:** Awesome! At first, digits is [2 3 4]. After `nums[len(nums)-1] = 8`, the digits becomes [2 3 8] (it's because, digits is created by slicing the nums slice, so they share the same backing array). And lastly, after `digits[0] += nums[3]`, the digits becomes [4 3 8].
|
||||
|
||||
|
||||
## Find the correct explanation below
|
||||
1. A slice variable can store a slice value and the slice value is actually a slice header behind the scenes *CORRECT*
|
||||
2. A slice variable can only store the same slice value and it cannot be changed afterward
|
||||
3. A slice header stores the slice value
|
||||
|
||||
> **1:** That's right. You can change the slice values that is being stored in a slice variable, and the slice value is actually a slice header behind the scenes.
|
||||
>
|
||||
> **3:** Nope, actually a slice header and a slice value is the same thing.
|
||||
|
||||
|
||||
* after slicing, does it copy all the values to the new slice?
|
||||
* why indexing a slice is fast? (array is contagious on memory)
|
||||
* separate backing arrays question
|
||||
* sliced array share the same explicit array
|
||||
|
||||
##
|
||||
backing array
|
||||
slice header
|
||||
capacity
|
||||
append mechanics
|
Reference in New Issue
Block a user