first commit
This commit is contained in:
2
TD2-8_Make_ChangePrograms/AllTheSolutionsTest1.txt
Normal file
2
TD2-8_Make_ChangePrograms/AllTheSolutionsTest1.txt
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,3 @@
|
||||
[[5, 5, 2, 0.2, 0.1, 0.05]]
|
||||
Previous Program 3 execution time: 0.40930659999139607
|
||||
Modified Program 3 execution time: 0.580361800006358
|
||||
BIN
TD2-8_Make_ChangePrograms/TP2.zip
Normal file
BIN
TD2-8_Make_ChangePrograms/TP2.zip
Normal file
Binary file not shown.
BIN
TD2-8_Make_ChangePrograms/__pycache__/prg4.cpython-311.pyc
Normal file
BIN
TD2-8_Make_ChangePrograms/__pycache__/prg4.cpython-311.pyc
Normal file
Binary file not shown.
33
TD2-8_Make_ChangePrograms/prg1.py
Normal file
33
TD2-8_Make_ChangePrograms/prg1.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# gm
|
||||
import time
|
||||
|
||||
# Greedy : function to make change with sorted coins
|
||||
def greedy_make_change(amount, coins):
|
||||
i = 0
|
||||
change = []
|
||||
|
||||
while amount > 0 and len(coins) > i:
|
||||
# print(str(round(amount//coins[i])) + " Coins of " + str(coins[i]) + "€")
|
||||
for j in range(round(amount//coins[i])):
|
||||
change.append(coins[i])
|
||||
amount = round(amount%coins[i], 2)
|
||||
i = i+1
|
||||
if amount > 0:
|
||||
print(f"Cannot make exact change for {amount:.2f}€")
|
||||
return change
|
||||
|
||||
|
||||
# Initialisation
|
||||
L = [5,2,1,0.5,0.2,0.1,0.05]
|
||||
A = 12.35
|
||||
|
||||
start_time = time.perf_counter()
|
||||
solution = greedy_make_change(A, L)
|
||||
total_time = time.perf_counter() - start_time
|
||||
|
||||
# Print to file
|
||||
with open("sol_1.txt", "w") as f:
|
||||
f.write(str(solution))
|
||||
f.write("\n")
|
||||
f.write(str(total_time))
|
||||
|
||||
61
TD2-8_Make_ChangePrograms/prg2.py
Normal file
61
TD2-8_Make_ChangePrograms/prg2.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# aw
|
||||
import time
|
||||
|
||||
# Function to count occurrences of items in a list
|
||||
def count_occurrences(array):
|
||||
counts = {}
|
||||
for item in array:
|
||||
if item in counts:
|
||||
counts[item] += 1 # If the item is already in the dictionary, increment its count
|
||||
else:
|
||||
counts[item] = 1 # If the item is not in the dictionary, add it with a count of 1
|
||||
return counts
|
||||
|
||||
# Function to calculate all combinations of coins to make a specific amount
|
||||
def calculate_change_combinations(coins, amount):
|
||||
# Convert euro amounts to cents for calculations
|
||||
amount_cents = int(amount * 100)
|
||||
coin_values_cents = [int(coin * 100) for coin in coins]
|
||||
|
||||
# Initialize a list to store combinations and their counts
|
||||
combinations = []
|
||||
stack = [(0, [], 0)] # (current amount in cents, current combination, current coin index)
|
||||
|
||||
while stack:
|
||||
current_amount, current_combination, current_coin_index = stack.pop()
|
||||
|
||||
# If the current combination sums up to the target amount, add it to the list
|
||||
if current_amount == amount_cents:
|
||||
combinations.append(current_combination)
|
||||
# If the current amount is less than the target amount and there are more coins to consider
|
||||
elif current_amount < amount_cents and current_coin_index < len(coin_values_cents):
|
||||
coin = coin_values_cents[current_coin_index]
|
||||
max_count = (amount_cents - current_amount) // coin # Maximum count of the current coin
|
||||
|
||||
# Try adding different counts of the current coin to explore possibilities
|
||||
for count in range(max_count + 1):
|
||||
new_amount = current_amount + count * coin
|
||||
new_combination = current_combination + [coins[current_coin_index]] * count
|
||||
# Push the new state onto the stack for further exploration
|
||||
stack.append((new_amount, new_combination, current_coin_index + 1))
|
||||
|
||||
# Print the total number of combinations
|
||||
print(f"Total number of combinations: {len(combinations)}")
|
||||
return combinations
|
||||
|
||||
# Define the list of available coin denominations and the amount amount of change
|
||||
coin_list = [5, 2, 1, 0.5, 0.2, 0.1, 0.05]
|
||||
print("The types of coins we have are:", coin_list)
|
||||
change_amount = 12.35
|
||||
print("The amount of change we need to return is:", change_amount)
|
||||
|
||||
start_time = time.perf_counter()
|
||||
# Call the function to calculate and display the combinations of coins for the given amount
|
||||
combinations = calculate_change_combinations(coin_list, change_amount)
|
||||
total_time = time.perf_counter() - start_time
|
||||
|
||||
# Write the solution to a .txt file
|
||||
with open("sol_2.txt", "w") as f:
|
||||
f.write(str(combinations))
|
||||
f.write("\n\n")
|
||||
f.write("Execution time is: " + str(total_time))
|
||||
40
TD2-8_Make_ChangePrograms/prg3.py
Normal file
40
TD2-8_Make_ChangePrograms/prg3.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# km
|
||||
import math
|
||||
import time
|
||||
|
||||
def make_change_recursive(coins, amount, start, current_change, result):
|
||||
if amount == 0:
|
||||
result.append(current_change[:])
|
||||
return
|
||||
|
||||
for i in range(start, len(coins)):
|
||||
coin_cents = round(coins[i] * 100)
|
||||
if amount >= coin_cents:
|
||||
current_change.append(coins[i])
|
||||
make_change_recursive(coins, amount - coin_cents, i, current_change, result)
|
||||
current_change.pop()
|
||||
|
||||
return result
|
||||
|
||||
def main():
|
||||
coins = [5, 2, 0.2, 0.1, 0.05] # Available coin values
|
||||
target_amount = 12.35 # Change to be made
|
||||
solutions = [] # Store all valid combinations
|
||||
|
||||
start_time = time.perf_counter()
|
||||
solutions = make_change_recursive(coins, target_amount * 100, 0, [], [])
|
||||
end_time = time.perf_counter()
|
||||
|
||||
duration = end_time - start_time
|
||||
|
||||
with open("AllTheSolutionsTest1.txt", "w") as file:
|
||||
# file.write("execution time: "+str(duration)+"s\n")
|
||||
file.write(str(solutions))
|
||||
# for solution in solutions:
|
||||
# file.write(",".join(map(str, solution)) + "\n")
|
||||
# print(solution,"\n")
|
||||
|
||||
file.write("\n"+str(duration)+"\n")
|
||||
print("execution time: "+str(duration)+"s")
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
44
TD2-8_Make_ChangePrograms/prg3_bis.py
Normal file
44
TD2-8_Make_ChangePrograms/prg3_bis.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# km
|
||||
import math
|
||||
import time
|
||||
|
||||
def make_change_recursive(coins, amount, start, current_change, result):
|
||||
if amount == 0:
|
||||
result.append(current_change[:])
|
||||
return
|
||||
|
||||
for i in range(start, len(coins)):
|
||||
coin_cents = round(coins[i] * 100)
|
||||
if amount >= coin_cents:
|
||||
current_change.append(coins[i])
|
||||
make_change_recursive(coins, amount - coin_cents, i, current_change, result)
|
||||
current_change.pop()
|
||||
|
||||
return result
|
||||
|
||||
def main():
|
||||
coins = [5, 2, 0.2, 0.1, 0.05] # Available coin values
|
||||
target_amount = 12.35 # Change to be made
|
||||
solutions = [] # Store all valid combinations
|
||||
|
||||
# Sort the coins in descending order,
|
||||
# so that the highest values are used first
|
||||
coins.sort(reverse=True)
|
||||
|
||||
start_time = time.perf_counter()
|
||||
solutions = make_change_recursive(coins, target_amount * 100, 0, [], [])
|
||||
end_time = time.perf_counter()
|
||||
|
||||
duration = end_time - start_time
|
||||
|
||||
with open("AllTheSolutionsTest1.txt", "w") as file:
|
||||
# file.write("execution time: "+str(duration)+"s\n")
|
||||
file.write(str(solutions))
|
||||
# for solution in solutions:
|
||||
# file.write(",".join(map(str, solution)) + "\n")
|
||||
# print(solution,"\n")
|
||||
|
||||
file.write("\n"+str(duration)+"\n")
|
||||
print("execution time: "+str(duration)+"s")
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
80
TD2-8_Make_ChangePrograms/prg4.py
Normal file
80
TD2-8_Make_ChangePrograms/prg4.py
Normal file
@@ -0,0 +1,80 @@
|
||||
# gm
|
||||
|
||||
# You calculate all the solutions that you do not display, as you go.
|
||||
# If you store the solutions in a 2D array of solutions, in the order in which
|
||||
# the valid combinations are found, then a subsequent display following
|
||||
# that order can be compared with the contents of the CORRECT_ ... file
|
||||
# and will show no difference.
|
||||
|
||||
import time
|
||||
|
||||
# Function to calculate all combinations of coins to make a specific amount
|
||||
def calculate_change_combinations(coins, amount):
|
||||
# Convert euro amounts to cents for calculations
|
||||
amount_cents = int(amount * 100)
|
||||
coin_values_cents = [int(coin * 100) for coin in coins]
|
||||
|
||||
# Initialize a list to store combinations and their counts
|
||||
combinations = []
|
||||
stack = [(0, [], 0)] # (current amount in cents, current combination, current coin index)
|
||||
|
||||
while stack:
|
||||
current_amount, current_combination, current_coin_index = stack.pop()
|
||||
|
||||
# If the current combination sums up to the target amount, add it to the list
|
||||
if current_amount == amount_cents:
|
||||
combinations.append(current_combination)
|
||||
|
||||
# If the current amount is less than the target amount and there are more coins to consider
|
||||
elif current_amount < amount_cents and current_coin_index < len(coin_values_cents):
|
||||
coin = coin_values_cents[current_coin_index]
|
||||
max_count = (amount_cents - current_amount) // coin # Maximum count of the current coin
|
||||
|
||||
# Try adding different counts of the current coin to explore possibilities
|
||||
for count in range(max_count + 1):
|
||||
new_amount = current_amount + count * coin
|
||||
new_combination = current_combination + [coins[current_coin_index]] * count
|
||||
# Push the new state onto the stack for further exploration
|
||||
stack.append((new_amount, new_combination, current_coin_index + 1))
|
||||
|
||||
return combinations
|
||||
|
||||
# Define the list of available coin denominations and the amount amount of change
|
||||
L = [5, 2, 1, 0.5, 0.2, 0.1, 0.05]
|
||||
A = 12.35
|
||||
|
||||
start = time.perf_counter()
|
||||
solution = calculate_change_combinations(L, A)
|
||||
total_time = time.perf_counter() - start
|
||||
|
||||
with open("sol_4.txt", "w") as f:
|
||||
f.write(str(solution))
|
||||
f.write("\n")
|
||||
f.write(str(total_time))
|
||||
|
||||
|
||||
# Read CORRECT_ ... file and compare with the contents of the list of solutions
|
||||
# Do not consider the last line
|
||||
with open("AllTheSolutionsTest1.txt", "r") as f:
|
||||
correct_solution = f.read().splitlines()[:-1]
|
||||
|
||||
|
||||
# Sort each array on correct_solution and solution
|
||||
for i in range(len(solution)):
|
||||
solution[i].sort()
|
||||
|
||||
for i in range(len(correct_solution)):
|
||||
correct_solution[i] = correct_solution[i].replace("[", "").replace("]", "").replace(",", "").split()
|
||||
correct_solution[i].sort()
|
||||
|
||||
# Check if each element of correct_solution is in solution
|
||||
missing = 0
|
||||
missing_solution = []
|
||||
for i in range(len(correct_solution)):
|
||||
if correct_solution[i] not in solution:
|
||||
print("ERROR: solution not found")
|
||||
missing +=1
|
||||
missing_solution.append(correct_solution[i])
|
||||
|
||||
print("Number of missing solutions: ", missing)
|
||||
# print("Missing solutions: ", missing_solution)
|
||||
81
TD2-8_Make_ChangePrograms/prg5.py
Normal file
81
TD2-8_Make_ChangePrograms/prg5.py
Normal file
@@ -0,0 +1,81 @@
|
||||
# km
|
||||
|
||||
# modify program 3 so as to evaluate the cost of a solution
|
||||
# according to the number of units of the values. Calculate the best
|
||||
# solution according to this cost by displaying successive values that
|
||||
# improve. Display the trace of this execution in a file
|
||||
# CORRECT_SolutionsWhichImproveIncrementally.txt
|
||||
# Calculate the number of solutions displayed, and the display economy,
|
||||
# using the wc –l command. Calculate with the time, or date command,
|
||||
# the execution time programs 3 and 5
|
||||
|
||||
# wc -l command ?
|
||||
|
||||
import math
|
||||
import time
|
||||
|
||||
def make_change_recursive_prg3(coins, amount, start, current_change, result):
|
||||
if amount == 0:
|
||||
result.append(current_change[:])
|
||||
return
|
||||
|
||||
for i in range(start, len(coins)):
|
||||
coin_cents = round(coins[i] * 100)
|
||||
if amount >= coin_cents:
|
||||
current_change.append(coins[i])
|
||||
make_change_recursive_prg3(coins, amount - coin_cents, i, current_change, result)
|
||||
current_change.pop()
|
||||
|
||||
return result
|
||||
|
||||
def calculate_solution_cost(solution):
|
||||
# Calculate the cost of a solution as the sum of units of values
|
||||
cost = sum([round(coin * 100) for coin in solution])
|
||||
return cost
|
||||
|
||||
def make_change_recursive(coins, amount, start, current_change, result, best_cost):
|
||||
if amount == 0:
|
||||
current_cost = calculate_solution_cost(current_change)
|
||||
if current_cost < best_cost[0]:
|
||||
result.append(current_change[:])
|
||||
best_cost[0] = current_cost
|
||||
return
|
||||
|
||||
for i in range(start, len(coins)):
|
||||
coin_cents = round(coins[i] * 100)
|
||||
if amount >= coin_cents:
|
||||
current_change.append(coins[i])
|
||||
make_change_recursive(coins, amount - coin_cents, i, current_change, result, best_cost)
|
||||
current_change.pop()
|
||||
|
||||
def main():
|
||||
coins = [5, 2, 0.2, 0.1, 0.05] # Available coin values
|
||||
target_amount = 12.35 # Change to be made
|
||||
solutions = [] # Store all valid combinations
|
||||
best_cost = [float('inf')] # Initialize with a very high cost
|
||||
|
||||
start_time_prg3 = time.perf_counter()
|
||||
solutions = make_change_recursive_prg3(coins, target_amount * 100, 0, [], [])
|
||||
end_time_prg3 = time.perf_counter()
|
||||
duration_prg3 = end_time_prg3 - start_time_prg3
|
||||
|
||||
solutions = []
|
||||
start_time = time.perf_counter()
|
||||
make_change_recursive(coins, target_amount * 100, 0, [], solutions, best_cost)
|
||||
end_time = time.perf_counter()
|
||||
|
||||
duration = end_time - start_time
|
||||
|
||||
with open("CORRECT_SolutionsWhichImproveIncrementally.txt", "w") as file:
|
||||
file.write(str(solutions))
|
||||
file.write("\nPrevious Program 3 execution time: " + str(duration_prg3))
|
||||
file.write("\nModified Program 3 execution time: " + str(duration))
|
||||
# for solution in solutions:
|
||||
# file.write(",".join(map(str, solution)) + "\n")
|
||||
|
||||
print("Previous Program 3 execution time: " + str(duration_prg3) + "\n")
|
||||
print("Modified Program 3 execution time: " + str(duration) + "\n")
|
||||
print("Total solutions displayed:", len(solutions))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
64
TD2-8_Make_ChangePrograms/prg6.py
Normal file
64
TD2-8_Make_ChangePrograms/prg6.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# modify program 5 or program 4 so that the best calculated
|
||||
# solution is stored in a solution table. Evaluate the computation time
|
||||
# saved and the display savings.
|
||||
|
||||
import time
|
||||
|
||||
# Function to calculate all combinations of coins to make a specific amount
|
||||
def calculate_change_combinations(coins, amount):
|
||||
# Convert euro amounts to cents for calculations
|
||||
amount_cents = int(amount * 100)
|
||||
coin_values_cents = [int(coin * 100) for coin in coins]
|
||||
|
||||
# Initialize a list to store combinations and their counts
|
||||
combinations = []
|
||||
stack = [(0, [], 0)] # (current amount in cents, current combination, current coin index)
|
||||
|
||||
best_solution = [[] for i in range(len(coins))]
|
||||
|
||||
while stack:
|
||||
current_amount, current_combination, current_coin_index = stack.pop()
|
||||
|
||||
# If the current combination sums up to the target amount, add it to the list
|
||||
if current_amount == amount_cents:
|
||||
if len(current_combination) < len(best_solution):
|
||||
best_solution = current_combination
|
||||
|
||||
# If the current amount is less than the target amount and there are more coins to consider
|
||||
elif current_amount < amount_cents and current_coin_index < len(coin_values_cents):
|
||||
coin = coin_values_cents[current_coin_index]
|
||||
max_count = (amount_cents - current_amount) // coin # Maximum count of the current coin
|
||||
|
||||
# Try adding different counts of the current coin to explore possibilities
|
||||
for count in range(max_count + 1):
|
||||
new_amount = current_amount + count * coin
|
||||
new_combination = current_combination + [coins[current_coin_index]] * count
|
||||
# Push the new state onto the stack for further exploration
|
||||
stack.append((new_amount, new_combination, current_coin_index + 1))
|
||||
|
||||
return best_solution
|
||||
|
||||
# Define the list of available coin denominations and the amount amount of change
|
||||
L = [5, 2, 1, 0.5, 0.2, 0.1, 0.05]
|
||||
A = 12.35
|
||||
|
||||
start = time.perf_counter()
|
||||
solution = calculate_change_combinations(L, A)
|
||||
|
||||
with open("sol_4-6.txt", "w") as f:
|
||||
f.write(str(solution))
|
||||
f.write("\n")
|
||||
|
||||
total_time_3 = time.perf_counter() - start
|
||||
|
||||
start = time.perf_counter()
|
||||
solution = calculate_change_combinations(L, A)
|
||||
total_time_6 = time.perf_counter() - start
|
||||
|
||||
print("Solution: ", solution)
|
||||
print("Total time for program 3: ", total_time_3)
|
||||
print("Total time for program 6: ", total_time_6)
|
||||
|
||||
|
||||
|
||||
|
||||
68
TD2-8_Make_ChangePrograms/prg7.py
Normal file
68
TD2-8_Make_ChangePrograms/prg7.py
Normal file
@@ -0,0 +1,68 @@
|
||||
# modify program 5 or program 4 so that the best calculated
|
||||
# solution is stored in a solution table. Evaluate the computation time
|
||||
# saved and the display savings.
|
||||
|
||||
import time
|
||||
|
||||
# Function to calculate all combinations of coins to make a specific amount
|
||||
def calculate_change_combinations(coins, amount):
|
||||
# Convert euro amounts to cents for calculations
|
||||
amount_cents = int(amount * 100)
|
||||
coin_values_cents = [int(coin * 100) for coin in coins]
|
||||
|
||||
# Initialize a list to store combinations and their counts
|
||||
combinations = []
|
||||
stack = [(0, [], 0)] # (current amount in cents, current combination, current coin index)
|
||||
|
||||
best_solution = [[] for i in range(100)]
|
||||
|
||||
increment = 0
|
||||
|
||||
while stack:
|
||||
current_amount, current_combination, current_coin_index = stack.pop()
|
||||
|
||||
# If the current combination sums up to the target amount, add it to the list
|
||||
if current_amount == amount_cents:
|
||||
if len(current_combination) < len(best_solution):
|
||||
best_solution = current_combination
|
||||
else:
|
||||
increment += 1
|
||||
|
||||
# If we don't improve our result 5 times in a row, we cut
|
||||
if (increment >= 5):
|
||||
return best_solution
|
||||
|
||||
# If the current amount is less than the target amount and there are more coins to consider
|
||||
elif current_amount < amount_cents and current_coin_index < len(coin_values_cents):
|
||||
coin = coin_values_cents[current_coin_index]
|
||||
max_count = (amount_cents - current_amount) // coin # Maximum count of the current coin
|
||||
|
||||
# Try adding different counts of the current coin to explore possibilities
|
||||
for count in range(max_count + 1):
|
||||
new_amount = current_amount + count * coin
|
||||
new_combination = current_combination + [coins[current_coin_index]] * count
|
||||
# Push the new state onto the stack for further exploration
|
||||
stack.append((new_amount, new_combination, current_coin_index + 1))
|
||||
|
||||
return best_solution
|
||||
|
||||
# Define the list of available coin denominations and the amount amount of change
|
||||
L = [5, 2, 1, 0.5, 0.2, 0.1, 0.05]
|
||||
A = 12.35
|
||||
|
||||
start = time.perf_counter()
|
||||
solution = calculate_change_combinations(L, A)
|
||||
|
||||
with open("sol_4-7.txt", "w") as f:
|
||||
f.write(str(solution))
|
||||
f.write("\n")
|
||||
|
||||
total_time_3 = time.perf_counter() - start
|
||||
|
||||
start = time.perf_counter()
|
||||
solution = calculate_change_combinations(L, A)
|
||||
total_time_7 = time.perf_counter() - start
|
||||
|
||||
print("Solution: ", solution)
|
||||
print("Total time for program 3: ", total_time_3)
|
||||
print("Total time for program 7: ", total_time_7)
|
||||
2
TD2-8_Make_ChangePrograms/sol_1.txt
Normal file
2
TD2-8_Make_ChangePrograms/sol_1.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
[5, 5, 2, 0.2, 0.1, 0.05]
|
||||
2.6099999999997653e-05
|
||||
3
TD2-8_Make_ChangePrograms/sol_2.txt
Normal file
3
TD2-8_Make_ChangePrograms/sol_2.txt
Normal file
File diff suppressed because one or more lines are too long
2
TD2-8_Make_ChangePrograms/sol_3.txt
Normal file
2
TD2-8_Make_ChangePrograms/sol_3.txt
Normal file
File diff suppressed because one or more lines are too long
2
TD2-8_Make_ChangePrograms/sol_3_bis.txt
Normal file
2
TD2-8_Make_ChangePrograms/sol_3_bis.txt
Normal file
File diff suppressed because one or more lines are too long
1
TD2-8_Make_ChangePrograms/sol_4-6.txt
Normal file
1
TD2-8_Make_ChangePrograms/sol_4-6.txt
Normal file
@@ -0,0 +1 @@
|
||||
[]
|
||||
1
TD2-8_Make_ChangePrograms/sol_4-7.txt
Normal file
1
TD2-8_Make_ChangePrograms/sol_4-7.txt
Normal file
@@ -0,0 +1 @@
|
||||
[5, 5, 2, 0.2, 0.1, 0.05]
|
||||
2
TD2-8_Make_ChangePrograms/sol_4.txt
Normal file
2
TD2-8_Make_ChangePrograms/sol_4.txt
Normal file
File diff suppressed because one or more lines are too long
3
TD2-8_Make_ChangePrograms/sol_5.txt
Normal file
3
TD2-8_Make_ChangePrograms/sol_5.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
[[5, 5, 2, 0.2, 0.1, 0.05]]
|
||||
Previous Program 3 execution time: 0.4225550999981351
|
||||
Modified Program 3 execution time: 0.5911691000073915
|
||||
Reference in New Issue
Block a user