Online judge showing wrong answer for simple palindrome program


(Chigozirim C ) #1

This is my code:

package main

import (
	"fmt"
)

func isPalindrome(value *string) bool {
	for i, j := 0, len(*value)-1; i < j; i, j = i+1, j-1 {
		if (*value)[i] != (*value)[j] {
			return false
		}
	}
	return true
}

func main() {
	var n, count int16
	var num string
	for fmt.Scanf("%v", &n); n > 0; n-- {
		fmt.Scanln(&num)
		if isPalindrome(&num) {
			count++
		}
	}
	fmt.Println(count)
}

It’s a simple palindrome problem from https://codeforces.com/gym/102416/problem/A
The problem is that online judge is showing that the above code produces the wrong answer for the input:

4
3
546
74647
74565

Try it online!

The online judge result:

This is really strange because when I run the code using the above input, I get the right answer which is 2, but when the online judge runs it, it gives 3. I am using go version 1.13 but the online judge is running version 1.12. Is there some difference between the two versions of go that might be causing this issue? Or is this a problem with the online judge?


(Karl Benedict) #2
  • Throw some \n’s into your format string for Scanf.
  • Rethink the function (why the pointer? simplify the loop, no need for two variables there).

(Chigozirim C ) #3

This won’t be the first time, I’ve encountered a problem with the online judge saying something is wrong without showing what is actually wrong with the output, or in this case showing a total different output from what should have printed.