From 4f6081258900e4cf78c52746ff9c5db72e8b8e6d Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Sun, 18 Aug 2019 15:32:48 +0300 Subject: [PATCH] refactor: slice exercises 16-21 --- .../16-internals-backing-array-fix/main.go | 39 +++++++-------- .../solution/main.go | 5 +- .../17-internals-backing-array-sort/main.go | 4 +- .../18-internals-slice-header/main.go | 47 ++++++++++++------- .../solution/main.go | 1 - .../exercises/19-observe-len-cap/main.go | 2 +- .../exercises/21-correct-the-lyric/main.go | 2 +- .../main.go | 13 +++++ 16-slices/exercises/25-add-lines/main.go | 1 + 16-slices/exercises/README.md | 6 +-- 10 files changed, 68 insertions(+), 52 deletions(-) diff --git a/16-slices/exercises/16-internals-backing-array-fix/main.go b/16-slices/exercises/16-internals-backing-array-fix/main.go index 21b15c4..2d09e51 100644 --- a/16-slices/exercises/16-internals-backing-array-fix/main.go +++ b/16-slices/exercises/16-internals-backing-array-fix/main.go @@ -9,25 +9,19 @@ package main import ( "fmt" - "math/rand" - "time" ) // --------------------------------------------------------- // EXERCISE: Fix the backing array problem // -// You receive numbers from an API. After you're done working -// with it, the API needs to continue using those numbers. -// -// But your program changes the numbers (changes the API's slice). -// -// Fix the program so that your program doesn't modify -// the original numbers. +// Ensure that changing the elements of the `mine` slice +// does not change the elements of the `nums` slice. // // -// RESTRICTION +// CURRENT OUTPUT (INCORRECT) // -// Fix the problem only in the designated area of the code below. +// Mine : [-50 -100 -150 25 30 50] +// Original nums: [-50 -100 -150] // // // EXPECTED OUTPUT @@ -35,26 +29,27 @@ import ( // Mine : [-50 -100 -150] // Original nums: [56 89 15] // -// Note: Original nums may vary (they're random) -// But your slice should look like the above (mine slice) -// -// Yes, it should output only three numbers for the both slices! -// // --------------------------------------------------------- func main() { - // API returns random numbers in an int slice - rand.Seed(time.Now().UnixNano()) - nums := rand.Perm(100) + // DON'T TOUCH THE FOLLOWING CODE + nums := []int{56, 89, 15, 25, 30, 50} // ---------------------------------------- - // RESTRICTIONS — ONLY ADD YOUR CODE HERE + // ONLY ADD YOUR CODE HERE // + // Ensure that nums slice never changes even though + // the mine slice changes. mine := nums - // // ---------------------------------------- + // DON'T TOUCH THE FOLLOWING CODE + // + // This code changes the elements of the nums + // slice. + // mine[0], mine[1], mine[2] = -50, -100, -150 - fmt.Println("Mine :", mine) + + fmt.Println("Mine :", mine[:3]) fmt.Println("Original nums:", nums[:3]) } diff --git a/16-slices/exercises/16-internals-backing-array-fix/solution/main.go b/16-slices/exercises/16-internals-backing-array-fix/solution/main.go index f9419fc..67fcb64 100644 --- a/16-slices/exercises/16-internals-backing-array-fix/solution/main.go +++ b/16-slices/exercises/16-internals-backing-array-fix/solution/main.go @@ -9,13 +9,10 @@ package main import ( "fmt" - "math/rand" - "time" ) func main() { - rand.Seed(time.Now().UnixNano()) - nums := rand.Perm(100) + nums := []int{56, 89, 15, 25, 30, 50} // ---------------------------------------- // breaks the connection: diff --git a/16-slices/exercises/17-internals-backing-array-sort/main.go b/16-slices/exercises/17-internals-backing-array-sort/main.go index 379bae7..29570a2 100644 --- a/16-slices/exercises/17-internals-backing-array-sort/main.go +++ b/16-slices/exercises/17-internals-backing-array-sort/main.go @@ -16,12 +16,12 @@ import ( // // 1. Sort only the middle 3 items. // -// 2. All the slices should see your change. +// 2. All the slices should see your changes. // // // RESTRICTION // -// Do not sort manually. Sort by slicing then by using the sort package. +// Do not sort manually. Sort by using the sort package. // // // EXPECTED OUTPUT diff --git a/16-slices/exercises/18-internals-slice-header/main.go b/16-slices/exercises/18-internals-slice-header/main.go index 65fd43a..17fccc7 100644 --- a/16-slices/exercises/18-internals-slice-header/main.go +++ b/16-slices/exercises/18-internals-slice-header/main.go @@ -20,9 +20,9 @@ import ( // // EXPECTED OUTPUT // -// Note that, your memory usage numbers may vary. These are on my -// own system. However, the size of the arrays and slices should be -// the same on your own system as well (if you're on 64-bit machine). +// Note that, your memory usage numbers may vary. However, the size of the +// arrays and slices should be the same on your own system as well +// (if you're on a 64-bit machine). // // // [initial memory usage] @@ -54,62 +54,73 @@ import ( // - Just call it with a message that matches to the expected output. // // passArray function: -// - Accepts a [size]int array, so you can pass it your array. // - It automatically prints the memory usage. +// - Accepts a [size]int array, so you can pass your array to it. // // passSlice function: -// - Accepts an int slice, so you can pass it one of your slices. // - It automatically prints the memory usage. +// - Accepts an int slice, so you can pass it one of your slices. // // --------------------------------------------------------- const size = 1e7 func main() { - // stops the gc: prevents cleaning up the memory + // don't worry about this code. + // it stops the garbage collector: prevents cleaning up the memory. + // see the link if you're curious: + // https://en.wikipedia.org/wiki/Garbage_collection_(computer_science) debug.SetGCPercent(-1) - // run the program to see what this prints + // run the program to see the initial memory usage. report("initial memory usage") // 1. allocate an array with 10 million int elements - // this array's size is equal to ~80MB - // hint: use the `size` constant - // - // 2. print the memory usage + // the array's size will be equal to ~80MB + // hint: use the `size` constant above. + + // 2. print the memory usage (use the report func). + + // 3. copy the array to a new array. - // 3. copy the array to a new array (just assign) // 4. print the memory usage // 5. pass the array to the passArray function - // 6. convert the one of the arrays to a slice (by slicing) + // 6. convert one of the arrays to a slice + // 7. slice only the first 1000 elements of the array + // 8. slice only the elements of the array between 1000 and 10000 - // 9. print the memory usage + + // 9. print the memory usage (report func) // 10. pass the one of the slices to the passSlice function // 11. print the sizes of the arrays and slices // hint: use the unsafe.Sizeof function + // see more here: https://golang.org/pkg/unsafe/#Sizeof } -// observe that passing an array affects the memory usage dramatically -// // passes [size]int array — about 80MB! +// +// observe that passing an array to a function (or assigning it to a variable) +// affects the memory usage dramatically func passArray(items [size]int) { items[0] = 100 report("inside passArray") } -// observe that passing a slice doesn't affect the memory usage -// // only passes 24-bytes of slice header +// +// observe that passing a slice doesn't affect the memory usage func passSlice(items []int) { items[0] = 100 report("inside passSlice") } +// reports the current memory usage +// don't worry about this code func report(msg string) { var m runtime.MemStats runtime.ReadMemStats(&m) diff --git a/16-slices/exercises/18-internals-slice-header/solution/main.go b/16-slices/exercises/18-internals-slice-header/solution/main.go index f706765..132fb02 100644 --- a/16-slices/exercises/18-internals-slice-header/solution/main.go +++ b/16-slices/exercises/18-internals-slice-header/solution/main.go @@ -17,7 +17,6 @@ import ( const size = 1e7 func main() { - // stops the gc: prevents cleaning up the memory debug.SetGCPercent(-1) report("initial memory usage") diff --git a/16-slices/exercises/19-observe-len-cap/main.go b/16-slices/exercises/19-observe-len-cap/main.go index 85c99fe..9df867e 100644 --- a/16-slices/exercises/19-observe-len-cap/main.go +++ b/16-slices/exercises/19-observe-len-cap/main.go @@ -27,7 +27,7 @@ func main() { // // 7. comment out everything // - // 8. declare it again using a slice literal + // 8. declare the games slice again using a slice literal // (use the same elements from step 3) // --- #2 --- diff --git a/16-slices/exercises/21-correct-the-lyric/main.go b/16-slices/exercises/21-correct-the-lyric/main.go index 0f95fd9..a1e5d2e 100644 --- a/16-slices/exercises/21-correct-the-lyric/main.go +++ b/16-slices/exercises/21-correct-the-lyric/main.go @@ -34,7 +34,7 @@ import ( // // BONUS // -// + Think about when does the append allocates a new backing array. +// + Think about when does the append allocate a new backing array. // // + Check whether your conclusions are correct. // diff --git a/16-slices/exercises/23-limit-the-backing-array-sharing/main.go b/16-slices/exercises/23-limit-the-backing-array-sharing/main.go index 4fe4a10..d39d969 100644 --- a/16-slices/exercises/23-limit-the-backing-array-sharing/main.go +++ b/16-slices/exercises/23-limit-the-backing-array-sharing/main.go @@ -32,6 +32,19 @@ import ( // `api.temps` slice. // // +// BE CAREFUL +// +// This code imports the api package from the learn go programming +// original repository. So, when you change the code in the api folder +// ensure that this code imports your own code. +// +// see above (this is the original repository code): +// you need to change it to your own repository if you're using your own +// repository: +// +// "github.com/inancgumus/learngo/16-slices/exercises/23-limit-the-backing-array-sharing/api" +// +// // STEPS // // You only need to change the code inside the `api/api.go` folder diff --git a/16-slices/exercises/25-add-lines/main.go b/16-slices/exercises/25-add-lines/main.go index 8f8c726..d8e4041 100644 --- a/16-slices/exercises/25-add-lines/main.go +++ b/16-slices/exercises/25-add-lines/main.go @@ -55,6 +55,7 @@ import ( // // go get -u github.com/inancgumus/prettyslice // +// // --------------------------------------------------------- func main() { diff --git a/16-slices/exercises/README.md b/16-slices/exercises/README.md index 58c2659..ab10436 100644 --- a/16-slices/exercises/README.md +++ b/16-slices/exercises/README.md @@ -80,11 +80,11 @@ Please update your local copy of the prettyslice package for some examples to wo 2. **[Limit the backing array sharing](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/23-limit-the-backing-array-sharing)** - Your API does not control the slices that it share with the outside world. You need to fix it. + Your package needs to control the slices that it shares with the outside world. 3. **[Fix the Memory Leak](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/24-fix-the-memory-leak)** - A slice retrieved from an API causes a memory leak in your program. You need to fix it. + A slice retrieved from a package causes a memory leak in your program. You need to fix it. 4. **[Add a newline after each sentence](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/25-add-lines)** @@ -92,4 +92,4 @@ Please update your local copy of the prettyslice package for some examples to wo 5. **[Print Daily Requests](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/26-print-daily-requests)** - Group the requests log into a multi-dimensional from a single-dimensional slice. Allocate a slice with the exact size needed by doing some wizardary calculations. And lastly, pretty print the result. + Group the web request logs into a multi-dimensional slice. Allocate a slice with the exact size needed by doing some wizardary calculations. And lastly, pretty print the result.