element reference inside accumulate method

General Tech Bugs & Fixes 2 years ago

0 3 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (3)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
0
 

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!

import itertools as it
def second_order(p, q, r, initial_values):
    """Return sequence defined by s(n) = p * s(n-1) + q * s(n-2) + r."""
    intermediate = it.accumulate(
        it.repeat(initial_values),
        lambda s, i: (s[1], p*s[1] + q*s[0] + r)
    )
    return intermediate
fibs = second_order(p=1, q=1, r=0, initial_values=(0, 1))
list(next(fibs) for n in range(8))
profilepic.png
manpreet 2 years ago

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

0 views   0 shares

profilepic.png
manpreet 2 years ago

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


0 views   0 shares

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.