add: slice internals exercises
This commit is contained in:
@@ -1,55 +1,68 @@
|
||||
# Slice Exercises
|
||||
|
||||
## TODO
|
||||
* internals
|
||||
* shared array: implicit/explicit
|
||||
* appending to a nil array
|
||||
* sorts package sorting
|
||||
---
|
||||
|
||||
* exercises about capacity
|
||||
* exercises about the mechanics of append
|
||||
* growing
|
||||
* adding elements at the middle etc
|
||||
* exercises about full slice expressions
|
||||
# ANNOUNCEMENT
|
||||
|
||||
* questions:
|
||||
* slice header questions
|
||||
* slice and ask what's the pointer field, len, cap etc
|
||||
* when a new backing array is allocated: nil, empty, no capacity
|
||||
* when to use a full slice expression
|
||||
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
|
||||
|
||||
---
|
||||
|
||||
## Exercises Level I - Basics
|
||||
## Full Slice Exp + Make + Copy + Multi-Dim Slices
|
||||
|
||||
1. **[???](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/???)**
|
||||
# FIX THIS
|
||||
1. **[Limit the backing array sharing](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/??-limit-the-backing-array-sharing)**
|
||||
|
||||
---
|
||||
2. **[Get and Set Array Elements](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/02-get-set-arrays)**
|
||||
* 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
|
||||
```
|
||||
|
||||
3. **[Refactor to Array Literals](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/03-array-literal)**
|
||||
|
||||
4. **[Refactor to Ellipsis](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/04-ellipsis)**
|
||||
* multi dim slices batches
|
||||
```go
|
||||
actions := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
batchSize := 3
|
||||
var batches [][]int
|
||||
|
||||
5. **[Fix](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/05-fix)**
|
||||
|
||||
6. **[Compare the Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/06-compare)**
|
||||
|
||||
7. **[Assign the Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/07-assign)**
|
||||
|
||||
---
|
||||
|
||||
## Exercises Level II
|
||||
|
||||
1. **[Wizard Printer](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/08-wizard-printer)**
|
||||
|
||||
2. **[Currency Converter](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/09-currency-converter)**
|
||||
|
||||
3. **[Hipster's Bookstore Search Engine](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/10-hipsters-love-search)**
|
||||
|
||||
4. **[Find the Average](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/11-average)**
|
||||
|
||||
5. **[Number Sorter](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/12-sorter)**
|
||||
|
||||
6. **[Word Finder](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/13-word-finder)**
|
||||
for batchSize < len(actions) {
|
||||
actions, batches = actions[batchSize:], append(batches, actions[0:batchSize:batchSize])
|
||||
}
|
||||
batches = append(batches, actions)
|
||||
```
|
14
x-tba/slices/exercises/_template/main.go
Normal file
14
x-tba/slices/exercises/_template/main.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: ?
|
||||
//
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
11
x-tba/slices/exercises/_template/solution/main.go
Normal file
11
x-tba/slices/exercises/_template/solution/main.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// 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() {
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
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
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
// 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)
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
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
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
// 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