Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursion task
#1
Hello ALL
I have a following :
limits=[(1,9,1,"v"),(50,350,50,"d")]
for i in range(len(limits)):

    begin=limits[i][0]
    end=limits[i][1]
    increment=limits[i][2]

I want to get a list of list like
  [[1,50],[1,100],[1,150]...,[2,50],[2,100]..]   
like combination of all values from begin to end values.
How can I do it with recursion ?
Thnks for help Wink
Reply
#2
please, post the code as one block with proper indentation. If you want to comment specific line - use python comments.
It's difficult to understand what you have and what you want to achieve. e.g. what is i?

not recursion (don't see the need for it or how you would use it anyway), probably something like

import itertools
limits=[(1,9,1,"v"),(50,350,50,"d")]
print(list(itertools.product(range(*limits[0][:-1]), range(*limits[1][:-1]))))
Output:
[(1, 50), (1, 100), (1, 150), (1, 200), (1, 250), (1, 300), (2, 50), (2, 100), (2, 150), (2, 200), (2, 250), (2, 300), (3, 50), (3, 100), (3, 150), (3, 200), (3, 250), (3, 300), (4, 50), (4, 100), (4, 150), (4, 200), (4, 250), (4, 300), (5, 50), (5, 100), (5, 150), (5, 200), (5, 250), (5, 300), (6, 50), (6, 100), (6, 150), (6, 200), (6, 250), (6, 300), (7, 50), (7, 100), (7, 150), (7, 200), (7, 250), (7, 300), (8, 50), (8, 100), (8, 150), (8, 200), (8, 250), (8, 300)]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-30-2021, 08:10 AM)buran Wrote: please, post the code as one block with proper indentation. If you want to comment specific line - use python comments.
It's difficult to understand what you have and what you want to achieve. e.g. what is i?

not recursion (don't see the need for it or how you would use it anyway), probably something like

import itertools
limits=[(1,9,1,"v"),(50,350,50,"d")]
print(list(itertools.product(range(*limits[0][:-1]), range(*limits[1][:-1]))))
Output:
[(1, 50), (1, 100), (1, 150), (1, 200), (1, 250), (1, 300), (2, 50), (2, 100), (2, 150), (2, 200), (2, 250), (2, 300), (3, 50), (3, 100), (3, 150), (3, 200), (3, 250), (3, 300), (4, 50), (4, 100), (4, 150), (4, 200), (4, 250), (4, 300), (5, 50), (5, 100), (5, 150), (5, 200), (5, 250), (5, 300), (6, 50), (6, 100), (6, 150), (6, 200), (6, 250), (6, 300), (7, 50), (7, 100), (7, 150), (7, 200), (7, 250), (7, 300), (8, 50), (8, 100), (8, 150), (8, 200), (8, 250), (8, 300)]

Thanks for suggestions. I would prefer recursion as to practice. Could you solve without itertools if possible
Reply
#4
(Jan-30-2021, 08:12 AM)scorp08 Wrote: I would prefer recursion as to practice. Could you solve without itertools if possible
No, I will not.
You want to practice - then do it... :-)
I am moving this to Homework section now
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
product will give you all the combinations you're looking for. Just need to set up the ranges to pass in.

from itertools import product

limits=[(1,9,1,"v"),(50,350,50,"d")]
rangelist = [range(x[0], x[1], x[2]) for x in limits]
print(list(product(*rangelist)))
Reply
#6
(Jan-30-2021, 08:14 AM)buran Wrote:
(Jan-30-2021, 08:12 AM)scorp08 Wrote: I would prefer recursion as to practice. Could you solve without itertools if possible
No, I will not.
You want to practice - then do it... :-)
I am moving this to Homework section now

Ok doesnt matter :)
Reply
#7
Recursion gets so abused. There are times that it is ideal - solving the Towers of Hanoi, for example. But a lot of students tend to try to use recursion when loops would do better. By better, consider the overhead that occurs every time the function is called, instead of simply moving through a loop. And, with libraries like itertools the looping gets even faster
Reply
#8
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.
Reply
#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
#10
There are some places where recursion is somewhat suitable,although it may not be fast. This may be what you are looking for, though it uses iteration instead of recursion:

def do(n):
    out = [[1, 0]]
    for i in range(n):
        out.append([1, out[len(out) - 1][1] + 50])
    return out
    
print(do(1))
# [[1, 0], [1, 50]]

print(do(2))
# [[1, 0], [1, 50], [1, 100]]

print(do(10))
# [[1, 0], [1, 50], [1, 100], [1, 150], [1, 200], [1, 250], [1, 300], [1, 350], [1, 400], [1, 450], [1, 500]]
Reply


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,664 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