Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update algo.py #266

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions resources/algo.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
lines = []
while True:
line = input()
if line.strip() == "":
break
lines.append(line)
def main():
lines = []
print("Enter your code (press Enter twice to finish):")

# Read lines of input until a blank line is encountered
while True:
line = input()
if line.strip() == "":
break
lines.append(line)

for i in lines:
if 'def' in i:
definition=i.strip()[4:-1]
definition=definition.replace("self, ","")
# Extract function name
function_name = None
for line in lines:
if 'def ' in line:
# Use partition for cleaner extraction of function name
definition = line.partition('def ')[2].strip()
function_name = definition.split('(')[0] # Get the function name
break

output_code=""
for i in lines:
output_code+=i
output_code+="\n"
if 'def' in i:
break
# Check if a function definition was found
if function_name is None:
print("No function definition found.")
return

output_code+="\t\t\n\t\t\n\t\t\n\t\t\n"
output_code+="#testCase\n\nobj=Solution()\nprint(obj.{})".format(definition)
print(output_code)
# Create output code
output_code = "\n".join(lines) + "\n\n"
output_code += "\t\t\n\t\t\n\t\t\n\t\t\n" # Indentation for readability
output_code += "#testCase\n\n"
output_code += f"obj = Solution()\nprint(obj.{function_name}())" # Add parentheses for function call

# Print the output code
print(output_code)

if __name__ == "__main__":
main()