From 97343ec7e8190d941726745ef99f7e20ac15b70b Mon Sep 17 00:00:00 2001 From: JISAUAY Date: Tue, 11 Mar 2025 20:28:30 -0500 Subject: [PATCH] solved p2, p3 --- AGGRCOW.py | 54 ++++++++++++++++++++++++++++++++++++++++ ARTHEVAL2.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 AGGRCOW.py create mode 100644 ARTHEVAL2.py diff --git a/AGGRCOW.py b/AGGRCOW.py new file mode 100644 index 0000000..9272c9c --- /dev/null +++ b/AGGRCOW.py @@ -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() diff --git a/ARTHEVAL2.py b/ARTHEVAL2.py new file mode 100644 index 0000000..8bc78e9 --- /dev/null +++ b/ARTHEVAL2.py @@ -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() \ No newline at end of file