tests
This commit is contained in:
parent
f92f5ef7f6
commit
307c51539c
10 changed files with 252 additions and 1 deletions
|
|
@ -1,4 +1,3 @@
|
|||
# test_basic.py
|
||||
def factorial(n):
|
||||
"""Calculate factorial recursively"""
|
||||
if n <= 1:
|
||||
|
|
|
|||
19
test/ruby/25.in
Normal file
19
test/ruby/25.in
Normal file
|
|
@ -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))
|
||||
27
test/ruby/26.in
Normal file
27
test/ruby/26.in
Normal file
|
|
@ -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}")
|
||||
31
test/ruby/27.in
Normal file
31
test/ruby/27.in
Normal file
|
|
@ -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")
|
||||
31
test/ruby/28.in
Normal file
31
test/ruby/28.in
Normal file
|
|
@ -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")
|
||||
34
test/ruby/29.in
Normal file
34
test/ruby/29.in
Normal file
|
|
@ -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()}")
|
||||
18
test/ruby/30.in
Normal file
18
test/ruby/30.in
Normal file
|
|
@ -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"))
|
||||
33
test/ruby/31.in
Normal file
33
test/ruby/31.in
Normal file
|
|
@ -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")
|
||||
29
test/ruby/32.in
Normal file
29
test/ruby/32.in
Normal file
|
|
@ -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}")
|
||||
30
test/ruby/33.in
Normal file
30
test/ruby/33.in
Normal file
|
|
@ -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}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue