[Tutor] Project Euler #8

Dave Angel davea at davea.name
Fri May 31 20:54:10 CEST 2013


On 05/31/2013 02:23 PM, Nick Shemonsky wrote:
> Hey there everybody. I'm new to python

Welcome.  But are you new to programming, or just to Python in 
particular?  And which Python?  I'd guess 2.7

> and am attempting to teach
> myself to code while brushing up on my math skills via the problems at
> projecteuler.net. My solutions thus far have been mostly brute force
> and this is no exception but I'm having an issue with tackling some
> iteration in the problem.

Good approach.

>
> Essentially, the problem is to find the largest product of 5
> consecutive digits in a random series of numbers. I'm trying to get
> things working on a very small string to start... my code thus far is
> below.
>
> I begin by getting a list of strings from the original string, only
> keeping those that are 5 digits long... then those numbers are
> converted to integers and I get the product with the product function.
>   As you can see, I'm currently doing it only for the string of 12345
> stored at strings[0]. If I run that list comprehension as [int(i) for
> i in strings], I get a new list of all the integers I want; 12345,
> 23456, 34567, etc... but I can no longer iterate through to multiply
> them all together...
>
> I feel like I need to either pull each individual string apart first
> and temporarily store it then convert each to an int and get the
> product or instead, just find a way to iterate through the members of
> a member of a list... I've read up on a bunch of stuff regarding
> map(), zip() and enumerate() but none of it seems to be applicable
> here or maybe I'm misunderstanding it.
>
> Once I have a list of products a simple max() should do the trick...
> or maybe it'd be quicker to compare a to b through each iteration and
> just keep the larger product rather than creating a giant list of
> products once I get this step working? Any ideas towards the next step
> would be much appreciated.
>
> str_num = '1234567890'
> n = 5
>
> strings = [str_num[i:i+5] for i in range(0, len(str_num)) if
> len(str_num[i:i+5])==5]

If you changed the range() size, you could eliminate the extra if test. 
  After all, the only ones that'll be short are the last 4.  Also, 
xrange is better than range, if you ever try this on a really big 
dataset.  Good habit to get into, and in Python 3.x, the original range 
is gone, and xrange is called range.

    strings = [str_num[i:i+5] for i in xrange(0, len(str_num-4)) ]

> integers = [int(i) for i in strings[0]]

No clue why you would do this.  Stick with strings.

>
> def product(x):

Put in a docstring about what this function is intended to do.  Then 
maybe you'll realize why it isn't doing it.  I'd assume you wanted it to 
multiply the 5 digits represented by a single substring, and return the 
product.

>      p = 1
>      for n in integers:

You're referencing a global instead of the parameter you were passed.
        for n in x:

>          p*=n

Since n is now a character, like "6"  you want
            p *= int(n)
>      return p
>
> print product(integers)

In your original code, this should have been
   print product(integers[0])

but now you probably want

for substr in strings:
      print substr, product(substr)

And then you modify that loop so that instead of printing, it'll figure 
out the largest one.



-- 
DaveA


More information about the Tutor mailing list