Adding into a Linked List of Containers












2














Programming is not my strongest suit and though the due date for this assignment was not met, I want to at least learn how this works before retaking the course for next year.



The whole point of the program is to be a shipyard management system that handles new packages that are arranged in containers which cannot go over 2000 lbs else it creates a new container for the same destination.



EDIT 3: This is the updated code. Sorry for not clarifying!! :<



class Shipyard:
def __init__(self):
self._cont = self.Container(None, None, None, None)
self._size = 0

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# adds package when container check is done

# If shipyard is empty or container name is alphabetically lower than
# the first container by destination name.

if self.isEmpty() or dest < self._cont._first._dest:
self._size += 1
self._cont._first = self.Container(owner, dest,
weight, self._cont._first)
self._cont._first.add_pack(owner, dest, weight)

return

# Else will go through the contents of the linkedlist to find the spot
# it belongs to.

cur_Cont = self._cont._first


while(cur_Cont._next != None and dest > cur_Cont._next._dest):
cur_Cont = cur_Cont._next

if dest == cur_Cont._dest:
if (cur_Cont._weight + weight) > 2000:
self._size += 1
cur_Cont._next = self.Container(owner, dest,
weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)
return

if (cur_Cont._weight + weight) <= 2000:
cur_Cont.add_pack(owner, dest, weight)
return

if (cur_Cont._next._weight + weight) <= 2000:
cur_Cont._next.add_pack(owner, dest, weight)
return


self._size += 1

# Creates a new container with the given sepcifications.
cur_Cont._next = self.Container(owner, dest, weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)

return

def traversePrint(self) : # Use for Printing Container
cur_Cont = self._cont._first # Modify to include details
c_weight = cur_Cont._weight

while cur_Cont != None: # of container or packages
c_weight = cur_Cont._weight
print(cur_Cont._dest, c_weight)
cur_Cont.traversePrint()
cur_Cont = cur_Cont._next


# -----------------------------------------------------------------------------
# Nested within the Shipyard

class Container:
def __init__(self, owner, dest, weight, next):
self._owner = owner
self._dest = dest
self._first = None
self._size = 0
self._weight = 0
self._next = next
self._pack = self.Packages(None, None, None, None)

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# Add pack for container class which adds a package.

if self.isEmpty() or weight < self._pack._first._weight:
self._size += 1
self._pack._first = self.Packages(owner, dest,
weight, self._pack._first)
self._weight += weight
return

cur_Pack = self._pack._first

while(cur_Pack._next != None and weight > cur_Pack._next._weight):
cur_Pack = cur_Pack._next

if weight == cur_Pack._weight:
self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight
return

self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight

return

def traversePrint(self):
cur_Pack = self._pack._first # Modify to include details

p_weight = cur_Pack._weight

while cur_Pack != None: # of container or packages
p_weight = cur_Pack._weight
if cur_Pack._dest == self._pack._dest:
print(cur_Pack._owner, cur_Pack._dest, p_weight, end = " ")
cur_Pack = cur_Pack._next
else:
print(cur_Pack._owner, cur_Pack._dest, p_weight)
cur_Pack = cur_Pack._next

# -----------------------------------------------------------------------------
# Nested within Container doesn't contain any methods

class Packages:

def __init__(self, owner, dest, weight, next):
self._first = None
self._size = 0
self._owner = owner
self._dest = dest
self._weight = weight
self._next = next

# -----------------------------------------------------------------------------

class Weight(Exception):
pass

# -----------------------------------------------------------------------------
shipyard = Shipyard()

len(shipyard)

shipyard.add_pack("Lori", "Bristol", 300)
shipyard.add_pack("Mallory", "Bristol", 200)
shipyard.add_pack("Chung", "Alabama", 700)
shipyard.add_pack("Roger", "Alabama", 900)
shipyard.add_pack("Ali", "Giza", 45)
shipyard.add_pack("Soumick", "New Delhi", 600)
shipyard.add_pack("Mizuki", "Tokyo", 1900)
shipyard.add_pack("Tadashi", "Tokyo", 120)
shipyard.add_pack("Kanna", "Tokyo", 220)


shipyard.traversePrint()


This is what appears when program is ran:



Alabama 1600
Chung Alabama 700
Roger Alabama 900
Bristol 500
Mallory Bristol 200
Lori Bristol 300
Giza 45
Ali Giza 45
New Delhi 600
Soumick New Delhi 600
Tokyo 220
Kanna Tokyo 220
Tokyo 120 # THIS AND Tokyo 220 must be added into the same container
Tadashi Tokyo 120 # I don't know what I didn't include because
Tokyo 1900 # It seems to me that Alabama 700 and Alabama 900 were
Mizuki Tokyo 1900 # Adding Correctly.


PROBLEM EDIT: Packages that should be adding aren't adding up together and instead creates an entirely new container. I don't know what part of the code is messing up now T_T.



If there is a question please ask me in the comments below.



Thank you for reading.



Edit: I plan to use an additional add_pack(owner, dest, weight) in the Container section to hold the owner input/parameter and will be called in the Shipyard version of add_pack(owner,dest, weight)



Edit 2: SO I was able to fix the initial problem but now I have another problem/question. Yes, the school required us to use LinkedLists else we didn't get marks for it. Now my new problem is I don't know if I should update the weight of the container class in the Shipyard method add_pack or if I should update it when using add_pack in the Container class.



Edit 3.5: So I updated the code and now it clearly shows the two add_pack methods: One in the Shipyard class and one in the Container class. Sorry for bad edits and formatting. I'm just really frustrated at how I can't figure this thing out.



Edit 4: I've got both traversePrint on Container and Shipyard. Shipyard's traversePrint lists the Containers and Container's traversePrint lists the packages within the containers. I'm able to display the packages w/c leads me to believe they are being added correctly.



Edit 5: I've mostly figured out how to add containers, and packages into containers but some condition checking i.e. If weight of two things add over 2000 lbs else add into the same one <- this isn't working.










share|improve this question




















  • 1




    In your container creation method, can you just call the method again to create a second container if needed? If you're having trouble with this, can you show what is causing the issue or what part of the problem you're getting stuck on?
    – Unsolved Cypher
    Nov 22 at 6:57






  • 1




    A couple of questions: what happens if the container is still not full but by adding a new package the total weight is more than 2000 lbs? And also: where is the owner information stored?
    – toti08
    Nov 22 at 13:28










  • @toti08 it should create a new container under the same destination name. The owner information will be stored in the Packages class w/c I don't need here. I only wanted to know how to implement adding containers. Conditions are: a. If containers will go over 2000 then the package is moved to a new container with the same destination name. b. If a package can fit into an existing container then it will be added inside without creating a newer one.
    – bluekozo
    Nov 22 at 14:17












  • @UnsolvedCypher My problem with this is I can't seem to alter the code and make it so that say adding a package to the same container also adds the weight. i.e. like in the result section w/c shows Alabama 700 and Alabama 900 it should've resulted in Alabama 1600. Tokyo 120 and Tokyo 1900 should've triggered a condition in which it would have only created the second Tokyo container because adding their weight will go over 2000 lbs which is what is required for this program.
    – bluekozo
    Nov 22 at 14:19










  • Is there any reason for using a linked list here? Why not stick with python's built in list type?
    – xtofl
    Nov 22 at 14:39
















2














Programming is not my strongest suit and though the due date for this assignment was not met, I want to at least learn how this works before retaking the course for next year.



The whole point of the program is to be a shipyard management system that handles new packages that are arranged in containers which cannot go over 2000 lbs else it creates a new container for the same destination.



EDIT 3: This is the updated code. Sorry for not clarifying!! :<



class Shipyard:
def __init__(self):
self._cont = self.Container(None, None, None, None)
self._size = 0

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# adds package when container check is done

# If shipyard is empty or container name is alphabetically lower than
# the first container by destination name.

if self.isEmpty() or dest < self._cont._first._dest:
self._size += 1
self._cont._first = self.Container(owner, dest,
weight, self._cont._first)
self._cont._first.add_pack(owner, dest, weight)

return

# Else will go through the contents of the linkedlist to find the spot
# it belongs to.

cur_Cont = self._cont._first


while(cur_Cont._next != None and dest > cur_Cont._next._dest):
cur_Cont = cur_Cont._next

if dest == cur_Cont._dest:
if (cur_Cont._weight + weight) > 2000:
self._size += 1
cur_Cont._next = self.Container(owner, dest,
weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)
return

if (cur_Cont._weight + weight) <= 2000:
cur_Cont.add_pack(owner, dest, weight)
return

if (cur_Cont._next._weight + weight) <= 2000:
cur_Cont._next.add_pack(owner, dest, weight)
return


self._size += 1

# Creates a new container with the given sepcifications.
cur_Cont._next = self.Container(owner, dest, weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)

return

def traversePrint(self) : # Use for Printing Container
cur_Cont = self._cont._first # Modify to include details
c_weight = cur_Cont._weight

while cur_Cont != None: # of container or packages
c_weight = cur_Cont._weight
print(cur_Cont._dest, c_weight)
cur_Cont.traversePrint()
cur_Cont = cur_Cont._next


# -----------------------------------------------------------------------------
# Nested within the Shipyard

class Container:
def __init__(self, owner, dest, weight, next):
self._owner = owner
self._dest = dest
self._first = None
self._size = 0
self._weight = 0
self._next = next
self._pack = self.Packages(None, None, None, None)

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# Add pack for container class which adds a package.

if self.isEmpty() or weight < self._pack._first._weight:
self._size += 1
self._pack._first = self.Packages(owner, dest,
weight, self._pack._first)
self._weight += weight
return

cur_Pack = self._pack._first

while(cur_Pack._next != None and weight > cur_Pack._next._weight):
cur_Pack = cur_Pack._next

if weight == cur_Pack._weight:
self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight
return

self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight

return

def traversePrint(self):
cur_Pack = self._pack._first # Modify to include details

p_weight = cur_Pack._weight

while cur_Pack != None: # of container or packages
p_weight = cur_Pack._weight
if cur_Pack._dest == self._pack._dest:
print(cur_Pack._owner, cur_Pack._dest, p_weight, end = " ")
cur_Pack = cur_Pack._next
else:
print(cur_Pack._owner, cur_Pack._dest, p_weight)
cur_Pack = cur_Pack._next

# -----------------------------------------------------------------------------
# Nested within Container doesn't contain any methods

class Packages:

def __init__(self, owner, dest, weight, next):
self._first = None
self._size = 0
self._owner = owner
self._dest = dest
self._weight = weight
self._next = next

# -----------------------------------------------------------------------------

class Weight(Exception):
pass

# -----------------------------------------------------------------------------
shipyard = Shipyard()

len(shipyard)

shipyard.add_pack("Lori", "Bristol", 300)
shipyard.add_pack("Mallory", "Bristol", 200)
shipyard.add_pack("Chung", "Alabama", 700)
shipyard.add_pack("Roger", "Alabama", 900)
shipyard.add_pack("Ali", "Giza", 45)
shipyard.add_pack("Soumick", "New Delhi", 600)
shipyard.add_pack("Mizuki", "Tokyo", 1900)
shipyard.add_pack("Tadashi", "Tokyo", 120)
shipyard.add_pack("Kanna", "Tokyo", 220)


shipyard.traversePrint()


This is what appears when program is ran:



Alabama 1600
Chung Alabama 700
Roger Alabama 900
Bristol 500
Mallory Bristol 200
Lori Bristol 300
Giza 45
Ali Giza 45
New Delhi 600
Soumick New Delhi 600
Tokyo 220
Kanna Tokyo 220
Tokyo 120 # THIS AND Tokyo 220 must be added into the same container
Tadashi Tokyo 120 # I don't know what I didn't include because
Tokyo 1900 # It seems to me that Alabama 700 and Alabama 900 were
Mizuki Tokyo 1900 # Adding Correctly.


PROBLEM EDIT: Packages that should be adding aren't adding up together and instead creates an entirely new container. I don't know what part of the code is messing up now T_T.



If there is a question please ask me in the comments below.



Thank you for reading.



Edit: I plan to use an additional add_pack(owner, dest, weight) in the Container section to hold the owner input/parameter and will be called in the Shipyard version of add_pack(owner,dest, weight)



Edit 2: SO I was able to fix the initial problem but now I have another problem/question. Yes, the school required us to use LinkedLists else we didn't get marks for it. Now my new problem is I don't know if I should update the weight of the container class in the Shipyard method add_pack or if I should update it when using add_pack in the Container class.



Edit 3.5: So I updated the code and now it clearly shows the two add_pack methods: One in the Shipyard class and one in the Container class. Sorry for bad edits and formatting. I'm just really frustrated at how I can't figure this thing out.



Edit 4: I've got both traversePrint on Container and Shipyard. Shipyard's traversePrint lists the Containers and Container's traversePrint lists the packages within the containers. I'm able to display the packages w/c leads me to believe they are being added correctly.



Edit 5: I've mostly figured out how to add containers, and packages into containers but some condition checking i.e. If weight of two things add over 2000 lbs else add into the same one <- this isn't working.










share|improve this question




















  • 1




    In your container creation method, can you just call the method again to create a second container if needed? If you're having trouble with this, can you show what is causing the issue or what part of the problem you're getting stuck on?
    – Unsolved Cypher
    Nov 22 at 6:57






  • 1




    A couple of questions: what happens if the container is still not full but by adding a new package the total weight is more than 2000 lbs? And also: where is the owner information stored?
    – toti08
    Nov 22 at 13:28










  • @toti08 it should create a new container under the same destination name. The owner information will be stored in the Packages class w/c I don't need here. I only wanted to know how to implement adding containers. Conditions are: a. If containers will go over 2000 then the package is moved to a new container with the same destination name. b. If a package can fit into an existing container then it will be added inside without creating a newer one.
    – bluekozo
    Nov 22 at 14:17












  • @UnsolvedCypher My problem with this is I can't seem to alter the code and make it so that say adding a package to the same container also adds the weight. i.e. like in the result section w/c shows Alabama 700 and Alabama 900 it should've resulted in Alabama 1600. Tokyo 120 and Tokyo 1900 should've triggered a condition in which it would have only created the second Tokyo container because adding their weight will go over 2000 lbs which is what is required for this program.
    – bluekozo
    Nov 22 at 14:19










  • Is there any reason for using a linked list here? Why not stick with python's built in list type?
    – xtofl
    Nov 22 at 14:39














2












2








2







Programming is not my strongest suit and though the due date for this assignment was not met, I want to at least learn how this works before retaking the course for next year.



The whole point of the program is to be a shipyard management system that handles new packages that are arranged in containers which cannot go over 2000 lbs else it creates a new container for the same destination.



EDIT 3: This is the updated code. Sorry for not clarifying!! :<



class Shipyard:
def __init__(self):
self._cont = self.Container(None, None, None, None)
self._size = 0

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# adds package when container check is done

# If shipyard is empty or container name is alphabetically lower than
# the first container by destination name.

if self.isEmpty() or dest < self._cont._first._dest:
self._size += 1
self._cont._first = self.Container(owner, dest,
weight, self._cont._first)
self._cont._first.add_pack(owner, dest, weight)

return

# Else will go through the contents of the linkedlist to find the spot
# it belongs to.

cur_Cont = self._cont._first


while(cur_Cont._next != None and dest > cur_Cont._next._dest):
cur_Cont = cur_Cont._next

if dest == cur_Cont._dest:
if (cur_Cont._weight + weight) > 2000:
self._size += 1
cur_Cont._next = self.Container(owner, dest,
weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)
return

if (cur_Cont._weight + weight) <= 2000:
cur_Cont.add_pack(owner, dest, weight)
return

if (cur_Cont._next._weight + weight) <= 2000:
cur_Cont._next.add_pack(owner, dest, weight)
return


self._size += 1

# Creates a new container with the given sepcifications.
cur_Cont._next = self.Container(owner, dest, weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)

return

def traversePrint(self) : # Use for Printing Container
cur_Cont = self._cont._first # Modify to include details
c_weight = cur_Cont._weight

while cur_Cont != None: # of container or packages
c_weight = cur_Cont._weight
print(cur_Cont._dest, c_weight)
cur_Cont.traversePrint()
cur_Cont = cur_Cont._next


# -----------------------------------------------------------------------------
# Nested within the Shipyard

class Container:
def __init__(self, owner, dest, weight, next):
self._owner = owner
self._dest = dest
self._first = None
self._size = 0
self._weight = 0
self._next = next
self._pack = self.Packages(None, None, None, None)

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# Add pack for container class which adds a package.

if self.isEmpty() or weight < self._pack._first._weight:
self._size += 1
self._pack._first = self.Packages(owner, dest,
weight, self._pack._first)
self._weight += weight
return

cur_Pack = self._pack._first

while(cur_Pack._next != None and weight > cur_Pack._next._weight):
cur_Pack = cur_Pack._next

if weight == cur_Pack._weight:
self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight
return

self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight

return

def traversePrint(self):
cur_Pack = self._pack._first # Modify to include details

p_weight = cur_Pack._weight

while cur_Pack != None: # of container or packages
p_weight = cur_Pack._weight
if cur_Pack._dest == self._pack._dest:
print(cur_Pack._owner, cur_Pack._dest, p_weight, end = " ")
cur_Pack = cur_Pack._next
else:
print(cur_Pack._owner, cur_Pack._dest, p_weight)
cur_Pack = cur_Pack._next

# -----------------------------------------------------------------------------
# Nested within Container doesn't contain any methods

class Packages:

def __init__(self, owner, dest, weight, next):
self._first = None
self._size = 0
self._owner = owner
self._dest = dest
self._weight = weight
self._next = next

# -----------------------------------------------------------------------------

class Weight(Exception):
pass

# -----------------------------------------------------------------------------
shipyard = Shipyard()

len(shipyard)

shipyard.add_pack("Lori", "Bristol", 300)
shipyard.add_pack("Mallory", "Bristol", 200)
shipyard.add_pack("Chung", "Alabama", 700)
shipyard.add_pack("Roger", "Alabama", 900)
shipyard.add_pack("Ali", "Giza", 45)
shipyard.add_pack("Soumick", "New Delhi", 600)
shipyard.add_pack("Mizuki", "Tokyo", 1900)
shipyard.add_pack("Tadashi", "Tokyo", 120)
shipyard.add_pack("Kanna", "Tokyo", 220)


shipyard.traversePrint()


This is what appears when program is ran:



Alabama 1600
Chung Alabama 700
Roger Alabama 900
Bristol 500
Mallory Bristol 200
Lori Bristol 300
Giza 45
Ali Giza 45
New Delhi 600
Soumick New Delhi 600
Tokyo 220
Kanna Tokyo 220
Tokyo 120 # THIS AND Tokyo 220 must be added into the same container
Tadashi Tokyo 120 # I don't know what I didn't include because
Tokyo 1900 # It seems to me that Alabama 700 and Alabama 900 were
Mizuki Tokyo 1900 # Adding Correctly.


PROBLEM EDIT: Packages that should be adding aren't adding up together and instead creates an entirely new container. I don't know what part of the code is messing up now T_T.



If there is a question please ask me in the comments below.



Thank you for reading.



Edit: I plan to use an additional add_pack(owner, dest, weight) in the Container section to hold the owner input/parameter and will be called in the Shipyard version of add_pack(owner,dest, weight)



Edit 2: SO I was able to fix the initial problem but now I have another problem/question. Yes, the school required us to use LinkedLists else we didn't get marks for it. Now my new problem is I don't know if I should update the weight of the container class in the Shipyard method add_pack or if I should update it when using add_pack in the Container class.



Edit 3.5: So I updated the code and now it clearly shows the two add_pack methods: One in the Shipyard class and one in the Container class. Sorry for bad edits and formatting. I'm just really frustrated at how I can't figure this thing out.



Edit 4: I've got both traversePrint on Container and Shipyard. Shipyard's traversePrint lists the Containers and Container's traversePrint lists the packages within the containers. I'm able to display the packages w/c leads me to believe they are being added correctly.



Edit 5: I've mostly figured out how to add containers, and packages into containers but some condition checking i.e. If weight of two things add over 2000 lbs else add into the same one <- this isn't working.










share|improve this question















Programming is not my strongest suit and though the due date for this assignment was not met, I want to at least learn how this works before retaking the course for next year.



The whole point of the program is to be a shipyard management system that handles new packages that are arranged in containers which cannot go over 2000 lbs else it creates a new container for the same destination.



EDIT 3: This is the updated code. Sorry for not clarifying!! :<



class Shipyard:
def __init__(self):
self._cont = self.Container(None, None, None, None)
self._size = 0

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# adds package when container check is done

# If shipyard is empty or container name is alphabetically lower than
# the first container by destination name.

if self.isEmpty() or dest < self._cont._first._dest:
self._size += 1
self._cont._first = self.Container(owner, dest,
weight, self._cont._first)
self._cont._first.add_pack(owner, dest, weight)

return

# Else will go through the contents of the linkedlist to find the spot
# it belongs to.

cur_Cont = self._cont._first


while(cur_Cont._next != None and dest > cur_Cont._next._dest):
cur_Cont = cur_Cont._next

if dest == cur_Cont._dest:
if (cur_Cont._weight + weight) > 2000:
self._size += 1
cur_Cont._next = self.Container(owner, dest,
weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)
return

if (cur_Cont._weight + weight) <= 2000:
cur_Cont.add_pack(owner, dest, weight)
return

if (cur_Cont._next._weight + weight) <= 2000:
cur_Cont._next.add_pack(owner, dest, weight)
return


self._size += 1

# Creates a new container with the given sepcifications.
cur_Cont._next = self.Container(owner, dest, weight, cur_Cont._next)
cur_Cont._next.add_pack(owner, dest, weight)

return

def traversePrint(self) : # Use for Printing Container
cur_Cont = self._cont._first # Modify to include details
c_weight = cur_Cont._weight

while cur_Cont != None: # of container or packages
c_weight = cur_Cont._weight
print(cur_Cont._dest, c_weight)
cur_Cont.traversePrint()
cur_Cont = cur_Cont._next


# -----------------------------------------------------------------------------
# Nested within the Shipyard

class Container:
def __init__(self, owner, dest, weight, next):
self._owner = owner
self._dest = dest
self._first = None
self._size = 0
self._weight = 0
self._next = next
self._pack = self.Packages(None, None, None, None)

def __len__(self):
return self._size

def isEmpty(self):
return self._size == 0

def add_pack(self, owner, dest, weight):
# Add pack for container class which adds a package.

if self.isEmpty() or weight < self._pack._first._weight:
self._size += 1
self._pack._first = self.Packages(owner, dest,
weight, self._pack._first)
self._weight += weight
return

cur_Pack = self._pack._first

while(cur_Pack._next != None and weight > cur_Pack._next._weight):
cur_Pack = cur_Pack._next

if weight == cur_Pack._weight:
self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight
return

self._size += 1
cur_Pack._next = self.Packages(owner, dest,
weight, cur_Pack._next)
self._weight += weight

return

def traversePrint(self):
cur_Pack = self._pack._first # Modify to include details

p_weight = cur_Pack._weight

while cur_Pack != None: # of container or packages
p_weight = cur_Pack._weight
if cur_Pack._dest == self._pack._dest:
print(cur_Pack._owner, cur_Pack._dest, p_weight, end = " ")
cur_Pack = cur_Pack._next
else:
print(cur_Pack._owner, cur_Pack._dest, p_weight)
cur_Pack = cur_Pack._next

# -----------------------------------------------------------------------------
# Nested within Container doesn't contain any methods

class Packages:

def __init__(self, owner, dest, weight, next):
self._first = None
self._size = 0
self._owner = owner
self._dest = dest
self._weight = weight
self._next = next

# -----------------------------------------------------------------------------

class Weight(Exception):
pass

# -----------------------------------------------------------------------------
shipyard = Shipyard()

len(shipyard)

shipyard.add_pack("Lori", "Bristol", 300)
shipyard.add_pack("Mallory", "Bristol", 200)
shipyard.add_pack("Chung", "Alabama", 700)
shipyard.add_pack("Roger", "Alabama", 900)
shipyard.add_pack("Ali", "Giza", 45)
shipyard.add_pack("Soumick", "New Delhi", 600)
shipyard.add_pack("Mizuki", "Tokyo", 1900)
shipyard.add_pack("Tadashi", "Tokyo", 120)
shipyard.add_pack("Kanna", "Tokyo", 220)


shipyard.traversePrint()


This is what appears when program is ran:



Alabama 1600
Chung Alabama 700
Roger Alabama 900
Bristol 500
Mallory Bristol 200
Lori Bristol 300
Giza 45
Ali Giza 45
New Delhi 600
Soumick New Delhi 600
Tokyo 220
Kanna Tokyo 220
Tokyo 120 # THIS AND Tokyo 220 must be added into the same container
Tadashi Tokyo 120 # I don't know what I didn't include because
Tokyo 1900 # It seems to me that Alabama 700 and Alabama 900 were
Mizuki Tokyo 1900 # Adding Correctly.


PROBLEM EDIT: Packages that should be adding aren't adding up together and instead creates an entirely new container. I don't know what part of the code is messing up now T_T.



If there is a question please ask me in the comments below.



Thank you for reading.



Edit: I plan to use an additional add_pack(owner, dest, weight) in the Container section to hold the owner input/parameter and will be called in the Shipyard version of add_pack(owner,dest, weight)



Edit 2: SO I was able to fix the initial problem but now I have another problem/question. Yes, the school required us to use LinkedLists else we didn't get marks for it. Now my new problem is I don't know if I should update the weight of the container class in the Shipyard method add_pack or if I should update it when using add_pack in the Container class.



Edit 3.5: So I updated the code and now it clearly shows the two add_pack methods: One in the Shipyard class and one in the Container class. Sorry for bad edits and formatting. I'm just really frustrated at how I can't figure this thing out.



Edit 4: I've got both traversePrint on Container and Shipyard. Shipyard's traversePrint lists the Containers and Container's traversePrint lists the packages within the containers. I'm able to display the packages w/c leads me to believe they are being added correctly.



Edit 5: I've mostly figured out how to add containers, and packages into containers but some condition checking i.e. If weight of two things add over 2000 lbs else add into the same one <- this isn't working.







python python-3.x linked-list singly-linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 18:46

























asked Nov 22 at 5:08









bluekozo

155




155








  • 1




    In your container creation method, can you just call the method again to create a second container if needed? If you're having trouble with this, can you show what is causing the issue or what part of the problem you're getting stuck on?
    – Unsolved Cypher
    Nov 22 at 6:57






  • 1




    A couple of questions: what happens if the container is still not full but by adding a new package the total weight is more than 2000 lbs? And also: where is the owner information stored?
    – toti08
    Nov 22 at 13:28










  • @toti08 it should create a new container under the same destination name. The owner information will be stored in the Packages class w/c I don't need here. I only wanted to know how to implement adding containers. Conditions are: a. If containers will go over 2000 then the package is moved to a new container with the same destination name. b. If a package can fit into an existing container then it will be added inside without creating a newer one.
    – bluekozo
    Nov 22 at 14:17












  • @UnsolvedCypher My problem with this is I can't seem to alter the code and make it so that say adding a package to the same container also adds the weight. i.e. like in the result section w/c shows Alabama 700 and Alabama 900 it should've resulted in Alabama 1600. Tokyo 120 and Tokyo 1900 should've triggered a condition in which it would have only created the second Tokyo container because adding their weight will go over 2000 lbs which is what is required for this program.
    – bluekozo
    Nov 22 at 14:19










  • Is there any reason for using a linked list here? Why not stick with python's built in list type?
    – xtofl
    Nov 22 at 14:39














  • 1




    In your container creation method, can you just call the method again to create a second container if needed? If you're having trouble with this, can you show what is causing the issue or what part of the problem you're getting stuck on?
    – Unsolved Cypher
    Nov 22 at 6:57






  • 1




    A couple of questions: what happens if the container is still not full but by adding a new package the total weight is more than 2000 lbs? And also: where is the owner information stored?
    – toti08
    Nov 22 at 13:28










  • @toti08 it should create a new container under the same destination name. The owner information will be stored in the Packages class w/c I don't need here. I only wanted to know how to implement adding containers. Conditions are: a. If containers will go over 2000 then the package is moved to a new container with the same destination name. b. If a package can fit into an existing container then it will be added inside without creating a newer one.
    – bluekozo
    Nov 22 at 14:17












  • @UnsolvedCypher My problem with this is I can't seem to alter the code and make it so that say adding a package to the same container also adds the weight. i.e. like in the result section w/c shows Alabama 700 and Alabama 900 it should've resulted in Alabama 1600. Tokyo 120 and Tokyo 1900 should've triggered a condition in which it would have only created the second Tokyo container because adding their weight will go over 2000 lbs which is what is required for this program.
    – bluekozo
    Nov 22 at 14:19










  • Is there any reason for using a linked list here? Why not stick with python's built in list type?
    – xtofl
    Nov 22 at 14:39








1




1




In your container creation method, can you just call the method again to create a second container if needed? If you're having trouble with this, can you show what is causing the issue or what part of the problem you're getting stuck on?
– Unsolved Cypher
Nov 22 at 6:57




In your container creation method, can you just call the method again to create a second container if needed? If you're having trouble with this, can you show what is causing the issue or what part of the problem you're getting stuck on?
– Unsolved Cypher
Nov 22 at 6:57




1




1




A couple of questions: what happens if the container is still not full but by adding a new package the total weight is more than 2000 lbs? And also: where is the owner information stored?
– toti08
Nov 22 at 13:28




A couple of questions: what happens if the container is still not full but by adding a new package the total weight is more than 2000 lbs? And also: where is the owner information stored?
– toti08
Nov 22 at 13:28












@toti08 it should create a new container under the same destination name. The owner information will be stored in the Packages class w/c I don't need here. I only wanted to know how to implement adding containers. Conditions are: a. If containers will go over 2000 then the package is moved to a new container with the same destination name. b. If a package can fit into an existing container then it will be added inside without creating a newer one.
– bluekozo
Nov 22 at 14:17






@toti08 it should create a new container under the same destination name. The owner information will be stored in the Packages class w/c I don't need here. I only wanted to know how to implement adding containers. Conditions are: a. If containers will go over 2000 then the package is moved to a new container with the same destination name. b. If a package can fit into an existing container then it will be added inside without creating a newer one.
– bluekozo
Nov 22 at 14:17














@UnsolvedCypher My problem with this is I can't seem to alter the code and make it so that say adding a package to the same container also adds the weight. i.e. like in the result section w/c shows Alabama 700 and Alabama 900 it should've resulted in Alabama 1600. Tokyo 120 and Tokyo 1900 should've triggered a condition in which it would have only created the second Tokyo container because adding their weight will go over 2000 lbs which is what is required for this program.
– bluekozo
Nov 22 at 14:19




@UnsolvedCypher My problem with this is I can't seem to alter the code and make it so that say adding a package to the same container also adds the weight. i.e. like in the result section w/c shows Alabama 700 and Alabama 900 it should've resulted in Alabama 1600. Tokyo 120 and Tokyo 1900 should've triggered a condition in which it would have only created the second Tokyo container because adding their weight will go over 2000 lbs which is what is required for this program.
– bluekozo
Nov 22 at 14:19












Is there any reason for using a linked list here? Why not stick with python's built in list type?
– xtofl
Nov 22 at 14:39




Is there any reason for using a linked list here? Why not stick with python's built in list type?
– xtofl
Nov 22 at 14:39












1 Answer
1






active

oldest

votes


















1














Split the problem in happable chunks:




  1. record the required shippables (as you currently do)

  2. determine how many containers you need for each destination (probably math.ceil(weight(dest) / 2000.0) or something like that)


So your usage will become



shipyard.add_pack("X", "dest", 500)
shipyard.add_pack("X", "dest", 1500)
shipyard.add_pack("X", "dest", 2200)
...

containers = {
dest: shipyard.containers_for(dest)
for dest in shipyard.destinations()
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424222%2fadding-into-a-linked-list-of-containers%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Split the problem in happable chunks:




    1. record the required shippables (as you currently do)

    2. determine how many containers you need for each destination (probably math.ceil(weight(dest) / 2000.0) or something like that)


    So your usage will become



    shipyard.add_pack("X", "dest", 500)
    shipyard.add_pack("X", "dest", 1500)
    shipyard.add_pack("X", "dest", 2200)
    ...

    containers = {
    dest: shipyard.containers_for(dest)
    for dest in shipyard.destinations()
    }





    share|improve this answer




























      1














      Split the problem in happable chunks:




      1. record the required shippables (as you currently do)

      2. determine how many containers you need for each destination (probably math.ceil(weight(dest) / 2000.0) or something like that)


      So your usage will become



      shipyard.add_pack("X", "dest", 500)
      shipyard.add_pack("X", "dest", 1500)
      shipyard.add_pack("X", "dest", 2200)
      ...

      containers = {
      dest: shipyard.containers_for(dest)
      for dest in shipyard.destinations()
      }





      share|improve this answer


























        1












        1








        1






        Split the problem in happable chunks:




        1. record the required shippables (as you currently do)

        2. determine how many containers you need for each destination (probably math.ceil(weight(dest) / 2000.0) or something like that)


        So your usage will become



        shipyard.add_pack("X", "dest", 500)
        shipyard.add_pack("X", "dest", 1500)
        shipyard.add_pack("X", "dest", 2200)
        ...

        containers = {
        dest: shipyard.containers_for(dest)
        for dest in shipyard.destinations()
        }





        share|improve this answer














        Split the problem in happable chunks:




        1. record the required shippables (as you currently do)

        2. determine how many containers you need for each destination (probably math.ceil(weight(dest) / 2000.0) or something like that)


        So your usage will become



        shipyard.add_pack("X", "dest", 500)
        shipyard.add_pack("X", "dest", 1500)
        shipyard.add_pack("X", "dest", 2200)
        ...

        containers = {
        dest: shipyard.containers_for(dest)
        for dest in shipyard.destinations()
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 14:36

























        answered Nov 22 at 14:27









        xtofl

        31.6k680160




        31.6k680160






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424222%2fadding-into-a-linked-list-of-containers%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks