add: slice internals exercises

This commit is contained in:
Inanc Gumus
2019-03-04 20:30:18 +03:00
parent fe38be1025
commit 2c7ee6ebf5
14 changed files with 598 additions and 42 deletions

View File

@@ -0,0 +1,45 @@
package main
import (
"fmt"
"strings"
)
// ---------------------------------------------------------
// EXERCISE: Correct the lyric
//
// You have a slice of lyrics data of Beatles' awesome
// song: Yesterday. Your goal is putting the words into
// correct positions.
//
//
// STEPS
//
// 1. Prepend "yesterday" to the `lyric` slice.
//
// 2. Put the words to the correct position in the `lyric` slice.
//
// 3. Print the `lyric` slice.
//
//
// EXPECTED OUTPUT
//
// [yesterday all my troubles seemed so far away now it looks as though they are here to stay oh i believe in yesterday]
//
//
// BONUS
//
// . Think about when does the append allocates a new backing array.
// . Then check whether your conclusions are true or not.
// . You can use the prettyslice package to check the backing array.
//
// ---------------------------------------------------------
func main() {
// DON'T TOUCH THIS:
lyric := strings.Fields(`all my troubles seemed so far away oh i believe in yesterday now it looks as though they are here to stay`)
// ADD YOUR CODE BELOW:
// ...
fmt.Printf("%s\n", lyric)
}

View File

@@ -0,0 +1,79 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"strings"
)
func main() {
// --- Correct Lyric ---
// yesterday all my troubles seemed so far away
// now it looks as though they are here to stay
// oh i believe in yesterday
lyric := strings.Fields(`all my troubles seemed so far away oh i believe in yesterday now it looks as though they are here to stay`)
// ------------------------------------------------------------------------
// #1: Prepend "yesterday" to `lyric`
// ------------------------------------------------------------------------
//
// --- BEFORE ---
// all my troubles seemed so far away oh i believe in yesterday
//
// --- AFTER ---
// yesterday all my troubles seemed so far away oh i believe in yesterday
//
// (warning: allocates a new backing array)
//
lyric = append([]string{"yesterday"}, lyric...)
// ------------------------------------------------------------------------
// #2: Put the words to the correct position in the `lyric` slice.
// ------------------------------------------------------------------------
//
// yesterday all my troubles seemed so far away oh i believe in yesterday
// | |
// v v
// index: 8 pos: 13
//
const N, M = 8, 13
// --- BEFORE ---
//
// yesterday all my troubles seemed so far away oh i believe in yesterday now it looks as though they are here to stay
//
// --- AFTER ---
// yesterday all my troubles seemed so far away oh i believe in yesterday now it looks as though they are here to stay oh i believe in yesterday
// ^
//
// |
// +--- duplicate
//
// (warning: allocates a new backing array)
lyric = append(lyric, lyric[N:M]...)
//
// --- BEFORE ---
// yesterday all my troubles seemed so far away oh i believe in yesterday now it looks as though they are here to stay oh i believe in yesterday
//
// --- AFTER ---
// yesterday all my troubles seemed so far away now it looks as though they are here to stay oh i believe in yesterday
//
// (does not allocate a new backing array because cap(lyric[:N]) > M)
//
lyric = append(lyric[:N], lyric[M:]...)
// ------------------------------------------------------------------------
// #3: Print
// ------------------------------------------------------------------------
fmt.Printf("%s\n", lyric)
}