update: slice exercises 21, 23, 24, 25, 26

This commit is contained in:
Inanc Gumus
2019-08-23 10:19:50 +03:00
parent cfc6f5fbfa
commit 435c08e800
7 changed files with 101 additions and 71 deletions

View 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:]...)
```

View File

@ -51,18 +51,7 @@ import (
// //
// HINTS // HINTS
// //
// Only look at this if you get stuck. // If you get stuck, check out the hints.md file.
//
// 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:]...)
// //
// --------------------------------------------------------- // ---------------------------------------------------------

View File

@ -16,9 +16,21 @@ import (
// --------------------------------------------------------- // ---------------------------------------------------------
// EXERCISE: Limit the backing array sharing // EXERCISE: Limit the backing array sharing
// //
// GOAL
//
// Limit the capacity of the slice that is returned
// from the `Read` function. Read on for more details.
//
//
// BEFORE YOU START
//
// In this exercise: API means the api package. It's in the // 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` // api folder. You need to change the code in the `api/api.go`
// to solve this exercise. // 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 // `Read` function of the api package returns a portion of
// its `temps` slice. Below, `main()` saves it to the // its `temps` slice. Below, `main()` saves it to the
@ -28,17 +40,18 @@ import (
// also changes the backing array of the `temps` slice. // also changes the backing array of the `temps` slice.
// We don't want that. // We don't want that.
// //
// Only allow `main()` to change the part of the `temps` // `main()` can change the part of the `temps` slice
// slice that is returned from the `Read()`. It shouldn't // that is returned from the `Read()`, but it shouldn't
// change the rest of the `temps`. // be able to change the elements in the rest of the
// // `temps`.
// Remember: `received` and `temps` share the same
// backing array.
// //
// //
// NOTE // WHAT YOU NEED TO DO?
// //
// You need to import the api package. // 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 // CURRENT

View 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.

View File

@ -33,10 +33,10 @@ import (
// //
// PROBLEM // PROBLEM
// //
// `main()` calls `api.Report()`. It reports the current // `main()` calls `api.Report()` that reports the current
// memory usage. // 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 // a slice with 10 millions of elements. But you only need
// the last 10 elements of the returned slice. // the last 10 elements of the returned slice.
// //
@ -87,26 +87,14 @@ import (
// Memory leak means: Your program is using unnecessary // Memory leak means: Your program is using unnecessary
// computer memory. It doesn't release memory that is // computer memory. It doesn't release memory that is
// no longer needed. // 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 // HINTS
// //
// Only read this if you get stuck. // Check out `hints.md` file 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.
// //
// --------------------------------------------------------- // ---------------------------------------------------------

View File

@ -16,10 +16,13 @@ import (
// So, create a new slice and copy every words into it. Lastly, // So, create a new slice and copy every words into it. Lastly,
// after each sentence, add a newline character ('\n'). // after each sentence, add a newline character ('\n').
// //
//
// ORIGINAL SLICE: // 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] // [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): // 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] // [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]
// //
// //

View File

@ -18,9 +18,13 @@ import (
// See the `reqs` slice and the steps in the code below. // 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 // 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.
//
// 2. Your code should work even if you add to or remove the
// existing elements from the `reqs` slice. // existing elements from the `reqs` slice.
// //
// For example, after solving the exercise, try it with // For example, after solving the exercise, try it with