how to rewrite a function to recursive [closed]
I have this function in python. It works like map(func, l1, l2) when both lists have the same length. It applies a function to all the items in list1 and list2.
def map2(func, l1, l2):
result =
if len(l1) > 0:
for i in range(len(l1)):
result.append(func(l1[i], l2[i]))
return result
How can I rewrite it to recursive program?
Thanks
python recursion
closed as unclear what you're asking by Spencer Wieczorek, stovfl, Shiladitya, greg-449, gnat Nov 10 '18 at 12:29
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have this function in python. It works like map(func, l1, l2) when both lists have the same length. It applies a function to all the items in list1 and list2.
def map2(func, l1, l2):
result =
if len(l1) > 0:
for i in range(len(l1)):
result.append(func(l1[i], l2[i]))
return result
How can I rewrite it to recursive program?
Thanks
python recursion
closed as unclear what you're asking by Spencer Wieczorek, stovfl, Shiladitya, greg-449, gnat Nov 10 '18 at 12:29
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
What isfunc
? please provide sample code with sample input and output data.
– Prince Francis
Nov 10 '18 at 7:51
func can be any function that applies to all the items in list1 and list2. For example: lambda x,y: [x,y]
– Julia
Nov 27 '18 at 22:42
My program does the same as the python map function: map(function_to_apply, list_of_inputs) except the 2 lists must have the same length. For example: map2(lambda x,y: [x,y], [1, 2, 3], [4, 5, 6]), output is [[1, 4], [2, 5], [3, 6]]
– Julia
Nov 27 '18 at 22:53
add a comment |
I have this function in python. It works like map(func, l1, l2) when both lists have the same length. It applies a function to all the items in list1 and list2.
def map2(func, l1, l2):
result =
if len(l1) > 0:
for i in range(len(l1)):
result.append(func(l1[i], l2[i]))
return result
How can I rewrite it to recursive program?
Thanks
python recursion
I have this function in python. It works like map(func, l1, l2) when both lists have the same length. It applies a function to all the items in list1 and list2.
def map2(func, l1, l2):
result =
if len(l1) > 0:
for i in range(len(l1)):
result.append(func(l1[i], l2[i]))
return result
How can I rewrite it to recursive program?
Thanks
python recursion
python recursion
edited Nov 27 '18 at 22:45
asked Nov 10 '18 at 7:46
Julia
12
12
closed as unclear what you're asking by Spencer Wieczorek, stovfl, Shiladitya, greg-449, gnat Nov 10 '18 at 12:29
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Spencer Wieczorek, stovfl, Shiladitya, greg-449, gnat Nov 10 '18 at 12:29
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
What isfunc
? please provide sample code with sample input and output data.
– Prince Francis
Nov 10 '18 at 7:51
func can be any function that applies to all the items in list1 and list2. For example: lambda x,y: [x,y]
– Julia
Nov 27 '18 at 22:42
My program does the same as the python map function: map(function_to_apply, list_of_inputs) except the 2 lists must have the same length. For example: map2(lambda x,y: [x,y], [1, 2, 3], [4, 5, 6]), output is [[1, 4], [2, 5], [3, 6]]
– Julia
Nov 27 '18 at 22:53
add a comment |
1
What isfunc
? please provide sample code with sample input and output data.
– Prince Francis
Nov 10 '18 at 7:51
func can be any function that applies to all the items in list1 and list2. For example: lambda x,y: [x,y]
– Julia
Nov 27 '18 at 22:42
My program does the same as the python map function: map(function_to_apply, list_of_inputs) except the 2 lists must have the same length. For example: map2(lambda x,y: [x,y], [1, 2, 3], [4, 5, 6]), output is [[1, 4], [2, 5], [3, 6]]
– Julia
Nov 27 '18 at 22:53
1
1
What is
func
? please provide sample code with sample input and output data.– Prince Francis
Nov 10 '18 at 7:51
What is
func
? please provide sample code with sample input and output data.– Prince Francis
Nov 10 '18 at 7:51
func can be any function that applies to all the items in list1 and list2. For example: lambda x,y: [x,y]
– Julia
Nov 27 '18 at 22:42
func can be any function that applies to all the items in list1 and list2. For example: lambda x,y: [x,y]
– Julia
Nov 27 '18 at 22:42
My program does the same as the python map function: map(function_to_apply, list_of_inputs) except the 2 lists must have the same length. For example: map2(lambda x,y: [x,y], [1, 2, 3], [4, 5, 6]), output is [[1, 4], [2, 5], [3, 6]]
– Julia
Nov 27 '18 at 22:53
My program does the same as the python map function: map(function_to_apply, list_of_inputs) except the 2 lists must have the same length. For example: map2(lambda x,y: [x,y], [1, 2, 3], [4, 5, 6]), output is [[1, 4], [2, 5], [3, 6]]
– Julia
Nov 27 '18 at 22:53
add a comment |
1 Answer
1
active
oldest
votes
Use lengths of the lists to terminate the recursion.
In this example implementation, if length of either of the list
s is 0, we have either mapped it all, or it is an empty list
to begin with, thus we terminate the recursion. Also worth noting that l1[1:]
is the slice of the list
we haven't mapped yet.
def map3(func, l1, l2):
if (len(l1) == 0 or len(l2) == 0):
return
return [func(l1[0], l2[0])] + map3(func, l1[1:], l2[1:])
Let's test it:
Test function:
def func(a, b):
return a * b
Test list
s:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
Run it:
print(map3(func, l1, l2))
Output:
[4, 10, 18]
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use lengths of the lists to terminate the recursion.
In this example implementation, if length of either of the list
s is 0, we have either mapped it all, or it is an empty list
to begin with, thus we terminate the recursion. Also worth noting that l1[1:]
is the slice of the list
we haven't mapped yet.
def map3(func, l1, l2):
if (len(l1) == 0 or len(l2) == 0):
return
return [func(l1[0], l2[0])] + map3(func, l1[1:], l2[1:])
Let's test it:
Test function:
def func(a, b):
return a * b
Test list
s:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
Run it:
print(map3(func, l1, l2))
Output:
[4, 10, 18]
add a comment |
Use lengths of the lists to terminate the recursion.
In this example implementation, if length of either of the list
s is 0, we have either mapped it all, or it is an empty list
to begin with, thus we terminate the recursion. Also worth noting that l1[1:]
is the slice of the list
we haven't mapped yet.
def map3(func, l1, l2):
if (len(l1) == 0 or len(l2) == 0):
return
return [func(l1[0], l2[0])] + map3(func, l1[1:], l2[1:])
Let's test it:
Test function:
def func(a, b):
return a * b
Test list
s:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
Run it:
print(map3(func, l1, l2))
Output:
[4, 10, 18]
add a comment |
Use lengths of the lists to terminate the recursion.
In this example implementation, if length of either of the list
s is 0, we have either mapped it all, or it is an empty list
to begin with, thus we terminate the recursion. Also worth noting that l1[1:]
is the slice of the list
we haven't mapped yet.
def map3(func, l1, l2):
if (len(l1) == 0 or len(l2) == 0):
return
return [func(l1[0], l2[0])] + map3(func, l1[1:], l2[1:])
Let's test it:
Test function:
def func(a, b):
return a * b
Test list
s:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
Run it:
print(map3(func, l1, l2))
Output:
[4, 10, 18]
Use lengths of the lists to terminate the recursion.
In this example implementation, if length of either of the list
s is 0, we have either mapped it all, or it is an empty list
to begin with, thus we terminate the recursion. Also worth noting that l1[1:]
is the slice of the list
we haven't mapped yet.
def map3(func, l1, l2):
if (len(l1) == 0 or len(l2) == 0):
return
return [func(l1[0], l2[0])] + map3(func, l1[1:], l2[1:])
Let's test it:
Test function:
def func(a, b):
return a * b
Test list
s:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
Run it:
print(map3(func, l1, l2))
Output:
[4, 10, 18]
edited Nov 10 '18 at 8:13
answered Nov 10 '18 at 7:59
Ayxan
1,510115
1,510115
add a comment |
add a comment |
1
What is
func
? please provide sample code with sample input and output data.– Prince Francis
Nov 10 '18 at 7:51
func can be any function that applies to all the items in list1 and list2. For example: lambda x,y: [x,y]
– Julia
Nov 27 '18 at 22:42
My program does the same as the python map function: map(function_to_apply, list_of_inputs) except the 2 lists must have the same length. For example: map2(lambda x,y: [x,y], [1, 2, 3], [4, 5, 6]), output is [[1, 4], [2, 5], [3, 6]]
– Julia
Nov 27 '18 at 22:53