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 (
|
2019-08-18 14:03:08 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-03-05 23:32:32 +03:00
|
|
|
s "github.com/inancgumus/prettyslice"
|
|
|
|
)
|
|
|
|
|
2019-08-18 14:03:08 +03:00
|
|
|
// ---------------------------------------------------------
|
|
|
|
// EXERCISE: Add a newline after each sentence
|
2019-03-06 01:14:01 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// You have a slice that contains Beatles' awesome song:
|
|
|
|
// Yesterday. You want to add newlines after each sentence.
|
2019-03-06 01:14:01 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// So, create a new slice and copy every words into it. Lastly,
|
|
|
|
// after each sentence, add a newline character ('\n').
|
2019-03-06 01:14:01 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-23 10:19:50 +03:00
|
|
|
// ORIGINAL SLICE:
|
|
|
|
//
|
2021-05-01 13:21:56 +03:00
|
|
|
// [yesterday all my troubles seemed so far away now it looks as though they are here to stay oh I believe in yesterday]
|
2019-08-23 10:19:50 +03:00
|
|
|
//
|
|
|
|
// EXPECTED SLICE (NEW):
|
|
|
|
//
|
2021-05-01 13:21:56 +03:00
|
|
|
// [yesterday all my troubles seemed so far \n away now it looks as though they are here to stay \n oh I believe in yesterday \n]
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// CURRENT OUTPUT
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2021-05-01 13:21:56 +03:00
|
|
|
// yesterday all my troubles seemed so far away now it looks as though they are here to stay oh I believe in yesterday
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// EXPECTED OUTPUT
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// yesterday all my troubles seemed so far away
|
|
|
|
// now it looks as though they are here to stay
|
2021-05-01 13:21:56 +03:00
|
|
|
// oh I believe in yesterday
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// RESTRICTIONS
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// + Don't use `append()`, use `copy()` instead.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// + Don't cheat like this:
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// fmt.Println(lyric[:8])
|
|
|
|
// fmt.Println(lyric[8:18])
|
|
|
|
// fmt.Println(lyric[18:23])
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// + Create a new slice that contains the sentences
|
|
|
|
// with line endings.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// NOTE
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// If the program does not work, please update your
|
|
|
|
// local copy of the prettyslice package:
|
|
|
|
//
|
|
|
|
// go get -u github.com/inancgumus/prettyslice
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 15:32:48 +03:00
|
|
|
//
|
2019-03-05 23:32:32 +03:00
|
|
|
// ---------------------------------------------------------
|
|
|
|
|
|
|
|
func main() {
|
2019-08-18 14:03:08 +03:00
|
|
|
// You need to add a newline after each sentence in another slice.
|
|
|
|
// Don't touch the following code.
|
2021-05-01 13:21:56 +03:00
|
|
|
lyric := strings.Fields(`yesterday all my troubles seemed so far away now it looks as though they are here to stay oh I believe in yesterday`)
|
2019-03-05 23:32:32 +03:00
|
|
|
|
2019-08-18 14:03:08 +03:00
|
|
|
// ===================================
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// ~~~ CHANGE THIS CODE ~~~
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
fix := lyric
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// ===================================
|
2019-03-05 23:32:32 +03:00
|
|
|
|
2019-08-18 14:03:08 +03:00
|
|
|
// Currently, it prints every sentence on the same line.
|
|
|
|
// Don't touch the following code.
|
|
|
|
s.Show("fix slice", fix)
|
2019-03-05 23:32:32 +03:00
|
|
|
|
2019-08-18 14:03:08 +03:00
|
|
|
for _, w := range fix {
|
|
|
|
fmt.Print(w)
|
|
|
|
if w != "\n" {
|
|
|
|
fmt.Print(" ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 23:32:32 +03:00
|
|
|
|
2019-08-18 14:03:08 +03:00
|
|
|
func init() {
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// YOU DON'T NEED TO TOUCH THIS
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// This initializes some options for the prettyslice package.
|
|
|
|
// You can change the options if you want.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// This code runs before the main function above.
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
// s.Colors(false) // if your editor is light background color then enable this
|
2019-03-05 23:32:32 +03:00
|
|
|
//
|
2019-08-18 14:03:08 +03:00
|
|
|
s.PrintBacking = true // prints the backing arrays
|
|
|
|
s.MaxPerLine = 5 // prints max 15 elements per line
|
|
|
|
s.SpaceCharacter = '⏎' // print this instead of printing a newline (for debugging)
|
2019-03-05 23:32:32 +03:00
|
|
|
}
|