def universal(prog_string, in_string):
"""Execute any Python program with any input."""
# Execute the definition of the function in prog_string
# This defines the function but doesn't invoke it
namespace = {}
exec(prog_string, namespace)
# Extract function name from the program string
# Find the first callable function in namespace
for name, obj in namespace.items():
if callable(obj) and not name.startswith('_'):
return obj(in_string)
return "No function found"
# Test the universal program
prog = """
def contains_gaga(inString):
return 'yes' if 'GAGA' in inString else 'no'
"""
print(universal(prog, 'GTTGAGA')) # Should output: yes
print(universal(prog, 'GTTAA')) # Should output: no
yes
no