New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
panic: runtime error: index out of range , when there are ~ 9k elements in array. #17910
Comments
@ralic the error you are getting seems to come from your code, according to the stack trace ie line2 := strings.Split(lines[1], " ") could you confirm that you actually get more that one line as a sanity check in your code? |
@namKolo, so you found the issue in your code? Does this issue still stand then? @ralic and @namKolo could y'all perhaps reproduce this by making a repository with the inputs that caused the failure? Otherwise we won't be able to diagnose what's going on easily. |
Failed Code for this large array : Working Code for the same input : ------------------------------------------------------------------------ -- On Tue, Nov 15, 2016 at 8:00 PM, Emmanuel T Odeke notifications@github.com
|
@ralic thank you for the repro. --- orig.go 2016-11-16 20:08:58.000000000 -0800
+++ fixed.go 2016-11-16 20:23:51.000000000 -0800
@@ -6,6 +6,7 @@
"os"
"fmt"
"strings"
+ "log"
"strconv"
)
@@ -21,6 +22,10 @@
lines = append(lines, scanner.Text())
}
+ if err := scanner.Err(); err != nil {
+ log.Fatalf("err reading standard input: %v", err)
+ }
+
line1 := strings.Split(lines[0], " ")
line2 := strings.Split(lines[1], " ")
//fmt.Println(line1)
@@ -37,11 +42,6 @@
}
}
- if err := scanner.Err(); err != nil {
- fmt.Fprintln(os.Stderr, "reading standard input:", err)
- }
-
-
// Performance Report
//elapsed := time.Since(start)
//fmt.Println("Time Elapsed:", elapsed) and the example on bufio.Scanner also shows how to catch that error https://golang.org/pkg/bufio/#example_Scanner_lines. To get your program to deal with the multitude of elements that are in the single line, I instead used bufio.NewReader and then Reader.ReadString('\n') like this https://play.golang.org/p/WGfyqmVk79. I don't see anything more that we can do on the Go side, except encourage checking errors early, otherwise this isn't a bug in the Go stdlib packages. |
Hi Emmanuel, Thank you so much for catching my need, I would also start using If the case of fixed size array, it seems to be a good workaround using Sincerely, -- On Wed, Nov 16, 2016 at 8:44 PM, Emmanuel T Odeke notifications@github.com
|
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.7.3
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/usr/local/opt/go/libexec/bin:/usr/local/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.7.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.7.3/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d6/lnrxqp194zn2c1g3_02j1pdh0000gn/T/go-build196018249=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
What did you do?
source code --
http://tinyurl.com/hnzm7nd
What did you expect to see?
https://www.hackerrank.com/challenges/ctci-array-left-rotation
All test case passed.
What did you see instead?
failed in test case 6, 8, 9
[Error Messages]
The text was updated successfully, but these errors were encountered: