tester::#AH1 failure

I’m stuck on tester::#AH1.

I couldn’t figure out why the exit status is not 0 as it should be, the bash debugging result in correct exit status:

remote: [compile] Moved ./.codecrafters/run.sh → ./your_program.sh
remote: [compile] Compilation successful.
remote: 
remote: Debug = true
remote: 
remote: [tester::#AH1] Running tests for Stage #AH1 (Decode bencoded lists)
remote: [tester::#AH1] Running ./your_program.sh decode le
remote: [tester::#AH1] Expected output: []
remote: [your_program] Logs from your program will appear here!
remote: [your_program] panic: runtime error: slice bounds out of range [1:0]
remote: [your_program] 
remote: [your_program] goroutine 1 [running]:
remote: [your_program] main.decodeBencode({0x7ffc71067ac8, 0x2})
remote: [your_program]  /app/app/main.go:62 +0x329
remote: [your_program] main.main()
remote: [your_program]  /app/app/main.go:84 +0xa9
remote: [tester::#AH1] Application didn't terminate successfully without errors. Expected 0 as exit code, got: 2
remote: [tester::#AH1] Test failed
remote: 
remote: Try our CLI to run tests faster without Git: https://codecrafters.io/cli
remote: 
remote: View our article on debugging test failures: https://codecrafters.io/debug
remote: 
To https://git.codecrafters.io/8129a07f95ceffdb
   3e236aa..6ba6d92  master -> master

and the bash exit status:

~/Downloads/warroom/Go_Playground/codecrafters-bittorrent-go master 49s
❯ ./your_program.sh decode "l5:helloi52ee"     
Logs from your program will appear here!
["hello","52"]

~/Downloads/warroom/Go_Playground/codecrafters-bittorrent-go master 7s
❯ echo $?
0

And here’s a snippet of my code:

func decodeBencode(bencodedString string) (interface{}, error) {
	if unicode.IsDigit(rune(bencodedString[0])) {
		var firstColonIndex int

		for i := 0; i < len(bencodedString); i++ {
			if bencodedString[i] == ':' {
				firstColonIndex = i
				break
			}
		}

		lengthStr := bencodedString[:firstColonIndex]

		length, err := strconv.Atoi(lengthStr)
		if err != nil {
			return "", err
		}

		return bencodedString[firstColonIndex+1 : firstColonIndex+1+length], nil
	} else if bencodedString[0] == 'i' {
		if (unicode.IsLetter(rune(bencodedString[0]))) && (unicode.IsLetter(rune(bencodedString[len(bencodedString)-1]))) {
			substring := bencodedString[1 : len(bencodedString)-1]
			result_int, err := strconv.Atoi(substring)
			if err != nil {
				return "", err
			}
			return result_int, nil
			//	return "", fmt.Errorf("Only strings are supported at the moment")

		}
		} else {
		var first_colon, second_colon, third_colon int
		var final_list []string
		for i := 0; i < (len(bencodedString) - 1); i++ {
			if (string(bencodedString[i])) == ":" {
				first_colon = i
			} else if (string(bencodedString[i])) == "i" {
				second_colon = i
			} else if (string(bencodedString[i])) == "e" {
				third_colon = i
			}
		}
		string1 := string(bencodedString[first_colon+1 : second_colon])
		string2 := string(bencodedString[second_colon+1 : third_colon])
		final_list = []string{string1, string2}
		return final_list, nil
	}
	return nil, fmt.Errorf("Unsupported bencoded string")
}

Could someone help me to debug this issue?

Hey @karthiksedoc, it is not safe to reply on first_colon, second_colon, and third_colon.

The current error occurs because for the case le there is no colon in the input at all:

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.