assign
This commit is contained in:
parent
a47ef094af
commit
c516afc400
13 changed files with 240 additions and 184 deletions
|
|
@ -5,7 +5,7 @@ def count_words(text):
|
|||
for word in words:
|
||||
word = word.strip('.,!?')
|
||||
if word in word_count:
|
||||
word_count[word] = word_count[word] + 1
|
||||
word_count[word] += 1
|
||||
else:
|
||||
word_count[word] = 1
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ def count_words(text)
|
|||
for word in words
|
||||
word = word.delete('.,!?')
|
||||
if __contains__(word_count, word)
|
||||
word_count[word] = word_count[word] + 1
|
||||
word_count[word] += 1
|
||||
else
|
||||
word_count[word] = 1
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,31 +1,34 @@
|
|||
def count_words(text):
|
||||
words = text.lower().split()
|
||||
word_count = {}
|
||||
class BankAccount:
|
||||
def __init__(self, owner, balance=0):
|
||||
self.owner = owner
|
||||
self.balance = balance
|
||||
|
||||
for word in words:
|
||||
word = word.strip('.,!?')
|
||||
if word in word_count:
|
||||
word_count[word] = word_count[word] + 1
|
||||
else:
|
||||
word_count[word] = 1
|
||||
def deposit(self, amount):
|
||||
if amount > 0:
|
||||
self.balance += amount
|
||||
return True
|
||||
return False
|
||||
|
||||
return word_count
|
||||
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})"
|
||||
|
||||
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
|
||||
account = BankAccount("Alice", 1000)
|
||||
print(account)
|
||||
|
||||
text = "Hello world hello there world hello"
|
||||
counts = count_words(text)
|
||||
common_word, frequency = find_most_common(counts)
|
||||
print(f"Deposit $500: {account.deposit(500)}")
|
||||
print(f"Balance: ${account.get_balance()}")
|
||||
|
||||
print(f"Text: {text}")
|
||||
print(f"Word counts: {counts}")
|
||||
print(f"Most common: '{common_word}' appears {frequency} times")
|
||||
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()}")
|
||||
|
|
@ -1,34 +1,18 @@
|
|||
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})"
|
||||
def write_to_file(filename, content):
|
||||
with open(filename, 'w') as file:
|
||||
file.write(content)
|
||||
print(f"Written to {filename}")
|
||||
|
||||
account = BankAccount("Alice", 1000)
|
||||
print(account)
|
||||
def read_from_file(filename):
|
||||
try:
|
||||
with open(filename, 'r') as file:
|
||||
content = file.read()
|
||||
return content
|
||||
except FileNotFoundError:
|
||||
return "File not found"
|
||||
|
||||
print(f"Deposit $500: {account.deposit(500)}")
|
||||
print(f"Balance: ${account.get_balance()}")
|
||||
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(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()}")
|
||||
print(read_from_file("nonexistent.txt"))
|
||||
|
|
@ -1,18 +1,33 @@
|
|||
def write_to_file(filename, content):
|
||||
with open(filename, 'w') as file:
|
||||
file.write(content)
|
||||
print(f"Written to {filename}")
|
||||
import math
|
||||
|
||||
def read_from_file(filename):
|
||||
try:
|
||||
with open(filename, 'r') as file:
|
||||
content = file.read()
|
||||
return content
|
||||
except FileNotFoundError:
|
||||
return "File not found"
|
||||
def circle_area(radius):
|
||||
return math.pi * radius ** 2
|
||||
|
||||
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}")
|
||||
def circle_circumference(radius):
|
||||
return 2 * math.pi * radius
|
||||
|
||||
print(read_from_file("nonexistent.txt"))
|
||||
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")
|
||||
|
|
@ -1,33 +1,29 @@
|
|||
import math
|
||||
def countdown(n):
|
||||
while n > 0:
|
||||
print(f"Countdown: {n}")
|
||||
n -= 1
|
||||
print("Blast off!")
|
||||
|
||||
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
|
||||
def sum_until_negative():
|
||||
total = 0
|
||||
count = 0
|
||||
inputs = [5, 3, 8, -1, 4, 2]
|
||||
|
||||
if discriminant < 0:
|
||||
return None, None
|
||||
elif discriminant == 0:
|
||||
x = -b / (2 * a)
|
||||
return x, x
|
||||
for value in inputs:
|
||||
if value < 0:
|
||||
break
|
||||
total += value
|
||||
count += 1
|
||||
|
||||
if count > 0:
|
||||
average = total / count
|
||||
return total, average
|
||||
else:
|
||||
x1 = (-b + math.sqrt(discriminant)) / (2 * a)
|
||||
x2 = (-b - math.sqrt(discriminant)) / (2 * a)
|
||||
return x1, x2
|
||||
return 0, 0
|
||||
|
||||
print(f"Circle with radius 5:")
|
||||
print(f" Area: {circle_area(5):.2f}")
|
||||
print(f" Circumference: {circle_circumference(5):.2f}")
|
||||
print("Countdown from 5:")
|
||||
countdown(5)
|
||||
|
||||
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")
|
||||
print("\nSum until negative:")
|
||||
total, avg = sum_until_negative()
|
||||
print(f"Total: {total}, Average: {avg:.2f}")
|
||||
|
|
@ -1,29 +1,30 @@
|
|||
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]
|
||||
def analyze_numbers(numbers):
|
||||
if not numbers:
|
||||
return 0, 0, 0
|
||||
|
||||
for value in inputs:
|
||||
if value < 0:
|
||||
break
|
||||
total += value
|
||||
count += 1
|
||||
total = sum(numbers)
|
||||
average = total / len(numbers)
|
||||
|
||||
if count > 0:
|
||||
average = total / count
|
||||
return total, average
|
||||
else:
|
||||
return 0, 0
|
||||
positive_count = 0
|
||||
for num in numbers:
|
||||
if num > 0:
|
||||
positive_count += 1
|
||||
|
||||
return total, average, positive_count
|
||||
|
||||
print("Countdown from 5:")
|
||||
countdown(5)
|
||||
def get_coordinates():
|
||||
x = 10
|
||||
y = 20
|
||||
z = 30
|
||||
return x, y, z
|
||||
|
||||
print("\nSum until negative:")
|
||||
total, avg = sum_until_negative()
|
||||
print(f"Total: {total}, Average: {avg:.2f}")
|
||||
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}")
|
||||
|
|
@ -1,30 +1,31 @@
|
|||
def analyze_numbers(numbers):
|
||||
if not numbers:
|
||||
return 0, 0, 0
|
||||
def count_words(text):
|
||||
words = text.lower().split()
|
||||
word_count = {}
|
||||
|
||||
total = sum(numbers)
|
||||
average = total / len(numbers)
|
||||
for word in words:
|
||||
word = word.strip('.,!?')
|
||||
if word in word_count:
|
||||
word_count[word] = word_count[word] + 1
|
||||
else:
|
||||
word_count[word] = 1
|
||||
|
||||
positive_count = 0
|
||||
for num in numbers:
|
||||
if num > 0:
|
||||
positive_count += 1
|
||||
return word_count
|
||||
|
||||
def find_most_common(word_count):
|
||||
most_common = None
|
||||
max_count = 0
|
||||
|
||||
return total, average, positive_count
|
||||
for word, count in word_count.items():
|
||||
if count > max_count:
|
||||
most_common = word
|
||||
max_count = count
|
||||
|
||||
return most_common, max_count
|
||||
|
||||
def get_coordinates():
|
||||
x = 10
|
||||
y = 20
|
||||
z = 30
|
||||
return x, y, z
|
||||
text = "Hello world hello there world hello"
|
||||
counts = count_words(text)
|
||||
common_word, frequency = find_most_common(counts)
|
||||
|
||||
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}")
|
||||
print(f"Text: {text}")
|
||||
print(f"Word counts: {counts}")
|
||||
print(f"Most common: '{common_word}' appears {frequency} times")
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
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")
|
||||
Loading…
Add table
Add a link
Reference in a new issue