A
A
Anton2019-04-23 18:04:55
go
Anton, 2019-04-23 18:04:55

How does fmt.Scan read lines and split elements by int in Golang?

Passing the Diagonal Difference in Go test
My function only passed 2 or 3 tests

spoiler

func diagonalDifference(arr [][]int32) int32 {
var firstDiagonal int32 = 0
var secondDiagonal int32 = 0
for i, slice := range(arr) {
if i == 0 {
continue
}
for j,_ := range(slice) {
if i == 1 && j == 0 {
firstDiagonal = firstDiagonal + arr[i][j]
}
if i == 2 && j == 1 {
firstDiagonal = firstDiagonal + arr[i][j]
}
if i == 3 && j == 2 {
firstDiagonal = firstDiagonal + arr[i][j]
}
if i == 1 && j == 2 {
secondDiagonal = secondDiagonal + arr[i][j]
}
if i == 2 && j == 1 {
secondDiagonal = secondDiagonal + arr[i][j]
}
if i == 3 && j == 0 {
secondDiagonal = secondDiagonal + arr[i][j]
}
}
}
var result int32 = Abs(firstDiagonal - secondDiagonal)
fmt.Println(firstDiagonal)
fmt.Println(secondDiagonal)
return result
}

Found the code https://gist.github.com/ranapu/f9a34a2d0a129259eee...
Launched - all tests passed.
Launched from the console.
I enter lines
3
11 2 4
4 5 6
10 8 -12

How does fmt.Scan read lines and split elements by int in Golang?
In theory, you need to divide the string into elements and get an array from them.
someString := "one two three four"
words := strings.Fields(someString)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Solovyov, 2019-04-23
@Dreddsa

Here is the ioutil
set And yes, if strings are entered, you need to split it, and then translate it into int.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question