Added missing classes from final year at OSU

This commit is contained in:
2019-06-17 14:04:15 -07:00
parent 8fa1ffb1b0
commit c717a0316f
166 changed files with 653934 additions and 308 deletions

View File

@@ -0,0 +1,3 @@
To run this program, place the file "shopping.txt" in the same directory, then run
python3 shopping.py

View File

@@ -0,0 +1,61 @@
Test Case 1
Total Price 0
Member Items:
1:
2:
Test Case 2
Total Price 435
Member Items:
1: 3 4 5 6
2: 2 4 5
3: 3 4 6
4: 3 4 5
Test Case 3
Total Price 83
Member Items:
1: 3
2: 2 3
3: 1 2 3
4: 2 3 4
5: 1 2 3 4
6: 1 2 3 4
7: 2 3 5
8: 1 2 3 5
9: 2 3 4 5
10: 1 2 3 4 5
Test Case 4
Total Price 646
Member Items:
1: 1
2: 1
3: 2
4: 1 2
5: 1 5
6: 2 3
7: 7
8: 1 7
9: 10
10: 2 7
11: 1 2 7
12: 2 10
13: 2 3 7
14: 1 2 3 7
15: 7 9
16: 7 10
17: 1 7 10
18: 2 7 9
19: 2 7 10
20: 1 2 7 10
21: 2 3 7 9
22: 2 3 7 10
23: 1 2 3 7 10
24: 7 9 10
25: 2 6 7 10
26: 2 3 5 7 10
27: 2 7 9 10
28: 2 3 6 7 10
29: 1 2 3 6 7 10
30: 2 3 7 9 10

View File

@@ -0,0 +1,131 @@
INPUT_FILE = "shopping.txt"
OUTPUT_FILE = "results.txt"
def shopping_solver(max_individual_weight, shopping_items):
num_items = len(shopping_items["weights"])
# Each element of the table is (z, (a, b)) where z is the max value, and (a, b) are the x and y
# coordinates in the table for the parent that was used to create this value
lookup_table = [[(0, (0, 0)) for _ in range(num_items + 1)] for _ in range(max_individual_weight + 1)]
# Run the solver
for current_weight in range(max_individual_weight + 1):
for current_item in range(num_items + 1):
last_item_index = current_item - 1
if current_weight == 0 or current_item == 0:
lookup_table[current_weight][current_item] = 0, (0, 0)
elif shopping_items["weights"][last_item_index] <= current_weight:
included_weight = current_weight - shopping_items["weights"][last_item_index]
included = shopping_items["prices"][last_item_index] + lookup_table[included_weight][last_item_index][0]
not_included = lookup_table[current_weight][last_item_index][0]
if included > not_included:
lookup_table[current_weight][current_item] = included, (included_weight, last_item_index)
else:
lookup_table[current_weight][current_item] = not_included, (current_weight, last_item_index)
else:
lookup_table[current_weight][current_item] = \
lookup_table[current_weight][last_item_index][0], (current_weight, last_item_index)
# Trace parents to find the path
solved_path = []
last_weight_index, last_item_index = max_individual_weight, num_items
current_weight_index, current_item_index = lookup_table[max_individual_weight][num_items][1]
while [current_weight_index == 0,
current_item_index == 0,
last_weight_index == 0,
last_item_index == 0] != [True, True, True, True]:
if current_item_index != last_item_index and current_weight_index != last_weight_index:
solved_path.append(current_item_index + 1)
last_item_index = current_item_index
last_weight_index = current_weight_index
current_weight_index, current_item_index = lookup_table[current_weight_index][current_item_index][1]
return lookup_table[max_individual_weight][num_items][0], list(reversed(solved_path))
def get_tests_from_file(filename):
tests_from_file = []
with open(filename, "r") as shopping_file:
lines = shopping_file.readlines()
line_index = 0
num_tests = int(lines[line_index])
for test_number in range(num_tests):
current_test = {"items": {"weights": [], "prices": []}, "family": []}
line_index += 1
num_items = int(lines[line_index])
for item_number in range(num_items):
line_index += 1
item = lines[line_index]
item_split = item.split(" ")
current_test["items"]["weights"].append(int(item_split[1]))
current_test["items"]["prices"].append(int(item_split[0]))
line_index += 1
num_family = int(lines[line_index])
for family_number in range(num_family):
line_index += 1
current_test["family"].append(int(lines[line_index]))
tests_from_file.append(current_test)
return tests_from_file
def print_and_save_results(all_testing_results):
output_string = ""
for test_number, current_test in enumerate(all_testing_results):
output_string += "Test Case {}\n".format(test_number + 1)
output_string += "Total Price {}\n".format(current_test[0])
output_string += "Member Items:\n"
for member_number, items in enumerate(current_test[1]):
if items:
output_string += "{}: {}\n".format(member_number + 1, " ".join([str(item) for item in items]) + " ")
else:
output_string += "{}: \n".format(member_number + 1)
output_string += "\n"
print(output_string, end="")
with open(OUTPUT_FILE, "w") as output_file:
output_file.write(output_string)
if __name__ == "__main__":
tests = get_tests_from_file(INPUT_FILE)
all_test_results = []
for test in tests:
test_sum = 0
family_member_items = []
for family_member in test["family"]:
max_value, item_indexes = shopping_solver(family_member, test["items"])
test_sum += max_value
family_member_items.append(item_indexes)
all_test_results.append((test_sum, family_member_items))
print_and_save_results(all_test_results)

View File

@@ -0,0 +1,78 @@
4
2
77 7
66 6
2
5
5
6
32 16
43 12
26 4
50 8
20 3
27 9
4
25
23
21
19
5
1 1
2 1
3 1
2 2
5 5
10
1
2
3
4
5
6
7
8
9
10
10
1 1
4 3
4 3
4 4
5 4
8 6
10 7
9 7
11 8
13 9
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30