-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bee764c
commit a117747
Showing
3 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from ai_powered import ai_powered | ||
|
||
@ai_powered | ||
def binary_search(arr: list[int], x: int) -> int: | ||
''' Returns the index of x in arr if present, else -1 ''' | ||
... | ||
|
||
def test_binary_search(): | ||
|
||
assert binary_search([1, 2, 100, 103, 104, 105], 103) == 3 | ||
assert binary_search([1, 2, 100, 103, 104, 105], 2) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from ai_powered import ai_powered | ||
|
||
@ai_powered | ||
def python_expression_generator(expr: str) -> str: | ||
''' 将用户输入的数学表达式转换成合法的python表达式, 注意不要使用任何未定义的函数,如果用户表达式中有类似函数调用的表达,请转换为python内置函数或语法 ''' | ||
... | ||
|
||
def calculator(expr: str) -> int: | ||
''' calculate the result of the math expression ''' | ||
py_expr = python_expression_generator(expr) | ||
print(f"{py_expr =}") | ||
x = eval(py_expr) | ||
return int(x) | ||
|
||
def test_calculator(): | ||
assert calculator("1+1") == 2 | ||
assert calculator("1+2*3") == 7 | ||
assert calculator("2^10+3*4") == 1036 | ||
assert calculator("2^10+sum(1,10)") == 1079 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters