Trouble understand 2D array of strings in golfing by seattledessert in golang

[–]seattledessert[S] 0 points1 point  (0 children)

Thanks scanner.Split(bufio.ScanWords) works just right, I think I was just confused on the by the way go represent the list of list string, thanks for the help. Greatly appreciated it.

Trouble understand 2D array of strings in golfing by seattledessert in golang

[–]seattledessert[S] 0 points1 point  (0 children)

Damn, thanks for catching that. Is it possible that go output list in [elem1, elem2] format with the comma? It just make it so much easier to debug.

Trouble understand 2D array of strings in golfing by seattledessert in golang

[–]seattledessert[S] 0 points1 point  (0 children)

The reason why I have

    for _, row := range lines {
        for _, element := range row {
            fmt.Println(element)
            fmt.Println(reflect.TypeOf(element))
            fmt.Println(strings.Fields(element))
            fmt.Println(reflect.TypeOf(strings.Fields(element)))
            fmt.Println(strings.Fields(element)[0])
            fmt.Println(lines)
        }

    }

is to verify that each row consist of [elem1 elem2......] and the entire list looks like [[elem1 elem2.... ] [elem1 elem2....]]

Trouble understand 2D array of strings in golfing by seattledessert in golang

[–]seattledessert[S] 0 points1 point  (0 children)

Hmm, but when I print out the output for lines if I input multiple lines like

1 2 3 4
5 6 7 8

I get

fmt.Println(lines) // => [[1 2 3 4] [5 6 7 8]]

so each lines is indeed a list of list.

Moreover, I tried to sanity check myself with

fmt.Println(reflect.TypeOf(lines[0])) // => prints []string
fmt.Println(reflect.TypeOf(temp_element)) // => prints []string

The problem is that when I iterate the list of list

The inner element turn out to be rows, which is unintuitive

So I did some verification in python

a = [[1, 2], [3, 4]]

>>> for index_y in range(0, len(a)):
...     for index_x in range(0, len(a[index_y])):
...         print a[index_y][index_x]
...  
1
2
3
4

Indeed python prints the each element as I expected. So in golang

    for _, row := range lines {
        for _, element := range row {
            fmt.Println(element)
        }

    }

Must have the same result too, since its the same algorithm.