Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizKindly log in to use this feature. We’ll take you to the login page automatically.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
iref="https://forum.tuteehub.com/tag/n">ndexable[ref="https://forum.tuteehub.com/tag/n">n] is the way to get the value in index ref="https://forum.tuteehub.com/tag/n">n of iref="https://forum.tuteehub.com/tag/n">ndexable. Since tuples are indexable collections (they define __getitem__), you're getting the zero-index and one-index of that tuple (the first and second values, respectively).
This might be better-uref="https://forum.tuteehub.com/tag/n">nderstood iref="https://forum.tuteehub.com/tag/n">n the ref="https://forum.tuteehub.com/tag/n">no-loref="https://forum.tuteehub.com/tag/n">nger-valid syref="https://forum.tuteehub.com/tag/n">ntax:
lambda (sref="https://forum.tuteehub.com/tag/1">1, s2), i: (s2, p*s2 + q*sref="https://forum.tuteehub.com/tag/1">1 + r)
As you correctly intuited, accumulate([xref="https://forum.tuteehub.com/tag/1">1, x2, x3], fref="https://forum.tuteehub.com/tag/n">n) returns the infinite series [xref="https://forum.tuteehub.com/tag/1">1, fref="https://forum.tuteehub.com/tag/n">n(xref="https://forum.tuteehub.com/tag/1">1, x2), fref="https://forum.tuteehub.com/tag/n">n(x2, fref="https://forum.tuteehub.com/tag/n">n(xref="https://forum.tuteehub.com/tag/1">1, x2)), ...] fref="https://forum.tuteehub.com/tag/n">n in this case is a function that has the signature:
def fref="https://forum.tuteehub.com/tag/n">n(last_last_ref="https://forum.tuteehub.com/tag/value">value, last_ref="https://forum.tuteehub.com/tag/value">value)
The docs probably show this most clearly, using operator.add (aka +)
def accumulate(iterable, furef="https://forum.tuteehub.com/tag/n">nc=operator.add):
'Returref="https://forum.tuteehub.com/tag/n">n ruref="https://forum.tuteehub.com/tag/n">nref="https://forum.tuteehub.com/tag/n">niref="https://forum.tuteehub.com/tag/n">ng totals'
# accumulate([ref="https://forum.tuteehub.com/tag/1">1,2,3,4,5]) --> ref="https://forum.tuteehub.com/tag/1">1 3 6 ref="https://forum.tuteehub.com/tag/1">10 ref="https://forum.tuteehub.com/tag/1">15
# accumulate([ref="https://forum.tuteehub.com/tag/1">1,2,3,4,5], operator.mul) --> ref="https://forum.tuteehub.com/tag/1">1 2 6 24 ref="https://forum.tuteehub.com/tag/1">120
it = iter(iterable)
try:
total = ref="https://forum.tuteehub.com/tag/n">next(it)
except StopIteratioref="https://forum.tuteehub.com/tag/n">n:
returref="https://forum.tuteehub.com/tag/n">n
yield total
for elemeref="https://forum.tuteehub.com/tag/n">nt iref="https://forum.tuteehub.com/tag/n">n it:
total = furef="https://forum.tuteehub.com/tag/n">nc(total, elemeref="https://forum.tuteehub.com/tag/n">nt)
yield total
Since p=1 and q=1 and r=0, we can ignore them all together and it ultimately boils down to just this:
it.accumulate(
[(0,1),(0,1),(0,1),(0,1),(0,1), ...],
lambda prev, current: (prev[1], prev[1] + prev[0])
)
0th: -> result0 = (0 ,1)
1th: prev=(0,1) current=(0,1) -> result1 = (1, 0+1) = (1, 1)
2nd: prev=result1 current=(0,1) -> result2 = (0+1, 0+1+1) = (1, 2)
3rd: prev=result2 current=(0,1) -> result3 = (0+1+1, 0+1+1+0+1) = (2, 3)
4th: prev=result3 current=(0,1) -> result4 = (0+1+1+0+1, 0+1+1+0+1+0+1+1) = (3, 5)
...
as you can see current is never being used
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Kindly log in to use this feature. We’ll take you to the login page automatically.
Login
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
could someone help me understand what the s[1] and s[0] are referring to in the code below? This code will generate a Fibonacci serie. And I am still trying to understand how accumulate() works. Does it return the first value (0,1) as it is, then uses the result from the first application of the lambda function as s[1] and another tuple (0,1) from the list generated by repeat() as s[0]? or s[0] and s[1] are assigned as 0 and 1 respectively?
Thanks!