refactor: slices 25th exercise
This commit is contained in:
@ -14,58 +14,45 @@ import (
|
||||
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() {
|
||||
//
|
||||
// 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`)
|
||||
|
||||
// CREATE A LARGE ENOUGH BUFFER
|
||||
// `+3` because we're going to add 3 newline characters
|
||||
// `+3` because we're going to add 3 newline characters to the fix slice.
|
||||
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
|
||||
// |
|
||||
// v
|
||||
// cutpoint: 8
|
||||
// + The second sentence has 10 words so its cutting index is 10.
|
||||
//
|
||||
// ... now it looks as though they are here to stay oh i believe in yesterday
|
||||
// |
|
||||
// v
|
||||
// 10
|
||||
// now it looks as though they are here to stay oh i believe in yesterday
|
||||
// |
|
||||
// v
|
||||
// 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}
|
||||
|
||||
// `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++ {
|
||||
//
|
||||
// copy to `fix` from the `lyric`
|
||||
@ -81,16 +68,21 @@ func main() {
|
||||
//
|
||||
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
|
||||
// that's why n+i
|
||||
//
|
||||
// add a newline after each sentence.
|
||||
//
|
||||
// notice that the '\n' position slides as we move over.
|
||||
// that's why it's: `n+i`.
|
||||
//
|
||||
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)
|
||||
}
|
||||
|
||||
// print the fix slice
|
||||
s.Show("fix slice", fix)
|
||||
|
||||
// print the sentences
|
||||
for _, w := range fix {
|
||||
fmt.Print(w)
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user