Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursion task
#9
(Jan-31-2021, 05:09 AM)deanhystad Wrote: This aptly named recursive function does what you want (I think).
def dumb_recursion(a, a_end, a_incr, b, b_end, b_incr):
    if a >= a_end or b >= b_end:
        return
    result.append([a, b])
    dumb_recursion(a, a+a_incr, a_incr, b + b_incr, b_end, b_incr)
    dumb_recursion(a + a_incr, a_end, a_incr, b, b_end, b_incr)

result = []
dumb_recursion(1, 5, 1, 5, 20, 5)
print(result)
The tricky part is unwinding b all the way back to the start before incrementing a. This is really ugly code that is so much clearer as two loops.
def not_recursion(a, b):
    result = []
    for i in range(*a):
        for j in range(*b):
            result.append([i, j])
    return result

print(not_recursion((1, 5, 1), (5, 20, 5)))
And this would start you thinking "Multiplying lists must be a common thing in such a list heavy language as Python. I wonder if there are any functions that do this?" And you do a little looking around and you find you can do this in one line.

And I think the Towers of Hanoi recursive solution is stupid too.
Thnks and I forgot to write that limits length can be more than 2 :) so I think yes, itertools is good fit
Reply


Messages In This Thread
recursion task - by scorp08 - Jan-30-2021, 07:22 AM
RE: recursion task - by buran - Jan-30-2021, 08:10 AM
RE: recursion task - by scorp08 - Jan-30-2021, 08:12 AM
RE: recursion task - by buran - Jan-30-2021, 08:14 AM
RE: recursion task - by scorp08 - Jan-30-2021, 08:21 AM
RE: recursion task - by bowlofred - Jan-30-2021, 08:18 AM
RE: recursion task - by jefsummers - Jan-30-2021, 10:30 PM
RE: recursion task - by deanhystad - Jan-31-2021, 05:09 AM
RE: recursion task - by scorp08 - Jan-31-2021, 09:13 AM
RE: recursion task - by subtra3t - Feb-01-2021, 02:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,672 Oct-05-2020, 07:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020