-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxes.py
More file actions
133 lines (85 loc) · 3.5 KB
/
Boxes.py
File metadata and controls
133 lines (85 loc) · 3.5 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File: Boxes.py
# Description:
# Student Name: Sashi Ayyalasomayajula
# Student UT EID: sa55465
# Partner Name:
# Partner UT EID:
# Course Name: CS 313E
# Unique Number:
# Date Created:
# Date Last Modified:
import sys
# Input: 2-D list of boxes. Each box of three dimensions is sorted
# box_list is sorted
# Output: function returns two numbers, the maximum number of boxes
# that fit inside each other and the number of such nesting
# sets of boxes
def nesting_boxes(box_list):
n = len(box_list) #num of boxes
N_i = [0 for i in range (n)] #fills nest_level list with temp zeros
R_i = [0 for j in range (n)]#fills R_i list with temp zeros
for i in range(0, n): # outer for loop goes through all of the boxes in increasing order of the singular dimension
j = i - 1 #sets j as a relative variable as the previous index at every i
curr_max = 0
while j >= 0:
temp_nest = N_i[j]
if does_fit(box_list[j], box_list[i]): #while j is a box in boxlist, if box at j fits in the box at i, sets the nest level equal to temp nest
nest_level = temp_nest
if nest_level >= curr_max: #sets curr_max to the local highest nest level
curr_max = nest_level
j -= 1
x = i - 1 #sets x as a relative variable as the previous index at every i
while x >= 0:
curr_nest_level = N_i[x]
temp = curr_max
if does_fit(box_list[x], box_list[i]): # while box list at x exists, if it fits in box list at i, add the index to R_i if if condition is met
if curr_nest_level == temp:
R_i[i] += R_i[x]
x -= 1
N_i[i] = (curr_max+1) #resets current value of nest level at i
if R_i[i] == 0: #if there are no boxes that fit into the current, change R_i at the index from zero to one to account for the box.
R_i[i] = 1
N = len(N_i)
R = len(R_i)
highest_nest = max(N_i) # finds value of the largest box that can nest the highest number of smaller boxes,
num_sets = 0 #place holder
for i in range(R): #goes through R_i
if N_i[i] == highest_nest: #adds R_i to numsets if the current nest value equals the max nest value
num_sets += R_i[i]
return highest_nest, num_sets
# returns True if box1 fits inside box2
def does_fit (box1, box2):
return (box1[0] < box2[0] and box1[1] < box2[1] and box1[2] < box2[2])
def main():
# read the number of boxes
line = sys.stdin.readline()
line = line.strip()
num_boxes = int (line)
# create an empty list for the boxes
box_list = []
# read the boxes from the file
for i in range (num_boxes):
line = sys.stdin.readline()
line = line.strip()
box = line.split()
for j in range (len(box)):
box[j] = int (box[j])
box.sort()
box_list.append (box)
# print to make sure that the input was read in correctly
#print (box_list)
print()
# sort the box list
box_list.sort()
# print the box_list to see if it has been sorted.
#print (box_list)
print()
# get the maximum number of nesting boxes and the
# number of sets that have that maximum number of boxes
max_boxes, num_sets = nesting_boxes (box_list)
# print the largest number of boxes that fit
print (max_boxes)
# print the number of sets of such boxes
print (num_sets)
if __name__ == "__main__":
main()