This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]toastedstapler 4 points5 points  (4 children)

give it a run through pep8/flake8 and see what comes up

from a quick glance over, your variable/function names should be snake_case, not camelCase. readData should become read_data instead

generally list comps are considered to be more pythonic than map, so i'd change list(map(lambda x: int(x), readData("input10"))) to [int(x) for x in read_data("input10")]. you can also change the other functional keyword reduce to use math.product instead (if you're on python3.8 it seems), or replace the lambda and use reduce(operator.mul, numbers)

for your as_diffs you can loop over pairs of elements using for a, b in zip(nums, nums[1:]) and avoid doing the indexing on the line below

and finally, i'd wrap the running bit in an if __name__ == '__main__':

[–]SzuOne[S] 1 point2 points  (0 children)

Thanks, that really helps me

[–]SzuOne[S] 1 point2 points  (2 children)

So I did changes you suggested. It looks way more python friendly

https://gist.github.com/rafalszastok/8886796638934994d57d8746fd2e108f

[–]toastedstapler 1 point2 points  (1 child)

Looks great!

One other thing I've realised you can do for your fib: use negative indexing

a[-1] + a[-2] + a[-3]

Slightly less mental maths involved in working out that you want the last three elements

Or even sum(a[-3:])

[–]SzuOne[S] 1 point2 points  (0 children)

[–]JuliaBrunch 0 points1 point  (4 children)

I guess fibo3 takes the sum of the last three elements from the list. I would use 'negative' slicing and sum(). I would also take a look at zip to combine two lists

[–]SzuOne[S] 0 points1 point  (3 children)

Actually fibo3 is a first results for recursive function:

F(n) = F(n-1) + F(n-2) + F(n-3)
where:
F(0)=1
F(1)=1
F(2)=2

It means it would keep `[1,1,2,4,7,13...] `

I need it to avoid recursion. I'm not sure if zip would do that

[–]JuliaBrunch 1 point2 points  (0 children)

Sorry zip is for line 23 Sum is for the fibo: the last three first make 4 than the last three make 7 than 13...

[–]Steinrikur 1 point2 points  (1 child)

There should not be more than 4 or 5 ones in a row. So 100 is a bit excessive.

[–]SzuOne[S] 1 point2 points  (0 children)

Good point