2019-10-30 19:34:44 +03:00
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
|
|
|
// For more tutorials : https://learngoprogramming.com
|
|
|
|
// In-person training : https://www.linkedin.com/in/inancgumus/
|
|
|
|
// Follow me on twitter: https://twitter.com/inancgumus
|
|
|
|
|
2019-03-05 23:32:32 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
s "github.com/inancgumus/prettyslice"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
// EXERCISE: Practice advanced slice operations
|
|
|
|
//
|
|
|
|
// Please follow the directions in the following code.
|
|
|
|
//
|
|
|
|
// To see the expected output, please run:
|
|
|
|
//
|
|
|
|
// go run solution/main.go
|
|
|
|
//
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// ########################################################
|
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// #1: Create a string slice: `names` with a length and
|
|
|
|
// capacity of 5, and print it.
|
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
// ...
|
|
|
|
// s.Show("1st step", names)
|
|
|
|
|
|
|
|
// ########################################################
|
|
|
|
//
|
|
|
|
// #2: Append the following names to the names slice:
|
|
|
|
//
|
2021-05-01 13:24:44 +03:00
|
|
|
// "einstein", "tesla", "aristotle"
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
// Print the names slice.
|
|
|
|
//
|
|
|
|
// Observe how the slice and its backing array change.
|
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
// ...
|
|
|
|
// s.Show("2nd step", names)
|
|
|
|
|
|
|
|
// ########################################################
|
|
|
|
//
|
2021-03-31 13:34:16 +03:00
|
|
|
// #3: Overwrite the name slice by creating a new slice
|
|
|
|
// using make().
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2021-03-31 13:34:16 +03:00
|
|
|
// Adjust the make() function so that it creates a
|
|
|
|
// slice with capacity of 5, and puts the slice pointer
|
|
|
|
// to the first index.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2021-03-31 13:34:16 +03:00
|
|
|
// Then append the following names to the slice:
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2021-05-01 13:24:44 +03:00
|
|
|
// "einstein", "tesla", "aristotle"
|
2021-03-31 13:34:16 +03:00
|
|
|
//
|
|
|
|
// Expected output:
|
2021-05-01 13:24:44 +03:00
|
|
|
// ["einstein", "tesla", "aristotle" "" ""]
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
// s.Show("3rd step", names)
|
|
|
|
|
|
|
|
// ########################################################
|
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// #4: Copy only the first two elements of the following
|
2019-03-05 23:49:54 +03:00
|
|
|
// array to the last two elements of the `names` slice.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// Print the names slice, you should see 5 elements.
|
|
|
|
// So, do not forget extending the slice.
|
|
|
|
//
|
|
|
|
// Observe how its backing array stays the same.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Array (uncomment):
|
|
|
|
// moreNames := [...]string{"plato", "khayyam", "ptolemy"}
|
2019-08-18 14:56:37 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
// ...
|
2019-08-18 14:56:37 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
// s.Show("4th step", names)
|
|
|
|
|
|
|
|
// ########################################################
|
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// #5: Only copy the last 3 elements of the `names` slice
|
|
|
|
// to a new slice: `clone`.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// Append the first two elements of the `names` to the
|
|
|
|
// `clone`.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
// Ensure that after appending no new backing array
|
|
|
|
// allocations occur for the `clone` slice.
|
|
|
|
//
|
|
|
|
// Print the clone slice before and after the append.
|
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
// ...
|
|
|
|
// s.Show("5th step (before append)", clone)
|
2019-08-18 14:56:37 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
// ...
|
|
|
|
// s.Show("5th step (after append)", clone)
|
|
|
|
|
|
|
|
// ########################################################
|
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// #6: Slice the `clone` slice between 2nd and 4th (inclusive)
|
|
|
|
// elements into a new slice: `sliced`.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// Append "hypatia" to the `sliced`.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
// Ensure that new backing array allocation "occurs".
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
// Change the 3rd element of the `clone` slice
|
|
|
|
// to "elder".
|
|
|
|
//
|
|
|
|
// Doing so should not change any elements of
|
|
|
|
// the `sliced` slice.
|
|
|
|
//
|
|
|
|
// Print the `clone` and `sliced` slices.
|
|
|
|
//
|
2019-08-18 14:56:37 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
// ...
|
|
|
|
// s.Show("6th step", clone, sliced)
|
|
|
|
}
|
2019-08-22 20:55:47 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Don't mind about this function.
|
|
|
|
//
|
|
|
|
// For printing the slices: You can either use the
|
|
|
|
// prettyslice package or `fmt.Printf`.
|
|
|
|
//
|
|
|
|
func init() {
|
|
|
|
s.PrintBacking = true // prints the backing array
|
|
|
|
s.MaxPerLine = 10 // prints 10 slice elements per line
|
|
|
|
s.Width = 60 // prints 60 character per line
|
|
|
|
}
|