Skip to content

Commit 0a664d6

Browse files
committed
assignment 3
1 parent 3d69f0c commit 0a664d6

7 files changed

+225
-248
lines changed

main/Assignment 3.py

Lines changed: 0 additions & 248 deletions
This file was deleted.

main/calculate_caash.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def calculate_cash(file_path, start_cash):
2+
# Initialize cash variables
3+
current_cash = start_cash
4+
5+
# Read transactions from the file
6+
with open(file_path, 'r') as file:
7+
for line in file:
8+
invoice, amount, transaction_type = line.split()
9+
amount = float(amount)
10+
11+
# Update cash based on transaction type
12+
if transaction_type == 'P':
13+
current_cash += amount
14+
elif transaction_type == 'R':
15+
current_cash -= amount
16+
17+
return current_cash
18+
19+
if __name__ == "__main__":
20+
# Get user input
21+
start_cash = float(input("Enter the amount of cash at the beginning of the day: "))
22+
end_cash = float(input("Enter the expected amount of cash at the end of the day: "))
23+
file_path = input("Enter the name of the file with transactions: ")
24+
25+
# Calculate actual amount of cash at the end of the day
26+
actual_end_cash = calculate_cash(file_path, start_cash)
27+
28+
# Check if the actual amount matches the expected amount
29+
if actual_end_cash == end_cash:
30+
print("The actual amount of cash matches the expected amount.")
31+
else:
32+
print("The actual amount of cash does not match the expected amount.")

main/discount.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def discount(prices, isPet, nItems):
2+
pet_count = sum(isPet)
3+
other_items_count = nItems - pet_count
4+
5+
if pet_count >= 1 and other_items_count >= 5:
6+
other_items_total = sum(prices[i] for i in range(nItems) if not isPet[i])
7+
discount_amount = 0.2 * other_items_total
8+
return discount_amount
9+
else:
10+
return 0
11+
12+
if __name__ == "__main__":
13+
prices = []
14+
isPet = []
15+
nItems = 0
16+
17+
while True:
18+
price = float(input("Enter price (-1 to stop): "))
19+
if price == -1:
20+
break
21+
22+
pet = input("Is it a pet? (Y/N): ").upper() == 'Y'
23+
24+
prices.append(price)
25+
isPet.append(pet)
26+
nItems += 1
27+
28+
discount_amount = discount(prices, isPet, nItems)
29+
print(f"Discount Amount: ${discount_amount:.2f}")

main/invert_dict.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def invert_dict(input_dict):
2+
"""
3+
Invert the keys and values of a dictionary. Handles cases where multiple keys have the same value.
4+
5+
Args:
6+
input_dict (dict): The input dictionary.
7+
8+
Returns:
9+
dict: The inverted dictionary.
10+
"""
11+
inverted_dict = {}
12+
13+
for key, value in input_dict.items():
14+
if value in inverted_dict:
15+
inverted_dict[value].append(key)
16+
else:
17+
inverted_dict[value] = [key]
18+
19+
return inverted_dict
20+
21+
# Example usage:
22+
original_dict = {'a': 1, 'b': 2, 'c': 1, 'd': 3}
23+
inverted_result = invert_dict(original_dict)
24+
25+
print("Original Dictionary:", original_dict)
26+
print("Inverted Dictionary:", inverted_result)

0 commit comments

Comments
 (0)