refactor: slices 25th exercise
This commit is contained in:
@ -1,47 +1,31 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
s "github.com/inancgumus/prettyslice"
|
s "github.com/inancgumus/prettyslice"
|
||||||
)
|
)
|
||||||
|
|
||||||
//
|
|
||||||
// ! NOTE If the program does not work, please update your
|
|
||||||
// local copy of the prettyslice package:
|
|
||||||
//
|
|
||||||
// go get -u github.com/inancgumus/prettyslice
|
|
||||||
//
|
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
// EXERCISE: Add newlines to the lyric sentences
|
// EXERCISE: Add a newline after each sentence
|
||||||
//
|
//
|
||||||
// You have a slice of lyrics data of Beatles' awesome
|
// You have a slice that contains Beatles' awesome song:
|
||||||
// song: Yesterday. Your goal is adding newlines after
|
// Yesterday. You want to add newlines after each sentence.
|
||||||
// each sentence of the lyric.
|
|
||||||
//
|
//
|
||||||
// Your goal is creating a new slice, then copying
|
// So, create a new slice and copy every words into it. Lastly,
|
||||||
// each sentence of the lyric to the new buffer, then
|
// after each sentence, add a newline character ('\n').
|
||||||
// after each sentence adding a newline character.
|
|
||||||
//
|
//
|
||||||
// You will get across something like this all the time
|
// ORIGINAL SLICE:
|
||||||
// in your gopher life. Believe me, learning how to
|
// [yesterday all my troubles seemed so far away now it looks as though they are here to stay oh i believe in yesterday]
|
||||||
// solve this exercise will make you a better hopher.
|
//
|
||||||
|
// EXPECTED SLICE (NEW):
|
||||||
|
// [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]
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// RESTRICTIONS
|
// CURRENT OUTPUT
|
||||||
//
|
|
||||||
// . For warming-up, in this exercise, never use the `append()` func.
|
|
||||||
//
|
|
||||||
// . Instead, only use the `copy()` func.
|
|
||||||
//
|
|
||||||
// . Don't use slicing for printing the sentences.
|
|
||||||
//
|
|
||||||
// . Instead, use slicing for filling the new buffer.
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// STEPS
|
|
||||||
//
|
|
||||||
// . Please follow the instructions inside the code.
|
|
||||||
//
|
//
|
||||||
|
// yesterday all my troubles seemed so far away now it looks as though they are here to stay oh i believe in yesterday
|
||||||
//
|
//
|
||||||
// EXPECTED OUTPUT
|
// EXPECTED OUTPUT
|
||||||
//
|
//
|
||||||
@ -49,79 +33,67 @@ import (
|
|||||||
// now it looks as though they are here to stay
|
// now it looks as though they are here to stay
|
||||||
// oh i believe in yesterday
|
// oh i believe in yesterday
|
||||||
//
|
//
|
||||||
|
//
|
||||||
|
// RESTRICTIONS
|
||||||
|
//
|
||||||
|
// + Don't use `append()`, use `copy()` instead.
|
||||||
|
//
|
||||||
|
// + Don't cheat like this:
|
||||||
|
//
|
||||||
|
// fmt.Println(lyric[:8])
|
||||||
|
// fmt.Println(lyric[8:18])
|
||||||
|
// fmt.Println(lyric[18:23])
|
||||||
|
//
|
||||||
|
// + Create a new slice that contains the sentences
|
||||||
|
// with line endings.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// NOTE
|
||||||
|
//
|
||||||
|
// If the program does not work, please update your
|
||||||
|
// local copy of the prettyslice package:
|
||||||
|
//
|
||||||
|
// go get -u github.com/inancgumus/prettyslice
|
||||||
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// You need to add a newline after each sentence in another slice.
|
||||||
|
// Don't touch the following code.
|
||||||
|
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`)
|
||||||
|
|
||||||
|
// ===================================
|
||||||
|
//
|
||||||
|
// ~~~ CHANGE THIS CODE ~~~
|
||||||
|
//
|
||||||
|
fix := lyric
|
||||||
|
//
|
||||||
|
// ===================================
|
||||||
|
|
||||||
|
// Currently, it prints every sentence on the same line.
|
||||||
|
// Don't touch the following code.
|
||||||
|
s.Show("fix slice", fix)
|
||||||
|
|
||||||
|
for _, w := range fix {
|
||||||
|
fmt.Print(w)
|
||||||
|
if w != "\n" {
|
||||||
|
fmt.Print(" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
//
|
//
|
||||||
// YOU DON'T NEED TO TOUCH THIS
|
// YOU DON'T NEED TO TOUCH THIS
|
||||||
//
|
//
|
||||||
// This inits some options for the prettyslice package.
|
// This initializes some options for the prettyslice package.
|
||||||
// You can change the options if you want.
|
// You can change the options if you want.
|
||||||
//
|
//
|
||||||
|
// This code runs before the main function above.
|
||||||
|
//
|
||||||
// s.Colors(false) // if your editor is light background color then enable this
|
// s.Colors(false) // if your editor is light background color then enable this
|
||||||
|
//
|
||||||
s.PrintBacking = true // prints the backing arrays
|
s.PrintBacking = true // prints the backing arrays
|
||||||
s.MaxPerLine = 15 // prints max 15 elements per line
|
s.MaxPerLine = 5 // prints max 15 elements per line
|
||||||
s.SpaceCharacter = '*' // print this instead of printing a newline (for debugging)
|
s.SpaceCharacter = '⏎' // print this instead of printing a newline (for debugging)
|
||||||
|
|
||||||
//
|
|
||||||
// UNCOMMENT THE VARIABLE BELOW THEN START!
|
|
||||||
//
|
|
||||||
|
|
||||||
// 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`)
|
|
||||||
|
|
||||||
//
|
|
||||||
// RESTRICTION EXPLANATION:
|
|
||||||
//
|
|
||||||
// Do not use slicing for printing the sentences.
|
|
||||||
//
|
|
||||||
// For example, don't do something like this:
|
|
||||||
//
|
|
||||||
// fmt.Println(lyric[:8])
|
|
||||||
// fmt.Println(lyric[8:18])
|
|
||||||
// fmt.Println(lyric[18:23])
|
|
||||||
|
|
||||||
// ========================================================================
|
|
||||||
//
|
|
||||||
// #1: CREATE A LARGE ENOUGH BUFFER (A NEW SLICE)
|
|
||||||
//
|
|
||||||
// You need to put each lyric sentence
|
|
||||||
// + a newline character into this buffer
|
|
||||||
//
|
|
||||||
|
|
||||||
// I name the buffer: `fix`, you can name it however you want
|
|
||||||
// fix := make(...)
|
|
||||||
|
|
||||||
// ========================================================================
|
|
||||||
//
|
|
||||||
// #2: CALCULATE THE CUT POINTS
|
|
||||||
//
|
|
||||||
// You want to put newlines after each sentence. So you may want to put
|
|
||||||
// these index positions into a slice. Then you can use them to cut the
|
|
||||||
// lyric slice.
|
|
||||||
//
|
|
||||||
|
|
||||||
// cutpoints := []int{...}
|
|
||||||
|
|
||||||
// ========================================================================
|
|
||||||
//
|
|
||||||
// #3: CREATE A LOOP AND COPY THE SENTENCES INTO THE BUFFER
|
|
||||||
//
|
|
||||||
|
|
||||||
// for ... {
|
|
||||||
// Use the `copy` function to copy from the `lyric` slice to the buffer.
|
|
||||||
//
|
|
||||||
// Copy a newline character (in a string) to the buffer after each sentence.
|
|
||||||
//
|
|
||||||
// You can use slicing here for filling the new buffer.
|
|
||||||
//
|
|
||||||
// Uncomment this to aid debugging (to see how the fix slice changes)
|
|
||||||
// s.Show("fix slice", fix)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// ========================================================================
|
|
||||||
//
|
|
||||||
// #4: PRINT THE BUFFER
|
|
||||||
//
|
|
||||||
|
|
||||||
// ...
|
|
||||||
}
|
}
|
||||||
|
@ -14,58 +14,45 @@ import (
|
|||||||
s "github.com/inancgumus/prettyslice"
|
s "github.com/inancgumus/prettyslice"
|
||||||
)
|
)
|
||||||
|
|
||||||
//
|
|
||||||
// ! NOTE If the program does not work, please update your
|
|
||||||
// local copy of the prettyslice package:
|
|
||||||
//
|
|
||||||
// go get -u github.com/inancgumus/prettyslice
|
|
||||||
//
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//
|
|
||||||
// YOU DON'T NEED TO TOUCH THIS
|
|
||||||
//
|
|
||||||
// This inits some options for the prettyslice package.
|
|
||||||
// You can change the options if you want.
|
|
||||||
//
|
|
||||||
// s.Colors(false) // if your editor is light colored then enable this
|
|
||||||
s.PrintBacking = true // prints the backing arrays
|
|
||||||
s.MaxPerLine = 15 // prints max 15 elements per line
|
|
||||||
s.SpaceCharacter = '⏎' // print this instead of printing a newline (for debugging)
|
|
||||||
|
|
||||||
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`)
|
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`)
|
||||||
|
|
||||||
// CREATE A LARGE ENOUGH BUFFER
|
// `+3` because we're going to add 3 newline characters to the fix slice.
|
||||||
// `+3` because we're going to add 3 newline characters
|
|
||||||
fix := make([]string, len(lyric)+3)
|
fix := make([]string, len(lyric)+3)
|
||||||
|
|
||||||
// CALCULATE THE CUT POINTS
|
|
||||||
//
|
//
|
||||||
// + The first sentence has 8 words so its cutpoint is 8.
|
// USE A SLICE TO STORE WHERE EACH SENTENCE ENDS
|
||||||
//
|
//
|
||||||
// + The second one has 10 words so its cutpoint is 10.
|
// + The first sentence has 8 words so its cutting index is 8.
|
||||||
//
|
//
|
||||||
// + The third one has 5 words so its cutpoint is 5.
|
// yesterday all my troubles seemed so far away now it looks as though they are here to stay
|
||||||
|
// |
|
||||||
|
// v
|
||||||
|
// cutting index: 8
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// yesterday all my troubles seemed so far away now it looks as though they are here to stay
|
// + The second sentence has 10 words so its cutting index is 10.
|
||||||
// |
|
|
||||||
// v
|
|
||||||
// cutpoint: 8
|
|
||||||
//
|
//
|
||||||
// ... now it looks as though they are here to stay oh i believe in yesterday
|
// now it looks as though they are here to stay oh i believe in yesterday
|
||||||
// |
|
// |
|
||||||
// v
|
// v
|
||||||
// 10
|
// cutting index: 10
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// + The last sentence has 5 words so its cutting index is 5.
|
||||||
|
//
|
||||||
|
// oh i believe in yesterday
|
||||||
|
// |
|
||||||
|
// v
|
||||||
|
// cutting index: 5
|
||||||
//
|
//
|
||||||
// ... now it looks as though they are here to stay oh i believe in yesterday
|
|
||||||
// |
|
|
||||||
// v
|
|
||||||
// 5
|
|
||||||
cutpoints := []int{8, 10, 5}
|
cutpoints := []int{8, 10, 5}
|
||||||
|
|
||||||
// `n` tracks how much we've moved inside the `lyric` slice
|
//
|
||||||
// `i` tracks the sentence that we're on
|
// `n` tracks how much we've moved inside the `lyric` slice.
|
||||||
|
//
|
||||||
|
// `i` tracks the sentence that we're on.
|
||||||
|
//
|
||||||
for i, n := 0, 0; n < len(lyric); i++ {
|
for i, n := 0, 0; n < len(lyric); i++ {
|
||||||
//
|
//
|
||||||
// copy to `fix` from the `lyric`
|
// copy to `fix` from the `lyric`
|
||||||
@ -81,16 +68,21 @@ func main() {
|
|||||||
//
|
//
|
||||||
n += copy(fix[n+i:], lyric[n:n+cutpoints[i]])
|
n += copy(fix[n+i:], lyric[n:n+cutpoints[i]])
|
||||||
|
|
||||||
// add a newline after the number of copied elements
|
//
|
||||||
// notice that the '\n' position slides as we move over
|
// add a newline after each sentence.
|
||||||
// that's why n+i
|
//
|
||||||
|
// notice that the '\n' position slides as we move over.
|
||||||
|
// that's why it's: `n+i`.
|
||||||
|
//
|
||||||
fix[n+i] = "\n"
|
fix[n+i] = "\n"
|
||||||
|
|
||||||
// uncomment this to aid debugging (to see how the fix slice changes)
|
// uncomment to see how the fix slice changes.
|
||||||
// s.Show("fix slice", fix)
|
// s.Show("fix slice", fix)
|
||||||
}
|
}
|
||||||
|
|
||||||
// print the fix slice
|
s.Show("fix slice", fix)
|
||||||
|
|
||||||
|
// print the sentences
|
||||||
for _, w := range fix {
|
for _, w := range fix {
|
||||||
fmt.Print(w)
|
fmt.Print(w)
|
||||||
if w != "\n" {
|
if w != "\n" {
|
||||||
@ -98,3 +90,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
//
|
||||||
|
// YOU DON'T NEED TO TOUCH THIS
|
||||||
|
//
|
||||||
|
// This initializes some options for the prettyslice package.
|
||||||
|
// You can change the options if you want.
|
||||||
|
//
|
||||||
|
// This code runs before the main function above.
|
||||||
|
//
|
||||||
|
// s.Colors(false) // if your editor is light background color then enable this
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
@ -86,7 +86,7 @@ Please update your local copy of the prettyslice package for some examples to wo
|
|||||||
|
|
||||||
A slice retrieved from an API causes a memory leak in your program. You need to fix it.
|
A slice retrieved from an API causes a memory leak in your program. You need to fix it.
|
||||||
|
|
||||||
4. **[Add newlines to the lyric sentences](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/25-add-lines)**
|
4. **[Add a newline after each sentence](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/25-add-lines)**
|
||||||
|
|
||||||
Use the power of the `copy()` function and add newlines into a new buffer from a string slice. This exercise is more tricky than you might think.
|
Use the power of the `copy()` function and add newlines into a new buffer from a string slice. This exercise is more tricky than you might think.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user