-2

I was doing Problem 8 on Project Euler when I ran into a problem. I couldn't find a way to assign each digit in the thousand digit number its own value. Here is my code:

def Problem8():
    x = []
    num = [73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937765727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450]
    def ans(i):
        one = num[i]
        two = num[i+1]
        three = num[i+2]
        four = num[i+3]
        return one*two*three*four
    for i in range(1,(len(num2)-3)):
        x.append(ans(i))
    print(x)

Problem8()

I was just checking if it would work for the four adjacent digits, not thirteen like the question said. But my array kept on coming up empty. Why is it doing that? Is there a better way?

zondo
  • 19,901
  • 8
  • 44
  • 83

1 Answers1

1

The way you've written your code, doing [<bignumber>] creates a list with only one element, namely the big number in question. In that case, doing num[i] for i > 1 results in nothing.

If you want to create a list with all the digits of the number, consider doing the following:

num = map(int, str(<bignumber>)) # creates a list of digits 

The way this works is that str converts bignumber to a string, while map first converts the string iterable to a list of its individual characters and then converts each character in that list back to an integer, so that you now have an array of digits.

The rest of your analysis can then work without issue.


Other issues:

  • num2 is undefined. It appears nowhere in your code, and should throw an error. I presume it was a typo.
  • Your i only starts from 1, not 0 in your range call. Consider changing this range(0, len(num) - 3) so as not to avoid an off-by-one index error. Even better: use a list comprehension.
  • In Python 2.7, range creates an actual list in memory with 1000 elements. For even larger numbers, this is going to be a massive chunk of memory. Consider doing xrange instead, or - if on Python 3 - do nothing.
  • Lastly, you can compactify the whole output of ans into a single line. No need to create extra variables like one, two, etc.
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
  • Why the downvote? This works perfectly and goes beyond the bare minimum the OP requires. – Akshat Mahajan Apr 02 '16 at 16:57
  • I didn't downvote, but I do have a suggestion. `map()` requires an iterable for its second argument, but it doesn't need to be a list. You can simply use `map(int, str())` – zondo Apr 02 '16 at 17:04
  • You mean `str(bignumber)`? `bignumber` is an int, not iterable, and thus can't be used in `map`. :) But, yes, that's a good suggestion. – Akshat Mahajan Apr 02 '16 at 17:05
  • You caught me before I edited. ;) – zondo Apr 02 '16 at 17:06
  • @zondo Took your suggestions into account, edited my answer. – Akshat Mahajan Apr 02 '16 at 17:07
  • I did what you said, but an error pops up. It says "'map' object is not subscriptable" Why is this happening and what does it mean? – smid_the_best Apr 02 '16 at 19:41
  • In Python 3, `map` returns a generator, not a list. Do `list(map(int,str())`. See [this question](http://stackoverflow.com/questions/6800481/python-map-object-is-not-subscriptable) for advice. – Akshat Mahajan Apr 02 '16 at 19:44
  • This code worked great, but I want to know how to add a value to every two digit number. Meaning how would I do the sam thing except this time with two digits. – smid_the_best Apr 11 '16 at 15:21