update: slice exercises 21, 23, 24, 25, 26
This commit is contained in:
17
16-slices/exercises/21-correct-the-lyric/hints.md
Normal file
17
16-slices/exercises/21-correct-the-lyric/hints.md
Normal file
@ -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:]...)
|
||||
```
|
@ -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.
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
|
@ -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
|
||||
|
16
16-slices/exercises/24-fix-the-memory-leak/hints.md
Normal file
16
16-slices/exercises/24-fix-the-memory-leak/hints.md
Normal file
@ -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.
|
@ -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.
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user