update: slices 23th exercise

This commit is contained in:
Inanc Gumus
2019-08-18 12:59:50 +03:00
parent 4b2d2a3d6b
commit dfca449457
4 changed files with 52 additions and 39 deletions

View File

@ -1,13 +1,25 @@
package api package api
// note: "client" means the users or importers of your package.
// The original temperatures slice.
var temps = []int{5, 10, 3, 25, 45, 80, 90} var temps = []int{5, 10, 3, 25, 45, 80, 90}
// ^ ^ ^ ^ ^
// | | | +-----------+
// the client is allowed to |
// change these elements. |
// |
// but the client shouldn't change the
// rest of the elements after the 3rd element.
// Read returns a range of temperature readings beginning from // Read returns a range of temperature readings beginning from
// the `start` until to the `stop`. // the `start` until to the `stop`.
func Read(start, stop int) []int { func Read(start, stop int) []int {
// ---------------------------------------- // ----------------------------------------
// RESTRICTIONS — ONLY ADD YOUR CODE HERE // RESTRICTIONS — ONLY ADD YOUR CODE HERE
// returns a slice from the temps slice to the client.
portion := temps[start:stop] portion := temps[start:stop]
// ---------------------------------------- // ----------------------------------------

View File

@ -16,43 +16,45 @@ import (
// --------------------------------------------------------- // ---------------------------------------------------------
// EXERCISE: Limit the backing array sharing // EXERCISE: Limit the backing array sharing
// //
// You've created an API that returns population counts in a country. // You've created a package and saved the temperatures in an int slice.
// To do that, you return an int slice up to some portion of it. // (find it in the api folder).
// //
// There is a program that uses your API but it appends to the slice // When `Read()` function is called, you return the desired elements.
// 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 // Another programmer has decided to use your package.
// the client code wants to append to the returned slice from your // She calls: `api.Read(0, 3)` and gets a slice with a length of 3.
// API. //
// However: She appends to the slice, so she practically changes
// the elements in the api package's `temps` slice (the original one).
//
// Only allow her to change the first three elements.
// Prevent her from changing the rest of the elements of
// `api.temps` slice.
// //
// //
// STEPS // STEPS
// //
// 1. Open the code inside the `api/api.go` folder // You only need to change the code inside the `api/api.go` folder
//
// 2. Fix the code there (not here — but run this code after)
// //
// //
// CURRENT OUTPUT // CURRENT
// //
// 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] // v v
// Your readings : [5 10 3 1 3] // api.temps : [5 10 3 1 3 80 90]
// main.temps : [5 10 3 1 3]
// ^ ^ append changes the api package's
// temps slice: [1 3]
// //
// //
// EXPECTED OUTPUT
// //
// Now the program cannot change the API's original backing array // EXPECTED
// (beyond the returned capacity) (so the api now owns the control) //
// ^ ^ // Now the program cannot change the API's original backing array.
// | | // | |
// API's readings: [5 10 3 25 45 80 90] // v v
// Your readings : [5 10 3 1 3] // api.temps : [5 10 3 25 45 80 90]
// main.temps : [5 10 3 1 3]
// //
// --------------------------------------------------------- // ---------------------------------------------------------
@ -61,13 +63,13 @@ func main() {
// THIS IS THE CLIENT PROGRAM THAT USES YOUR API // THIS IS THE CLIENT PROGRAM THAT USES YOUR API
// YOU CANNOT CONTROL IT! :) // YOU CANNOT CONTROL IT! :)
// reads the first three temperatures // get the first three elements from api.temps
temps := api.Read(0, 3) temps := api.Read(0, 3)
// appends two new temperature readings // changes the api.temps's backing array.
// you need to prevent this.
temps = append(temps, []int{1, 3}...) temps = append(temps, []int{1, 3}...)
// prints the current temperatures fmt.Println("api.temps :", api.All())
fmt.Println("API's readings:", api.All()) fmt.Println("main.temps :", temps)
fmt.Println("Your readings :", temps)
} }

View File

@ -1,21 +1,20 @@
package api package api
// The original temperatures slice.
var temps = []int{5, 10, 3, 25, 45, 80, 90} var temps = []int{5, 10, 3, 25, 45, 80, 90}
// Read returns a range of temperature readings beginning from // Read returns a range of temperature readings beginning from
// the `start` until to the `stop`. // the `start` until to the `stop`.
func Read(start, stop int) []int { func Read(start, stop int) []int {
// //
// Uses a full slice expression to control the length of the // This third index prevents the clients of this package from
// backing array (or the capacity of the returned slice). // overwriting the original temps slice's backing array. It
// // limits the capacity of the returned slice. See the
// So the next append allocates a new backing array, which // full slice expressions lecture.
// in turn doesn't overwrite the temps slice's backing array. // ^
// ^^ // |
// ||
// / \
// | |
portion := temps[start:stop:stop] portion := temps[start:stop:stop]
return portion return portion
} }

View File

@ -17,6 +17,6 @@ func main() {
temps := api.Read(0, 3) temps := api.Read(0, 3)
temps = append(temps, []int{1, 3}...) temps = append(temps, []int{1, 3}...)
fmt.Println("API's readings:", api.All()) fmt.Println("api.temps :", api.All())
fmt.Println("Your readings :", temps) fmt.Println("main.temps :", temps)
} }