![]() |
recursion task - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: recursion task (/thread-32246.html) |
recursion task - scorp08 - Jan-30-2021 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 ![]() RE: recursion task - buran - Jan-30-2021 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]))))
RE: recursion task - scorp08 - Jan-30-2021 (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. Thanks for suggestions. I would prefer recursion as to practice. Could you solve without itertools if possible RE: recursion task - buran - Jan-30-2021 (Jan-30-2021, 08:12 AM)scorp08 Wrote: I would prefer recursion as to practice. Could you solve without itertools if possibleNo, I will not. You want to practice - then do it... :-) I am moving this to Homework section now RE: recursion task - bowlofred - Jan-30-2021 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))) RE: recursion task - scorp08 - Jan-30-2021 (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 possibleNo, I will not. Ok doesnt matter :) RE: recursion task - jefsummers - Jan-30-2021 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 RE: recursion task - deanhystad - Jan-31-2021 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. RE: recursion task - scorp08 - Jan-31-2021 (Jan-31-2021, 05:09 AM)deanhystad Wrote: This aptly named recursive function does what you want (I think).Thnks and I forgot to write that limits length can be more than 2 :) so I think yes, itertools is good fit RE: recursion task - subtra3t - Feb-01-2021 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]] |