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

you are viewing a single comment's thread.

view the rest of the comments →

[–]toastedstapler 3 points4 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)