Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iterating over initial array in reverse order. #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

askanium
Copy link
Contributor

No description provided.

@flintforge
Copy link
Contributor

Iterating over an array without modifying it is elementary python.
Doing it efficiently can be considered as a "trick" (or rather, good knowledge of python)

Try the benchmark :

%alias_magic t timeit
L = [x for x in range(3000000)] 
%t for e in reversed(L) : e
%t for e in L[::-1] : e
%t for i in range(len(L)-1, 0, -1): L[i]

also note that reversed can be used with any iterable, not only list

@askanium
Copy link
Contributor Author

askanium commented Jun 10, 2019

Depends on the definition of efficiently.

From a Time Complexity perspective, you are completely right, the solution I suggested is inferior.

From a Space Complexity perspective, both existing solutions take O(n) space, while mine takes O(1) space.

Time Complexity is more important than Space Complexity in most of the cases, as space is cheap, but for some specific cases, knowing how to iterate without duplicating the array might save the day.

Having said this, I don't mind if you close this PR if you think it doesn't bring value, just wanted to explain why I created it in the first place.

@flintforge
Copy link
Contributor

There are many perspectives but, generally speaking, only one acceptable definition :
https://www.lexico.com/en/definition/efficiently

Are you assuming that L[::-1] and reversed(L) are effectively reversing the list ?

Reversed is a type in the Python implementation.

And the function reversed() returns an iterator : https://docs.python.org/3/library/functions.html#reversed,
not a new list.

I think you can improve the example to show that both possibilities exists and that reversed performs faster (which is perfectly logical and without consuming any space)

@askanium
Copy link
Contributor Author

askanium commented Jun 11, 2019

I don't see how the definition you posted contradicts my comment about it. Saying "this algorithm is efficient time-wise" and "this algorithm is efficient space-wise" is perfectly valid and describes more accurately what type of efficiency was achieved.

Indeed, while reversed returns an iterator, L[::-1] does return a slice object, which takes up O(n) space. There is itertools.islice to obtain an iterator instead, but the syntax of doing that won't be so short anymore.

@flintforge
Copy link
Contributor

what about mentioning this notes in the code ? That would be very instructive for newcomers, doesn't it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants