Files
learngo/16-slices/exercises/21-correct-the-lyric/main.go
2019-08-23 00:37:56 +03:00

77 lines
2.1 KiB
Go

package main
import (
"fmt"
"strings"
)
// ---------------------------------------------------------
// EXERCISE: Correct the lyric
//
// You have a slice that contains the words of Beatles'
// legendary song: Yesterday. However, the order of the
// words are incorrect.
//
// CURRENT OUTPUT
//
// [all my troubles seemed so far away oh i believe in yesterday now it looks as though they are here to stay]
//
// 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]
//
//
// STEPS
//
// INITIAL SLICE:
// [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 the `lyric` slice.
//
// RESULT SHOULD BE:
// [yesterday all my troubles seemed so far away oh i believe in yesterday now it looks as though they are here to stay]
//
//
// 2. Put the words to the correct positions in the `lyric` slice.
//
// RESULT SHOULD BE:
// [yesterday all my troubles seemed so far away now it looks as though they are here to stay oh i believe in yesterday]
//
//
// 3. Print the `lyric` slice.
//
//
// BONUS
//
// + Think about when does the append allocate a new backing array.
//
// + Check whether your conclusions are correct.
//
//
// HINTS
//
// Only look at this if you get stuck.
//
// You can use the following slice operations to solve the exercise.
//
// + Prepends "value" to the slice:
// slice = append([]string{"value"}, slice...)
//
// + Appends some part (N to M) of the same slice to itself:
// slice = append(slice, slice[N:M]...)
//
// + Copies the last part of the slice starting from M to the first part of the slice until N:
// slice = append(slice[:N], slice[M:]...)
//
// ---------------------------------------------------------
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)
}