|
| 1 | +import textwrap |
| 2 | +import time |
| 3 | + |
| 4 | +import py_trees |
| 5 | + |
| 6 | +from playground.behavior_trees import ( |
| 7 | + create_assistant_action_on_thread, |
| 8 | + create_assistant_condition, |
| 9 | +) |
| 10 | +from playground.assistants_api import api |
| 11 | + |
| 12 | +# Create the root node (sequence) |
| 13 | +root = py_trees.composites.Sequence("RootSequence", memory=True) |
| 14 | + |
| 15 | +# selector = py_trees.composites.Selector("Hacker Team Selector", memory=True) |
| 16 | +# root.add_child(selector) |
| 17 | + |
| 18 | + |
| 19 | +thread = api.create_thread() |
| 20 | +challenge = textwrap.dedent(""" |
| 21 | + Given a string of digits, return the longest substring with alternating odd/even or even/odd digits. |
| 22 | + If two or more substrings have the same length, return the substring that occurs first. |
| 23 | + Examples |
| 24 | +
|
| 25 | + longest_substring("225424272163254474441338664823") ➞ "272163254" |
| 26 | + # substrings = 254, 272163254, 474, 41, 38, 23 |
| 27 | +
|
| 28 | + longest_substring("594127169973391692147228678476") ➞ "16921472" |
| 29 | + # substrings = 94127, 169, 16921472, 678, 476 |
| 30 | +
|
| 31 | + longest_substring("721449827599186159274227324466") ➞ "7214" |
| 32 | + # substrings = 7214, 498, 27, 18, 61, 9274, 27, 32 |
| 33 | + # 7214 and 9274 have same length, but 7214 occurs first. |
| 34 | +
|
| 35 | + Notes |
| 36 | +
|
| 37 | + The minimum alternating substring size is 2, and there will always be at least one alternating substring. |
| 38 | +""") |
| 39 | +judge_test_cases = textwrap.dedent(""" |
| 40 | + Test.assert_equals(longest_substring("844929328912985315632725682153"), "56327256") |
| 41 | + Test.assert_equals(longest_substring("769697538272129475593767931733"), "27212947") |
| 42 | + Test.assert_equals(longest_substring("937948289456111258444958189244"), "894561") |
| 43 | + Test.assert_equals(longest_substring("736237766362158694825822899262"), "636") |
| 44 | + Test.assert_equals(longest_substring("369715978955362655737322836233"), "369") |
| 45 | + Test.assert_equals(longest_substring("345724969853525333273796592356"), "496985") |
| 46 | + Test.assert_equals(longest_substring("548915548581127334254139969136"), "8581") |
| 47 | + Test.assert_equals(longest_substring("417922164857852157775176959188"), "78521") |
| 48 | + Test.assert_equals(longest_substring("251346385699223913113161144327"), "638569") |
| 49 | + Test.assert_equals(longest_substring("483563951878576456268539849244"), "18785") |
| 50 | + Test.assert_equals(longest_substring("853667717122615664748443484823"), "474") |
| 51 | + Test.assert_equals(longest_substring("398785511683322662883368457392"), "98785") |
| 52 | + Test.assert_equals(longest_substring("368293545763611759335443678239"), "76361") |
| 53 | + Test.assert_equals(longest_substring("775195358448494712934755311372"), "4947") |
| 54 | + Test.assert_equals(longest_substring("646113733929969155976523363762"), "76523") |
| 55 | + Test.assert_equals(longest_substring("575337321726324966478369152265"), "478369") |
| 56 | + Test.assert_equals(longest_substring("754388489999793138912431545258"), "545258") |
| 57 | + Test.assert_equals(longest_substring("198644286258141856918653955964"), "2581418569") |
| 58 | + Test.assert_equals(longest_substring("643349187319779695864213682274"), "349") |
| 59 | + Test.assert_equals(longest_substring("919331281193713636178478295857"), "36361") |
| 60 | + Test.assert_equals(longest_substring("2846286484444288886666448822244466688822247"), "47") |
| 61 | +""") |
| 62 | + |
| 63 | +hacker = create_assistant_action_on_thread( |
| 64 | + thread=thread, |
| 65 | + action_name="Hacker", |
| 66 | + assistant_name="Python Coding Assistant", |
| 67 | + assistant_instructions=textwrap.dedent(f""" |
| 68 | + Challenge goal: |
| 69 | + {challenge} |
| 70 | + Solve the challenge and output the final solution to a file called solution.py |
| 71 | + """), |
| 72 | +) |
| 73 | +root.add_child(hacker) |
| 74 | + |
| 75 | +judge = create_assistant_action_on_thread( |
| 76 | + thread=thread, |
| 77 | + action_name="Judge solution", |
| 78 | + assistant_name="Coding Challenge Judge", |
| 79 | + assistant_instructions=textwrap.dedent( |
| 80 | + f""" |
| 81 | + Challenge goal: |
| 82 | + {challenge} |
| 83 | + Load the solution from the file solution.py. |
| 84 | + Then confirm is a solution to the challenge and test it with the following test cases: |
| 85 | + {judge_test_cases} |
| 86 | + Run the code for the solution and confirm it passes all the test cases. |
| 87 | + If the solution passes all tests save the solution to a file called judged_solution.py |
| 88 | + """, |
| 89 | + ), |
| 90 | +) |
| 91 | +root.add_child(judge) |
| 92 | + |
| 93 | +# verifier operates on a different thread, essentially in closed room |
| 94 | +verifier = create_assistant_condition( |
| 95 | + condition_name="Verify solution", |
| 96 | + assistant_name="Python Coding Assistant", |
| 97 | + assistant_instructions=textwrap.dedent( |
| 98 | + f""" |
| 99 | + Challenge goal: |
| 100 | + {challenge} |
| 101 | + Load the file called judged_solution.py and verify that the solution is correct by running the code and confirm it passes all the test cases: |
| 102 | + {judge_test_cases} |
| 103 | + If the solution is correct, return only the single word SUCCESS, otherwise return the single word FAILURE. |
| 104 | + """, |
| 105 | + ), |
| 106 | +) |
| 107 | +root.add_child(verifier) |
| 108 | + |
| 109 | +# Create the behavior tree |
| 110 | +tree = py_trees.trees.BehaviourTree(root) |
| 111 | + |
| 112 | +# Tick the tree to run it |
| 113 | +# for i in range(1000): |
| 114 | +# print(f"Tick {i + 1}") |
| 115 | +# tree.tick() |
| 116 | +# time.sleep(30) # Simulate time between ticks |
| 117 | + |
| 118 | +while True: |
| 119 | + tree.tick() |
| 120 | + time.sleep(20) # Simulate time between ticks |
| 121 | + if root.status == py_trees.common.Status.SUCCESS: |
| 122 | + break |
0 commit comments