From 307c51539cbd6feb439a1c74726f3a2a104e9a83 Mon Sep 17 00:00:00 2001 From: bronku Date: Sat, 6 Dec 2025 10:06:42 +0100 Subject: [PATCH] tests --- test/ruby/23.in | 1 - test/ruby/25.in | 19 +++++++++++++++++++ test/ruby/26.in | 27 +++++++++++++++++++++++++++ test/ruby/27.in | 31 +++++++++++++++++++++++++++++++ test/ruby/28.in | 31 +++++++++++++++++++++++++++++++ test/ruby/29.in | 34 ++++++++++++++++++++++++++++++++++ test/ruby/30.in | 18 ++++++++++++++++++ test/ruby/31.in | 33 +++++++++++++++++++++++++++++++++ test/ruby/32.in | 29 +++++++++++++++++++++++++++++ test/ruby/33.in | 30 ++++++++++++++++++++++++++++++ 10 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 test/ruby/25.in create mode 100644 test/ruby/26.in create mode 100644 test/ruby/27.in create mode 100644 test/ruby/28.in create mode 100644 test/ruby/29.in create mode 100644 test/ruby/30.in create mode 100644 test/ruby/31.in create mode 100644 test/ruby/32.in create mode 100644 test/ruby/33.in diff --git a/test/ruby/23.in b/test/ruby/23.in index 33b2afc..8c6df93 100644 --- a/test/ruby/23.in +++ b/test/ruby/23.in @@ -1,4 +1,3 @@ -# test_basic.py def factorial(n): """Calculate factorial recursively""" if n <= 1: diff --git a/test/ruby/25.in b/test/ruby/25.in new file mode 100644 index 0000000..b8eb954 --- /dev/null +++ b/test/ruby/25.in @@ -0,0 +1,19 @@ +# test_arithmetic.py +def calculate_bmi(weight, height): + bmi = weight / (height ** 2) + + if bmi < 18.5: + category = "Underweight" + elif bmi < 25: + category = "Normal" + elif bmi < 30: + category = "Overweight" + else: + category = "Obese" + + return f"BMI: {bmi}, Category: {category}" + +# Test +print(calculate_bmi(70, 1.75)) +print(calculate_bmi(90, 1.80)) +print(calculate_bmi(50, 1.65)) \ No newline at end of file diff --git a/test/ruby/26.in b/test/ruby/26.in new file mode 100644 index 0000000..612fef7 --- /dev/null +++ b/test/ruby/26.in @@ -0,0 +1,27 @@ +def find_max_min(numbers): + if not numbers: + return None, None + + max_num = numbers[0] + min_num = numbers[0] + + for num in numbers[1:]: + if num > max_num: + max_num = num + if num < min_num: + min_num = num + + return max_num, min_num + +def reverse_list(items): + reversed_items = [] + for i in range(len(items) - 1, -1, -1): + reversed_items.append(items[i]) + return reversed_items + +numbers = [3, 1, 4, 1, 5, 9, 2, 6] +max_val, min_val = find_max_min(numbers) +print(f"List: {numbers}") +print(f"Max: {max_val}, Min: {min_val}") +print(f"Reversed: {reverse_list(numbers)}") +print(f"Original list unchanged: {numbers}") \ No newline at end of file diff --git a/test/ruby/27.in b/test/ruby/27.in new file mode 100644 index 0000000..0d36327 --- /dev/null +++ b/test/ruby/27.in @@ -0,0 +1,31 @@ +def count_words(text): + words = text.lower().split() + word_count = {} + + for word in words: + word = word.strip('.,!?') + if word in word_count: + word_count[word] += 1 + else: + word_count[word] = 1 + + return word_count + +def find_most_common(word_count): + most_common = None + max_count = 0 + + for word, count in word_count.items(): + if count > max_count: + most_common = word + max_count = count + + return most_common, max_count + +text = "Hello world hello there world hello" +counts = count_words(text) +common_word, frequency = find_most_common(counts) + +print(f"Text: {text}") +print(f"Word counts: {counts}") +print(f"Most common: '{common_word}' appears {frequency} times") \ No newline at end of file diff --git a/test/ruby/28.in b/test/ruby/28.in new file mode 100644 index 0000000..0d36327 --- /dev/null +++ b/test/ruby/28.in @@ -0,0 +1,31 @@ +def count_words(text): + words = text.lower().split() + word_count = {} + + for word in words: + word = word.strip('.,!?') + if word in word_count: + word_count[word] += 1 + else: + word_count[word] = 1 + + return word_count + +def find_most_common(word_count): + most_common = None + max_count = 0 + + for word, count in word_count.items(): + if count > max_count: + most_common = word + max_count = count + + return most_common, max_count + +text = "Hello world hello there world hello" +counts = count_words(text) +common_word, frequency = find_most_common(counts) + +print(f"Text: {text}") +print(f"Word counts: {counts}") +print(f"Most common: '{common_word}' appears {frequency} times") \ No newline at end of file diff --git a/test/ruby/29.in b/test/ruby/29.in new file mode 100644 index 0000000..467f0b2 --- /dev/null +++ b/test/ruby/29.in @@ -0,0 +1,34 @@ +class BankAccount: + def __init__(self, owner, balance=0): + self.owner = owner + self.balance = balance + + def deposit(self, amount): + if amount > 0: + self.balance += amount + return True + return False + + def withdraw(self, amount): + if 0 < amount <= self.balance: + self.balance -= amount + return True + return False + + def get_balance(self): + return self.balance + + def __str__(self): + return f"Account({self.owner}, Balance: ${self.balance:.2f})" + +account = BankAccount("Alice", 1000) +print(account) + +print(f"Deposit $500: {account.deposit(500)}") +print(f"Balance: ${account.get_balance()}") + +print(f"Withdraw $200: {account.withdraw(200)}") +print(f"Balance: ${account.get_balance()}") + +print(f"Withdraw $2000: {account.withdraw(2000)}") +print(f"Final balance: ${account.get_balance()}") \ No newline at end of file diff --git a/test/ruby/30.in b/test/ruby/30.in new file mode 100644 index 0000000..25225f9 --- /dev/null +++ b/test/ruby/30.in @@ -0,0 +1,18 @@ +def write_to_file(filename, content): + with open(filename, 'w') as file: + file.write(content) + print(f"Written to {filename}") + +def read_from_file(filename): + try: + with open(filename, 'r') as file: + content = file.read() + return content + except FileNotFoundError: + return "File not found" + +write_to_file("test_output.txt", "Hello, World!\nThis is a test.\n") +content = read_from_file("test_output.txt") +print(f"File content:\n{content}") + +print(read_from_file("nonexistent.txt")) \ No newline at end of file diff --git a/test/ruby/31.in b/test/ruby/31.in new file mode 100644 index 0000000..44e9964 --- /dev/null +++ b/test/ruby/31.in @@ -0,0 +1,33 @@ +import math + +def circle_area(radius): + return math.pi * radius ** 2 + +def circle_circumference(radius): + return 2 * math.pi * radius + +def solve_quadratic(a, b, c): + discriminant = b ** 2 - 4 * a * c + + if discriminant < 0: + return None, None + elif discriminant == 0: + x = -b / (2 * a) + return x, x + else: + x1 = (-b + math.sqrt(discriminant)) / (2 * a) + x2 = (-b - math.sqrt(discriminant)) / (2 * a) + return x1, x2 + +print(f"Circle with radius 5:") +print(f" Area: {circle_area(5):.2f}") +print(f" Circumference: {circle_circumference(5):.2f}") + +print(f"\nQuadratic 2x² + 5x - 3 = 0:") +x1, x2 = solve_quadratic(2, 5, -3) +print(f" Solutions: {x1:.2f}, {x2:.2f}") + +print(f"\nQuadratic x² + 1 = 0:") +x1, x2 = solve_quadratic(1, 0, 1) +if x1 is None: + print(" No real solutions") \ No newline at end of file diff --git a/test/ruby/32.in b/test/ruby/32.in new file mode 100644 index 0000000..7f00609 --- /dev/null +++ b/test/ruby/32.in @@ -0,0 +1,29 @@ +def countdown(n): + while n > 0: + print(f"Countdown: {n}") + n -= 1 + print("Blast off!") + +def sum_until_negative(): + total = 0 + count = 0 + inputs = [5, 3, 8, -1, 4, 2] + + for value in inputs: + if value < 0: + break + total += value + count += 1 + + if count > 0: + average = total / count + return total, average + else: + return 0, 0 + +print("Countdown from 5:") +countdown(5) + +print("\nSum until negative:") +total, avg = sum_until_negative() +print(f"Total: {total}, Average: {avg:.2f}") \ No newline at end of file diff --git a/test/ruby/33.in b/test/ruby/33.in new file mode 100644 index 0000000..84a0b4c --- /dev/null +++ b/test/ruby/33.in @@ -0,0 +1,30 @@ +def analyze_numbers(numbers): + if not numbers: + return 0, 0, 0 + + total = sum(numbers) + average = total / len(numbers) + + positive_count = 0 + for num in numbers: + if num > 0: + positive_count += 1 + + return total, average, positive_count + +def get_coordinates(): + x = 10 + y = 20 + z = 30 + return x, y, z + +data = [3, -1, 4, -2, 0, 5, -3] +total, avg, positives = analyze_numbers(data) + +print(f"Numbers: {data}") +print(f"Total: {total}") +print(f"Average: {avg:.2f}") +print(f"Positive numbers: {positives}") + +x, y, z = get_coordinates() +print(f"\nCoordinates: x={x}, y={y}, z={z}") \ No newline at end of file