solved p2, p3

This commit is contained in:
JISAUAY 2025-03-11 20:28:30 -05:00
parent 5b3351c2b8
commit 97343ec7e8
2 changed files with 123 additions and 0 deletions

54
AGGRCOW.py Normal file
View File

@ -0,0 +1,54 @@
def test_distance(cows: int, locations: list[int], distance: int) -> bool:
placed_cows = 1
last = locations[0]
for location in locations[1:]:
if location - last >= distance:
last = location
placed_cows += 1
if placed_cows >= cows:
return True
return False
def main():
test_cases: int = int(input())
while test_cases != 0:
stalls, cows = [int(n) for n in input().split()]
locations = []
i = 0
while i < stalls:
locations.append(int(input()))
i += 1
locations.sort()
l = locations[1] - locations[0]
r = max(locations) - min(locations)
m = (l + r) // 2
while True:
old_m = m
# Current split worked, so try more
if test_distance(cows, locations, m):
l = m + 1
else:
r = m - 1
m = (l + r) // 2
if m == old_m:
print(m)
break
test_cases -= 1
if __name__ == '__main__':
main()

69
ARTHEVAL2.py Normal file
View File

@ -0,0 +1,69 @@
# Returns result and index
def para(s: str, i: int) -> tuple[int, int]:
a, b = 0, 0
operator = '+'
c = s[i]
while c != ')':
c = s[i]
if c == '(':
i += 1
b, i = para(s, i)
match operator:
case '+':
a = a + b
case '-':
a = a - b
case '*':
a = a * b
elif c.isdigit():
b = int(c)
match operator:
case '+':
a = a + b
case '-':
a = a - b
case '*':
a = a * b
i += 1
else:
operator = c
i += 1
return (a, i)
def main():
s: str = input()
# Wish I could just use eval() but it uses pemdas
a, b = 0, 0
operator = '+'
i = 0
while i < len(s):
c = s[i]
if c == '(':
i += 1
b, i = para(s, i)
match operator:
case '+':
a = a + b
case '-':
a = a - b
case '*':
a = a * b
elif c.isdigit():
b = int(c)
match operator:
case '+':
a = a + b
case '-':
a = a - b
case '*':
a = a * b
i += 1
else:
operator = c
i += 1
print(a)
if __name__ == '__main__':
main()