update: slice exercises 23
This commit is contained in:
@ -1,32 +1,21 @@
|
||||
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}
|
||||
|
||||
// ^ ^ ^ ^ ^
|
||||
// | | | +-----------+
|
||||
// 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`.
|
||||
// Read returns a slice of elements from the temps slice.
|
||||
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]
|
||||
|
||||
// RESTRICTIONS — ONLY ADD YOUR CODE IN THIS AREA
|
||||
// ----------------------------------------
|
||||
|
||||
return portion
|
||||
}
|
||||
|
||||
// All returns all the temperature readings
|
||||
// All returns the temps slice
|
||||
func All() []int {
|
||||
return temps
|
||||
}
|
||||
|
@ -16,38 +16,24 @@ import (
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Limit the backing array sharing
|
||||
//
|
||||
// You've created a package and saved the temperatures in an int slice.
|
||||
// (find it in the api folder).
|
||||
// In this exercise: API means the api package. It's in the
|
||||
// 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.
|
||||
// She calls: `api.Read(0, 3)` and gets a slice with a length of 3.
|
||||
// `main()` appends to the slice but doing so changes the
|
||||
// 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
|
||||
// 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.
|
||||
// Only allow `main()` to change the part of the slice
|
||||
// it receives from the Read function.
|
||||
//
|
||||
//
|
||||
// BE CAREFUL
|
||||
// NOTE
|
||||
//
|
||||
// This code imports the api package from the learn go programming
|
||||
// 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
|
||||
// You need to import the api package.
|
||||
//
|
||||
//
|
||||
// CURRENT
|
||||
@ -57,13 +43,14 @@ import (
|
||||
// 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]
|
||||
// temps slice's backing array.
|
||||
//
|
||||
//
|
||||
//
|
||||
// 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
|
||||
// api.temps : [5 10 3 25 45 80 90]
|
||||
@ -72,17 +59,15 @@ import (
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DO NOT TOUCH THE FOLLOWING CODE
|
||||
// THIS IS THE CLIENT PROGRAM THAT USES YOUR API
|
||||
// YOU CANNOT CONTROL IT! :)
|
||||
// DO NOT CHANGE ANYTHING IN THIS CODE.
|
||||
|
||||
// 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.
|
||||
// you need to prevent this.
|
||||
temps = append(temps, []int{1, 3}...)
|
||||
// append changes the api package's temps slice's
|
||||
// backing array as well.
|
||||
slice = append(slice, []int{1, 3}...)
|
||||
|
||||
fmt.Println("api.temps :", api.All())
|
||||
fmt.Println("main.temps :", temps)
|
||||
fmt.Println("main.slice :", slice)
|
||||
}
|
||||
|
@ -1,24 +1,22 @@
|
||||
package api
|
||||
|
||||
// The original temperatures slice.
|
||||
var temps = []int{5, 10, 3, 25, 45, 80, 90}
|
||||
|
||||
// Read returns a range of temperature readings beginning from
|
||||
// the `start` until to the `stop`.
|
||||
// Read returns a slice of elements from the temps slice.
|
||||
func Read(start, stop int) []int {
|
||||
//
|
||||
// This third index prevents the clients of this package from
|
||||
// overwriting the original temps slice's backing array. It
|
||||
// limits the capacity of the returned slice. See the
|
||||
// full slice expressions lecture.
|
||||
// ^
|
||||
// |
|
||||
// The third index prevents the `main()` from
|
||||
// overwriting the original temps slice's
|
||||
// backing array. It limits the capacity of the
|
||||
// returned slice. See the full slice expressions
|
||||
// lecture for more details.
|
||||
//
|
||||
portion := temps[start:stop:stop]
|
||||
|
||||
return portion
|
||||
}
|
||||
|
||||
// All returns all the temperature readings
|
||||
// All returns the temps slice
|
||||
func All() []int {
|
||||
return temps
|
||||
}
|
||||
|
@ -14,9 +14,10 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
temps := api.Read(0, 3)
|
||||
temps = append(temps, []int{1, 3}...)
|
||||
slice := api.Read(0, 3)
|
||||
|
||||
slice = append(slice, []int{1, 3}...)
|
||||
|
||||
fmt.Println("api.temps :", api.All())
|
||||
fmt.Println("main.temps :", temps)
|
||||
fmt.Println("main.slice :", slice)
|
||||
}
|
||||
|
Reference in New Issue
Block a user