how to rewrite a function to recursive [closed]










-4














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










share|improve this 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 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















-4














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










share|improve this 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 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













-4












-4








-4


1





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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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







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












1 Answer
1






active

oldest

votes


















0














Use lengths of the lists to terminate the recursion.

In this example implementation, if length of either of the lists 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 lists:



l1 = [1, 2, 3]
l2 = [4, 5, 6]


Run it:



print(map3(func, l1, l2))


Output:



[4, 10, 18] 





share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Use lengths of the lists to terminate the recursion.

    In this example implementation, if length of either of the lists 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 lists:



    l1 = [1, 2, 3]
    l2 = [4, 5, 6]


    Run it:



    print(map3(func, l1, l2))


    Output:



    [4, 10, 18] 





    share|improve this answer



























      0














      Use lengths of the lists to terminate the recursion.

      In this example implementation, if length of either of the lists 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 lists:



      l1 = [1, 2, 3]
      l2 = [4, 5, 6]


      Run it:



      print(map3(func, l1, l2))


      Output:



      [4, 10, 18] 





      share|improve this answer

























        0












        0








        0






        Use lengths of the lists to terminate the recursion.

        In this example implementation, if length of either of the lists 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 lists:



        l1 = [1, 2, 3]
        l2 = [4, 5, 6]


        Run it:



        print(map3(func, l1, l2))


        Output:



        [4, 10, 18] 





        share|improve this answer














        Use lengths of the lists to terminate the recursion.

        In this example implementation, if length of either of the lists 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 lists:



        l1 = [1, 2, 3]
        l2 = [4, 5, 6]


        Run it:



        print(map3(func, l1, l2))


        Output:



        [4, 10, 18] 






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 '18 at 8:13

























        answered Nov 10 '18 at 7:59









        Ayxan

        1,510115




        1,510115













            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)