I started working on some of the problems of Project Euler and solved number 4 with a simple solution for brute force:
def mprods(a,b):
c = range(a,b)
f = []
for d in c:
for e in c:
f.append(d*e)
return f
max([z for z in mprods(100,1000) if str(z)==(''.join([str(z)[-i] for i in range(1,len(str(z))+1)]))])
After the solution, I tried to make it as compact as possible and came up with this terrible bottom line!
In order not to leave something halfway, I'm trying to condense a function mprodsin a list comprehension. So far I have come up with these attempts:
[d*e for d,e in (range(a,b), range(a,b))]
Obviously completely on the wrong track. :-)[d*e for x in [e for e in range(1,5)] for d in range(1,5)]
It gives me [4, 8, 12, 16, 4, 8, 12, 16, 4, 8, 12, 16, 4, 8, 12, 16]where I expect
[1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16]or similar.
Any Pythonistas that can help? :)
source
share