1
votes

I have written a function to replace sequence of repeated words by just one word in a slice of strings([]string). I use "range" to loop over the slice and use "append()" to modify the slice. Below is the code:

func RemoveContinuosStrings(input []string) []string {  
        top:=0

        for i,_ := range input {
                if input[i] != input[top] {
                        if top != i-1 {
                                input = append(input[:top+1], input[i:]...)
                        }   
                        top = i 
                }   
        }   
        return input[:top+1]
}

func main() {

        scanner := bufio.NewScanner(os.Stdin)
        fmt.Println("enter text")
        input := make([]string, 0, 0)

        for scanner.Scan() {
                input = append(input, scanner.Text())
        }   

        fmt.Println("INPUT", input)
        input = RemoveContinuosStrings(input)
        fmt.Println("OUTPUT", input)
}

on executing the above code I see following error:

===================================================  
    vignesh@vignesh-ThinkPad-E470 ~/go-book/chapter4 $ ./4_6_eliminate_duplicate_string  
    enter text
    qwe
    asd
    asd
    zxc
    asd
    asd
    INPUT [qwe asd asd zxc asd asd]
    panic: runtime error: index out of range

    goroutine 1 [running]:
    main.RemoveContinuosStrings(0xc420088000, 0x6, 0x8, 0x20, 0x0, 0x0)
        /home/vignesh/go-book/chapter4/4_6_eliminate_duplicate_string.go:11 +0x2a0
    main.main()
        /home/vignesh/go-book/chapter4/4_6_eliminate_duplicate_string.go:33 +0x33a
    vignesh@vignesh-ThinkPad-E470 ~/go-book/chapter4 $ 
=======================================================

So, is t wrong or not allowed to append to the slice being iterated via "range". I saw several examples online where they did append to the slice but were iterating without using "range" (eg: for i=0; i< lenOfSlice; i++). Please help/correct me if I am wrong :)

1

1 Answers

0
votes

When you are changing array size inside a loop, remember to use simple for instead of for range

func RemoveContinuosStrings(input []string) []string {
    top:=0

    for i:=0;i<len(input);i++ {
        if input[i] != input[top] {
            if top != i-1 {
                input = append(input[:top+1], input[i:]...)
                i=top+1
                top=top+1
            }else{
                top = i
            }
        }
    }
    return input[:top+1]
}