update: slice exercises 23

This commit is contained in:
Inanc Gumus
2019-08-22 20:00:58 +03:00
parent ce5a32ecbe
commit 792a7a75d4
4 changed files with 38 additions and 65 deletions

View File

@ -1,32 +1,21 @@
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}
// ^ ^ ^ ^ ^ // Read returns a slice of elements from the temps slice.
// | | | +-----------+
// 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
// 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 IN THIS AREA
// returns a slice from the temps slice to the client.
portion := temps[start:stop] portion := temps[start:stop]
// RESTRICTIONS — ONLY ADD YOUR CODE IN THIS AREA
// ---------------------------------------- // ----------------------------------------
return portion return portion
} }
// All returns all the temperature readings // All returns the temps slice
func All() []int { func All() []int {
return temps return temps
} }

View File

@ -16,38 +16,24 @@ import (
// --------------------------------------------------------- // ---------------------------------------------------------
// EXERCISE: Limit the backing array sharing // EXERCISE: Limit the backing array sharing
// //
// You've created a package and saved the temperatures in an int slice. // In this exercise: API means the api package. It's in the
// (find it in the api folder). // api folder.
// //
// When `Read()` function is called, you return the desired elements. // `Read` function of the api package returns a portion of
// a slice. The main function [main()] below uses the
// Read function.
// //
// Another programmer has decided to use your package. // `main()` appends to the slice but doing so changes the
// She calls: `api.Read(0, 3)` and gets a slice with a length of 3. // backing array of the api package's temps slice as well.
// We don't want that.
// //
// However: She appends to the slice, so she practically changes // Only allow `main()` to change the part of the slice
// the elements in the api package's `temps` slice (the original one). // it receives from the Read function.
//
// Only allow her to change the first three elements.
// Prevent her from changing the rest of the elements of
// `api.temps` slice.
// //
// //
// BE CAREFUL // NOTE
// //
// This code imports the api package from the learn go programming // You need to import the api package.
// 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
// //
// //
// CURRENT // CURRENT
@ -57,13 +43,14 @@ import (
// api.temps : [5 10 3 1 3 80 90] // api.temps : [5 10 3 1 3 80 90]
// main.temps : [5 10 3 1 3] // main.temps : [5 10 3 1 3]
// ^ ^ append changes the api package's // ^ ^ append changes the api package's
// temps slice: [1 3] // temps slice's backing array.
// //
// //
// //
// EXPECTED // EXPECTED
// //
// Now the program cannot change the API's original backing array. // The corrected api package does not allow the `main()` change
// the api package's temps slice's backing array.
// | | // | |
// v v // v v
// api.temps : [5 10 3 25 45 80 90] // api.temps : [5 10 3 25 45 80 90]
@ -72,17 +59,15 @@ import (
// --------------------------------------------------------- // ---------------------------------------------------------
func main() { func main() {
// DO NOT TOUCH THE FOLLOWING CODE // DO NOT CHANGE ANYTHING IN THIS CODE.
// THIS IS THE CLIENT PROGRAM THAT USES YOUR API
// YOU CANNOT CONTROL IT! :)
// get the first three elements from api.temps // get the first three elements from api.temps
temps := api.Read(0, 3) slice := api.Read(0, 3)
// changes the api.temps's backing array. // append changes the api package's temps slice's
// you need to prevent this. // backing array as well.
temps = append(temps, []int{1, 3}...) slice = append(slice, []int{1, 3}...)
fmt.Println("api.temps :", api.All()) fmt.Println("api.temps :", api.All())
fmt.Println("main.temps :", temps) fmt.Println("main.slice :", slice)
} }

View File

@ -1,24 +1,22 @@
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 slice of elements from the temps slice.
// the `start` until to the `stop`.
func Read(start, stop int) []int { func Read(start, stop int) []int {
// //
// This third index prevents the clients of this package from // The third index prevents the `main()` from
// overwriting the original temps slice's backing array. It // overwriting the original temps slice's
// limits the capacity of the returned slice. See the // backing array. It limits the capacity of the
// full slice expressions lecture. // returned slice. See the full slice expressions
// ^ // lecture for more details.
// | //
portion := temps[start:stop:stop] portion := temps[start:stop:stop]
return portion return portion
} }
// All returns all the temperature readings // All returns the temps slice
func All() []int { func All() []int {
return temps return temps
} }

View File

@ -14,9 +14,10 @@ import (
) )
func main() { func main() {
temps := api.Read(0, 3) slice := api.Read(0, 3)
temps = append(temps, []int{1, 3}...)
slice = append(slice, []int{1, 3}...)
fmt.Println("api.temps :", api.All()) fmt.Println("api.temps :", api.All())
fmt.Println("main.temps :", temps) fmt.Println("main.slice :", slice)
} }