2

I am working on Project Euler Problem 8 so please avoid going too far in your answer.

Where is the problem in the code such that when I add the int conversion, the output changes and some groups are no longer 13 digits. This happens multiple times throughout but an easy one to spot is 5th in from the last.

def large_product(n):
    string_n = str(n)
    n = [int(str_n[i:i+13]) for i in range(0, len(str_n), 13)]
    print(n)

What is happening when I add the int conversion that causes the output to change and some groups are no longer 13 digits? This happens multiple times throughout but an easy one to spot is the first below:

5593572972, 5716362695618, 8267042825248, 3600823257530, 420752963450

If I remove the int and get back a list of strings, they are all the correct 13 digits in length but then I cannot convert back to int and will constantly get hit with an "invalid literal for int() with base 10" message.

n = [(str_n[i:i+13]) for i in range(0, len(str_n), 13)]
str_n = str(n)
int_n = int(str_n)
print(int_n)

Returns:

ValueError: invalid literal for int() with base 10: "['7316717653133', '0624919225119', '6744265747423', '5534919493496', '9835203127745', '0632623957831', '8016984801869', '4788518438586', '1560789112949', '4954595017379', '5833195285320', '8805511125

How do you get these groups of numbers in a position to be multiplied together and then compared? Thanks in advance.

Tyler_P
  • 58
  • 10

1 Answers1

4

The problem seems to be with the digits which have 0 as prefixes. When you convert a string to integer which have 0 as prefix, the int conversion will ignore the zeros and print you the whole number.

try this for clarity,

print(int('0084646'))

The error in below block is that you are trying to convert a list into int.

n = [(str_n[i:i+13]) for i in range(0, len(str_n), 13)] # a list is getting generated
str_n = str(n) # "['7316717653133', '0624919225119', '6744265747423', '5534919493496', '9835203127745', '0632623957831', '8016984801869'..] -- '[' and ',' are special characters here
int_n = int(str_n) #converting list to int which will fail actually as special characters are there in string
print(int_n)

as i am not sure about what you are intended to do, i am not sure whether the int() conversion will help you or not.

for printing purpose use the reference here and here.

I would suggest you to go for pandas or numpy instead of core python.

adding list of integers - normal vs numpy

multiplying list of integers - normal vs numpy

SanthoshSolomon
  • 1,383
  • 1
  • 14
  • 25
  • When you say special characters do you mean the commas? Would stripping them allow for converting? – Tyler_P Aug 13 '18 at 05:34
  • i have made some edits in my answer. please refer. And as stated i can't say whether that conversion will help you or not in your purpose. – SanthoshSolomon Aug 13 '18 at 05:42
  • Yes you are right. I am just realizing that. Is there a way to parse an item of integers and perform calculations? E.g. multiply those groups of digits by themselves or is it best to become familiar with NumPy before tackling more problems like this? – Tyler_P Aug 13 '18 at 05:48
  • Though am not familiar with NumPy, i can say that would give you better experience and workaround than core Python. Else, happy headbanging! P.S.: I have added references in answer. – SanthoshSolomon Aug 13 '18 at 05:52