I think the issue has to do with you’re getting your substrings. You’re slicing a multiline string, and so some of your slices will include a newline character instead of a digit. Though your multiplication code will ignore those newlines, they still matter since they change the number of actual digits in the multiplication. If the maximum product you’re trying to find straddles a line boundary (as is likely for a large y
), you won’t find it correctly as you’ll only multiply one fewer digit than the question expects you to.
I suggest you remove the newlines from your string of digits before doing any slicing. The easiest way is probably with i = i.replace('\n', '')
. (You can then also remove the logic to skip over newlines from the mult
function if you want, though it probably doesn’t a actually hurt much to leave it in.)
solved Project Euler # 8- Find biggest product of n adjacent digits in number. Code works only for some values of n [closed]