From 435c08e80021f26db6dac03d0afd3a19e0fb30ff Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Fri, 23 Aug 2019 10:19:50 +0300 Subject: [PATCH] update: slice exercises 21, 23, 24, 25, 26 --- .../exercises/21-correct-the-lyric/hints.md | 17 +++++++ .../exercises/21-correct-the-lyric/main.go | 13 +---- .../main.go | 51 ++++++++++++------- .../exercises/24-fix-the-memory-leak/hints.md | 16 ++++++ .../exercises/24-fix-the-memory-leak/main.go | 24 +++------ 16-slices/exercises/25-add-lines/main.go | 11 ++-- .../exercises/26-print-daily-requests/main.go | 40 ++++++++------- 7 files changed, 101 insertions(+), 71 deletions(-) create mode 100644 16-slices/exercises/21-correct-the-lyric/hints.md create mode 100644 16-slices/exercises/24-fix-the-memory-leak/hints.md diff --git a/16-slices/exercises/21-correct-the-lyric/hints.md b/16-slices/exercises/21-correct-the-lyric/hints.md new file mode 100644 index 0000000..378a452 --- /dev/null +++ b/16-slices/exercises/21-correct-the-lyric/hints.md @@ -0,0 +1,17 @@ +# Hints + +You can use the following slice operations to solve the exercise: + ++ Prepends "value" to the slice: + ```go + slice = append([]string{"value"}, slice...) + ``` + ++ Appends some part (N to M) of the same slice to itself: + ```go + slice = append(slice, slice[N:M]...) + ``` ++ Copies the last part of the slice starting from M to the first part of the slice until N: + ```go + slice = append(slice[:N], slice[M:]...) + ``` diff --git a/16-slices/exercises/21-correct-the-lyric/main.go b/16-slices/exercises/21-correct-the-lyric/main.go index a2ad7c1..055265a 100644 --- a/16-slices/exercises/21-correct-the-lyric/main.go +++ b/16-slices/exercises/21-correct-the-lyric/main.go @@ -51,18 +51,7 @@ import ( // // HINTS // -// Only look at this if you get stuck. -// -// You can use the following slice operations to solve the exercise. -// -// + Prepends "value" to the slice: -// slice = append([]string{"value"}, slice...) -// -// + Appends some part (N to M) of the same slice to itself: -// slice = append(slice, slice[N:M]...) -// -// + Copies the last part of the slice starting from M to the first part of the slice until N: -// slice = append(slice[:N], slice[M:]...) +// If you get stuck, check out the hints.md file. // // --------------------------------------------------------- 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 ee91af5..e0c5852 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 @@ -16,29 +16,42 @@ import ( // --------------------------------------------------------- // EXERCISE: Limit the backing array sharing // -// In this exercise: API means the api package. It's in the -// api folder. You need to change the code in the `api/api.go` -// to solve this exercise. +// GOAL // -// `Read` function of the api package returns a portion of -// its `temps` slice. Below, `main()` saves it to the -// `received` slice. -// -// `main()` appends to the `received` slice but doing so -// also changes the backing array of the `temps` slice. -// We don't want that. -// -// Only allow `main()` to change the part of the `temps` -// slice that is returned from the `Read()`. It shouldn't -// change the rest of the `temps`. -// -// Remember: `received` and `temps` share the same -// backing array. +// Limit the capacity of the slice that is returned +// from the `Read` function. Read on for more details. // // -// NOTE +// BEFORE YOU START // -// You need to import the api package. +// In this exercise: API means the api package. It's in the +// api folder. You need to change the code in the `api/api.go` +// to solve this exercise, and you need import the api +// package. +// +// +// WHAT IS THE PROBLEM? +// +// `Read` function of the api package returns a portion of +// its `temps` slice. Below, `main()` saves it to the +// `received` slice. +// +// `main()` appends to the `received` slice but doing so +// also changes the backing array of the `temps` slice. +// We don't want that. +// +// `main()` can change the part of the `temps` slice +// that is returned from the `Read()`, but it shouldn't +// be able to change the elements in the rest of the +// `temps`. +// +// +// WHAT YOU NEED TO DO? +// +// So you need to limit the capacity of the returned +// slice somehow. Remember: `received` and `temps` +// share the same backing array. So, appending to it +// can overwrite the same backing array. // // // CURRENT diff --git a/16-slices/exercises/24-fix-the-memory-leak/hints.md b/16-slices/exercises/24-fix-the-memory-leak/hints.md new file mode 100644 index 0000000..0cf4fe2 --- /dev/null +++ b/16-slices/exercises/24-fix-the-memory-leak/hints.md @@ -0,0 +1,16 @@ +# Hints + ++ `millions` slice's backing array uses 65 MB of memory. + ++ `make` a new slice with 10 elements with a new backing array. + + + `copy` the last 10 elements of the `millions` slice to the new slice. + + + So you will have slice with a new backing array only with 10 elements. + + + Then overwrite the `millions` slice by simply `assigning` `last10` slice to it. + ++ **Remember:** slice ~= pointer to a backing array. + + If you overwrite the slice, it will lose that + pointer. So Go can collect the unused memory. \ No newline at end of file diff --git a/16-slices/exercises/24-fix-the-memory-leak/main.go b/16-slices/exercises/24-fix-the-memory-leak/main.go index aaf365d..03d3518 100644 --- a/16-slices/exercises/24-fix-the-memory-leak/main.go +++ b/16-slices/exercises/24-fix-the-memory-leak/main.go @@ -33,10 +33,10 @@ import ( // // PROBLEM // -// `main()` calls `api.Report()`. It reports the current +// `main()` calls `api.Report()` that reports the current // memory usage. // -// After that, it calls `api.Read()`. `api.Read()` returns +// After that, `main()` calls `api.Read()` that returns // a slice with 10 millions of elements. But you only need // the last 10 elements of the returned slice. // @@ -87,26 +87,14 @@ import ( // Memory leak means: Your program is using unnecessary // computer memory. It doesn't release memory that is // no longer needed. -// See this: https://en.wikipedia.org/wiki/Memory_leak +// +// See this for more information: +// https://en.wikipedia.org/wiki/Memory_leak // // // HINTS // -// Only read this if you get stuck. -// -// + `millions` slice's backing array uses 65 MB of memory. -// -// + Make a new slice with 10 elements, and copy the last -// 10 elements of the `millions` slice to it. This will -// create a new backing array for the new slice only -// with 10 elements. -// -// Then overwrite the `millions` slice by simply -// assigning `last10` slice to it. -// -// Remember: slice ~= pointer to a backing array. -// If you overwrite the slice, it will lose that -// pointer. So Go can collect the unused memory. +// Check out `hints.md` file if you get stuck. // // --------------------------------------------------------- diff --git a/16-slices/exercises/25-add-lines/main.go b/16-slices/exercises/25-add-lines/main.go index d8e4041..ecf0d08 100644 --- a/16-slices/exercises/25-add-lines/main.go +++ b/16-slices/exercises/25-add-lines/main.go @@ -16,11 +16,14 @@ import ( // So, create a new slice and copy every words into it. Lastly, // after each sentence, add a newline character ('\n'). // -// ORIGINAL SLICE: -// [yesterday all my troubles seemed so far away now it looks as though they are here to stay oh i believe in yesterday] // -// EXPECTED SLICE (NEW): -// [yesterday all my troubles seemed so far \n away now it looks as though they are here to stay \n oh i believe in yesterday \n] +// ORIGINAL SLICE: +// +// [yesterday all my troubles seemed so far away now it looks as though they are here to stay oh i believe in yesterday] +// +// EXPECTED SLICE (NEW): +// +// [yesterday all my troubles seemed so far \n away now it looks as though they are here to stay \n oh i believe in yesterday \n] // // // CURRENT OUTPUT diff --git a/16-slices/exercises/26-print-daily-requests/main.go b/16-slices/exercises/26-print-daily-requests/main.go index d165141..12395cd 100644 --- a/16-slices/exercises/26-print-daily-requests/main.go +++ b/16-slices/exercises/26-print-daily-requests/main.go @@ -18,28 +18,32 @@ import ( // See the `reqs` slice and the steps in the code below. // // -// RESTRICTION +// RESTRICTIONS // -// Your code should work even if you add to or remove the -// existing elements from the `reqs` slice. +// 1. You need to produce the daily slice, don't just loop +// and print the element totals directly. The goal is +// gaining more experience in slice operations. // -// For example, after solving the exercise, try it with -// this new data: +// 2. Your code should work even if you add to or remove the +// existing elements from the `reqs` slice. // -// reqs := []int{ -// 500, 600, 250, -// 200, 400, 50, -// 900, 800, 600, -// 750, 250, 100, -// 150, 654, 235, -// 320, 534, 765, -// 121, 876, 285, -// 543, 642, -// // the last element is missing (your code should be able to handle this) -// // that is why you shouldn't calculate the `size` below manually. -// } +// For example, after solving the exercise, try it with +// this new data: // -// The grand total of the new data should be 10525. +// reqs := []int{ +// 500, 600, 250, +// 200, 400, 50, +// 900, 800, 600, +// 750, 250, 100, +// 150, 654, 235, +// 320, 534, 765, +// 121, 876, 285, +// 543, 642, +// // the last element is missing (your code should be able to handle this) +// // that is why you shouldn't calculate the `size` below manually. +// } +// +// The grand total of the new data should be 10525. // // // EXPECTED OUTPUT