assign
This commit is contained in:
parent
a47ef094af
commit
c516afc400
13 changed files with 240 additions and 184 deletions
|
|
@ -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()}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue