1

I have the following piece of code. Line 1 is a container (for simplicity, one can think of it as a list of elements e1, e2, ..., en). Now there is a function function_fun which takes as input an element ei and returns a list of elements. So finally we need to recursively keep processing elements from container and apply function_fun to get new elements such that the container satisfies some_property. Is this a general paradigm, and is there any faster way to do this?

   1. container = [e1, e2, ..., en]
   2. while (!some_property(container))
   3.    temp_container = []
   4.    for e in container
   5.       ttemp_container = function_fun(e)
   5.       temp_container = temp_container + ttemp_container
   6.    container = temp_container
   7.    return container

If someone wants a concrete example of this, then let's say we want to generate all binary strings of length n. Here is how we will implement it here:

   1. container = ["0","1"]
   2. while (len(container[0])!=n)
   3.    temp_container = []
   4.    for e in container
   5.       ttemp_container = [e+"0", e+"1"]
   5.       temp_container = temp_container + ttemp_container
   6.    container = temp_container
   7.    return container
1
  • In maths it may be called a transitive closure Commented Apr 6, 2023 at 23:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.