p2 has me washed

This commit is contained in:
0x01FE 2025-03-11 12:16:34 -05:00
parent ba2d3e9bf6
commit 5b3351c2b8
2 changed files with 86 additions and 0 deletions

73
ARTHEVAL.py Normal file
View File

@ -0,0 +1,73 @@
def find_closing_p(s: str, opening_index: int) -> int:
skips = 0
for i, c in enumerate(s[opening_index + 1:]):
match c:
case ')':
if skips != 0:
skips -= 1
else:
return i + opening_index + 1
case '(':
skips += 1
def do_math(s: str) -> int:
tokens = []
i = 0
for c in s:
if c.isdigit():
try:
tokens[i] += c
except IndexError:
tokens.append(c)
elif c in {'+', '-', '*', '/'}:
i += 1
tokens.append(c)
last_piece = tokens.pop(0)
result = 0
for piece in tokens:
result = eval(f'{last_piece}{piece}')
last_piece = str(result)
return result
def do_parentheses(s: str) -> str:
if '(' in s:
opening_index: int = s.find('(')
closing_index: int = find_closing_p(s, opening_index)
sub_snp = s[opening_index + 1:closing_index]
return str(do_math(s[:opening_index] + do_parentheses(sub_snp)))
else:
if len(s) == 1:
return s
else:
return str(do_math(s))
# (5*6)-(4+(1))
def main():
s: str = input()
# Wish I could just use eval() but it uses pemdas
# Evaluate all parentheses
while '(' in s:
opening_index: int = s.find('(')
closing_index: int = find_closing_p(s, opening_index)
sub_snp = s[opening_index + 1:closing_index]
sub_s = s[opening_index:closing_index + 1]
s = s.replace(sub_s, do_parentheses(sub_snp))
print(do_math(s))
if __name__ == '__main__':
main()

13
GDCOTFI.py Normal file
View File

@ -0,0 +1,13 @@
import math
def main():
nums = []
for i in range(3):
nums.append(int(input()))
print(math.gcd(*nums))
if __name__ == '__main__':
main()