Posts: 8
Threads: 2
Joined: Sep 2019
Jan-30-2021, 07:22 AM
(This post was last modified: Jan-30-2021, 08:19 AM by scorp08.)
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
Posts: 8,160
Threads: 160
Joined: Sep 2016
Jan-30-2021, 08:10 AM
(This post was last modified: Jan-30-2021, 08:11 AM by buran.)
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)]
Posts: 8
Threads: 2
Joined: Sep 2019
(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
Posts: 8,160
Threads: 160
Joined: Sep 2016
(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
Posts: 1,583
Threads: 3
Joined: Mar 2020
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)))
Posts: 8
Threads: 2
Joined: Sep 2019
(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 :)
Posts: 1,358
Threads: 2
Joined: May 2019
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
Posts: 6,794
Threads: 20
Joined: Feb 2020
Jan-31-2021, 05:09 AM
(This post was last modified: Jan-31-2021, 05:11 AM by deanhystad.)
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.
Posts: 8
Threads: 2
Joined: Sep 2019
(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
Posts: 1
Threads: 0
Joined: Feb 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]]
|