Writing a shorter, readable, pythonic code

I am trying to create a shorter, more python, readable python. And I have this working solution for Project Euler problem 8 (find the largest product of 5 consecutive digits in a 1000-digit number).

Suggestions for writing a more pythonic version of this script?

numstring = ''
for line in open('8.txt'):
    numstring += line.rstrip()

nums = [int(x) for x in numstring]

best=0
for i in range(len(nums)-4):
    subset = nums[i:i+5]
    product=1
    for x in subset:
        product *= x
    if product>best:
        best=product
        bestsubset=subset

print best
print bestsubset

For example: for the fragment below, there must be one line. I am sure there is a topic here, but I am not sure how to describe what I am doing below.

numstring = ''
for line in open('8.txt'):
    numstring += line.rstrip()

Any suggestions? Thanks guys!

+5
source share
5 answers

Here is my solution. I tried to write the most "Pythonic" code that I know how to write.

with open('8.txt') as f:
    numstring = f.read().replace('\n', '')

nums = [int(x) for x in numstring]

def sub_lists(lst, length):
    for i in range(len(lst) - (length - 1)):
        yield lst[i:i+length]

def prod(lst):
    p = 1
    for x in lst:
        p *= x
    return p

best = max(prod(lst) for lst in sub_lists(nums, 5))
print(best)

, reduce, , , prod() :

# from functools import reduce   # uncomment this line for Python 3.x
from operator import mul
def prod(lst):
    return reduce(mul, lst, 1)

, . with, -. , PyPy -, , . with, .

@Steven Rumbalski:

nums = [int(c) for c in open('8.txt').read() if c.isdigit()]

, , , :

with open("8.txt") as f:
    nums = [int(ch) for ch in f.read() if ch.isdigit()]

, , , , ; with.

+1

,

numstring = ''.join(x.rstrip() for x in open('8.txt'))

: ! . .

from operator import mul
def prod(list):
    return reduce(mul, list)

numstring = ''.join(x.rstrip() for x in open('8.txt'))
nums = [int(x) for x in numstring]
print max(prod(nums[i:i+5]) for i in range(len(nums)-4))
+4
from operator import mul

def product(nums):
    return reduce(mul, nums)

nums = [int(c) for c in open('8.txt').read() if c.isdigit()]
result = max((product(nums[i:i+5]) for i in range(len(nums))))
+4

, , string, numstring:

numstring = ''

( string s) txt 8.txt:

for line in open('8.txt'):

, line.rstrip(). rstrip 'strips' (, ..) :

    numstring += line.rstrip()

, , 8.txt, : LineOne \nLyneDeux\t\nLionTree , :

>>>'LineOne' #loop first time
>>>'LineOneLyneDeux' # second time around the bush
>>>'LineOneLyneDeuxLionTree' #final answer, reggie
0

! :

with open("8.txt") as infile:
    number = infile.replace("\n", "")

Then create a list of lists with 5 consecutive numbers:

cons_numbers = [list(map(int, number[i:i+5])) for i in range(len(number) - 4)]

Then find the largest one and type it:

print(max(reduce(operator.mul, nums) for nums in cons_numbers))

If you are using Python 3.x, you need to replace reducewith functools.reduce.

0
source

All Articles