Nesting Boxes ( Due 13 Jul 2020 )

Imagine a room full of boxes. Each box has a length, width, and height. Since the boxes can be rotated those terms are inter- changeable. The dimensions are integral values in a consistent system of units. The boxes have rectangular surfaces and can be nested inside each other. A box can nest inside another box if all its dimensions are strictly less than the corresponding dimensions of the other. You may only nest a box such that the corresponding surfaces are parallel to each other. A box may not be nested along the diagonal. You cannot also put two or more boxes side by side inside another box.

Input: The list of boxes is given in a file called boxes.in. The first line gives the number of boxes n. The next n lines gives a set of three integers separated by one or more spaces. These integers represent the 3 dimensions of a box. Since you can rotate the boxes, the order of the dimensions does not matter. It may be to your advantage to sort the dimensions in ascending order.

Output: The output of your code will be the largest subset of boxes that nest inside each other starting with the inner most box to the outer most box. There should be one line for each box. Your output should be written to a file called boxes.out.

Largest Subset of Nesting Boxes
(2, 2, 3)
(3, 4, 4)
(5, 5, 6)
(6, 7, 9)

If there is two or more subsets of equal lengths that qualify as being the largest subset, then print all the largest qualifying subsets with a one line space between each subset. The minimum number of boxes that qualify as nesting is 2. If there are no boxes that nest in another, then write "No Nesting Boxes" instead of "Largest Subset of Nesting Boxes".

The file that you will be submitting called Boxes.py will have the following structure. You may NOT change the names of the functions but you may add as many helper functions as needed. You will follow the standard coding conventions in Python.

# Input: box_list is a list of boxes that have already been sorted
#        sub_set is a list that is the current subset of boxes
#        idx is an index in the list box_list
#        all_box_subsets is a 3-D list that has all the subset of boxes
# Output: generates all subsets of boxes and stores them in all_box_subsets
def sub_sets_boxes (box_list, sub_set, idx, all_box_subsets):

# Input: all_box_subsets is a 3-D list that has a subset of boxes
#        largest_size keeps track what the largest subset is
# Output: goes through all the subset of boxes and only stores the
#         largest subsets that nest in the 3-D list all_nesting_boxes
def largest_nesting_subsets (all_box_subsets, largest_size, all_nesting_boxes):

# Input: box1 and box2 are the dimensions of two boxes
# Output: 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():
  # open the file for reading
  in_file = open ("./boxes.in", "r")

  # read the number of boxes
  line = in_file.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 = in_file.readline()
    line = line.strip()
    box = line.split()
    for j in range (len(box)):
      box[j] = int (box[j])
    box.sort()
    box_list.append (box)

  # close the file
  in_file.close()

  print (box_list)
  print()

  # sort the box list
  box_list.sort()
  print (box_list)
  print()

  # create an empty list to hold all subset of boxes
  all_box_subsets = []

  # create a list to hold a single subset of boxes
  sub_set = []

  # generate all subsets of boxes and store them in all_box_subsets
  sub_sets_boxes (box_list, sub_set, 0, all_box_subsets)

  # initialize the size of the largest sub-set of nesting boxes
  largest_size = 0

  # create a list to hold the largest subsets of nesting boxes
  all_nesting_boxes = []

  # go through all the subset of boxes and only store the
  # largest subsets that nest in all_nesting_boxes
  largest_nesting_subsets (all_box_subsets, largest_size, all_nesting_boxes)

  # print all the largest subset of boxes

if __name__ == "__main__":
  main()

For this assignment you may work with a partner. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. If you are working with a partner then only one of you will submit the code. Make sure that in the header in HackerRank that you have your name and UT EID and your partner's name and UT EID. If you are working alone then you will just have your name and your UT EID.

Use the HackerRank platform to submit your code. We should receive your work by 11 PM on Monday, 13 Jul 2020. There will be substantial penalties if you do not adhere to the guidelines. HackerRank will not assign late penalties (if any), we will make the adjustments.