add: slice adv. ops. exercises
This commit is contained in:
@@ -25,8 +25,10 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
report()
|
||||
img = append([]byte(nil), img[:24]...)
|
||||
img = img[:24:24]
|
||||
// img = img[:24:24] // unnecessary
|
||||
report()
|
||||
|
||||
// s.PrintBacking = true
|
||||
// s.MaxPerLine = 8
|
||||
|
@@ -1,68 +0,0 @@
|
||||
# Slice Exercises
|
||||
|
||||
---
|
||||
|
||||
# ANNOUNCEMENT
|
||||
|
||||
I teach you what the other courses don't even care to teach.
|
||||
|
||||
**What's new?**
|
||||
* New Section: Advanced Slice Operations
|
||||
* New Exercises for the Slice Internals
|
||||
* New Exercises for the Slices: Advanced Operations
|
||||
|
||||
**What are you going to learn?**
|
||||
* Full Slice Expressions: Limiting access to the backing array
|
||||
* Make(): Preallocation
|
||||
* Copy(): Efficiently and safely copy elements without using a loop
|
||||
* Multi-Dimensional Slices
|
||||
|
||||
**What's coming next?**
|
||||
* Empty Filer Finder: Your first taste of file operations.
|
||||
* Bouncing Ball: Create a bouncing ball animation on a 2D surface.
|
||||
* Png Parser: Parse a PNG file by hand and tell its dimensions.
|
||||
|
||||
These lectures will be added in the next 3 weeks.
|
||||
|
||||
**Statistics:**
|
||||
* +1 hour of additional content!
|
||||
* +5 new lectures
|
||||
* +20 new questions
|
||||
* 3 + ? new exercises
|
||||
|
||||
**Total content in the slices section:**
|
||||
* ? hours
|
||||
* ? lectures
|
||||
* ? questions
|
||||
* ? exercises
|
||||
|
||||
---
|
||||
|
||||
## Full Slice Exp + Make + Copy + Multi-Dim Slices
|
||||
|
||||
# FIX THIS
|
||||
1. **[Limit the backing array sharing](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/??-limit-the-backing-array-sharing)**
|
||||
|
||||
* fix the excess memory allocation
|
||||
* return a huge slice from a func, ask the student fix it
|
||||
* full slice exp: https://play.golang.org/p/SPrLspRBXdI
|
||||
* copy: https://play.golang.org/p/SPrLspRBXdI
|
||||
* + put \n for the beatles exercise using copy
|
||||
```go
|
||||
s = append(s, 0 /* use the zero value of the element type */)
|
||||
copy(s[i+1:], s[i:])
|
||||
s[i] = x
|
||||
```
|
||||
|
||||
|
||||
* multi dim slices batches
|
||||
```go
|
||||
actions := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
batchSize := 3
|
||||
var batches [][]int
|
||||
|
||||
for batchSize < len(actions) {
|
||||
actions, batches = actions[batchSize:], append(batches, actions[0:batchSize:batchSize])
|
||||
}
|
||||
batches = append(batches, actions)
|
||||
```
|
@@ -1,14 +0,0 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: ?
|
||||
//
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
// 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
|
||||
|
||||
func main() {
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
package api
|
||||
|
||||
var temps = []int{5, 10, 3, 25, 45, 80, 90}
|
||||
|
||||
// Read returns a range of temperature readings beginning from
|
||||
// the `start` until to the `stop`.
|
||||
func Read(start, stop int) []int {
|
||||
// ----------------------------------------
|
||||
// RESTRICTIONS — ONLY ADD YOUR CODE HERE
|
||||
|
||||
portion := temps[start:stop]
|
||||
|
||||
// ----------------------------------------
|
||||
return portion
|
||||
}
|
||||
|
||||
// All returns all the temperature readings
|
||||
func All() []int {
|
||||
return temps
|
||||
}
|
@@ -1,73 +0,0 @@
|
||||
// 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"
|
||||
|
||||
"github.com/inancgumus/learngo/16-slices/exercises/19-limit-the-backing-array-sharing/api"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Limit the backing array sharing
|
||||
//
|
||||
// You've created an API that returns population counts in a country.
|
||||
// To do that, you return an int slice up to some portion of it.
|
||||
//
|
||||
// There is a program that uses your API but it appends to the slice
|
||||
// that your API returns. Doing so, overwrites your API's slice's
|
||||
// backing array as well.
|
||||
//
|
||||
// Change your API so that it prevents the overwriting when
|
||||
// the client code wants to append to the returned slice from your
|
||||
// API.
|
||||
//
|
||||
//
|
||||
// STEPS
|
||||
//
|
||||
// 1. Open the code inside the `api/api.go` folder
|
||||
//
|
||||
// 2. Fix the code there (not here — but run this code after)
|
||||
//
|
||||
//
|
||||
// CURRENT OUTPUT
|
||||
//
|
||||
// The following program overwrites the elements incorrectly
|
||||
// You need to change your API to prevent this behavior
|
||||
// ^ ^
|
||||
// | |
|
||||
// API's readings: [5 10 3 1 3 80 90]
|
||||
// Your readings : [5 10 3 1 3]
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
// Now the program cannot change the API's original backing array
|
||||
// (beyond the returned capacity) (so the api now owns the control)
|
||||
// ^ ^
|
||||
// | |
|
||||
// API's readings: [5 10 3 25 45 80 90]
|
||||
// Your readings : [5 10 3 1 3]
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DO NOT TOUCH THE FOLLOWING CODE
|
||||
// THIS IS THE CLIENT PROGRAM THAT USES YOUR API
|
||||
// YOU CANNOT CONTROL IT! :)
|
||||
|
||||
// reads the first three temperatures
|
||||
temps := api.Read(0, 3)
|
||||
|
||||
// appends two new temperature readings
|
||||
temps = append(temps, []int{1, 3}...)
|
||||
|
||||
// prints the current temperatures
|
||||
fmt.Println("API's readings:", api.All())
|
||||
fmt.Println("Your readings :", temps)
|
||||
}
|
@@ -1,24 +0,0 @@
|
||||
package api
|
||||
|
||||
var temps = []int{5, 10, 3, 25, 45, 80, 90}
|
||||
|
||||
// Read returns a range of temperature readings beginning from
|
||||
// the `start` until to the `stop`.
|
||||
func Read(start, stop int) []int {
|
||||
// Uses a full slice expression to control the length of the
|
||||
// backing array (or the capacity of the returned slice).
|
||||
//
|
||||
// So the next append allocates a new backing array, which
|
||||
// in turn doesn't overwrite the temps slice's backing array.
|
||||
// ^^
|
||||
// ||
|
||||
// / \
|
||||
// | |
|
||||
portion := temps[start:stop:stop]
|
||||
return portion
|
||||
}
|
||||
|
||||
// All returns all the temperature readings
|
||||
func All() []int {
|
||||
return temps
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
// 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"
|
||||
|
||||
"github.com/inancgumus/learngo/16-slices/exercises/19-limit-the-backing-array-sharing/solution/api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
temps := api.Read(0, 3)
|
||||
|
||||
temps = append(temps, []int{1, 3}...)
|
||||
|
||||
fmt.Println("API's readings:", api.All())
|
||||
fmt.Println("Your readings :", temps)
|
||||
}
|
Reference in New Issue
Block a user