def __init__(self, color='silver', name='jetpack', max_size=2, fuel=10): """constructor for a jetback object Inputs: color (str, opt) default is silver name(str, opt) default is jetpack max_size (int, opt) deafult is 2 feul (int, opt) default is 10 returns a jetpack with no contents""" Backpack.__init__(self, color, name, max_size) """ this calls the backpack object which is used to create the Jetpack object""" self.fuel = fuel
def __init__(self, color='silver', name='jetpack', max_size=2, fuel=10): """ Constructor for jetpacks. Holds 2 items, has fuel attribute. Inputs: color(str): color, defaults to silver name(str): name, defualts to jetback max_size(int): max number of elemetns, defaults to 2 Fuel(int): Defaults to 10. Amount of fuel left in jetpack Returns: an empty contented, 10 fuel filled silver jetpack """ Backpack.__init__(self, color, name, max_size) self.fuel = 10
def __init__(self, color='silver', name='jetpack', max_size=2, fuel = 10): """ Constructor for jetpacks. Holds 2 items, has fuel attribute. Inputs: color(str): color, defaults to silver name(str): name, defualts to jetback max_size(int): max number of elemetns, defaults to 2 Fuel(int): Defaults to 10. Amount of fuel left in jetpack Returns: an empty contented, 10 fuel filled silver jetpack """ Backpack.__init__(self, color, name, max_size) self.fuel = 10
def __init__(self, color='brown', name='knapsack', max_size=3): """Constructor for a knapsack object. A knapsack only holds 3 item by default instead of 5. Use the Backpack constructor to initialize the name and max_size attributes. Inputs: color (str, opt): the color of the knapsack. Defaults to 'brown'. name (str, opt): the name of the knapsack. Defaults to 'knapsack'. max_size (int, opt): the maximum number of items that can be stored in the knapsack. Defaults to 3. Returns: A knapsack object with no contents. """ Backpack.__init__(self, color, name, max_size) self.closed = True
def __init__(self, name): self.name = name self.skill_tree = None self.skill_point = 0 self.stats = Stats(self) self.avatar = WarriorAvatar() self.backpack = Backpack(self) self.equipments = Equipments(self)
def mate_packs(self, pack1, pack2): if len(pack1.boxes) == len(pack2.boxes): for box in pack1.boxes: if not box in pack2.boxes: break else: return pack1 return Backpack(self.gen_rand_box_combo(pack1.boxes + pack2.boxes))
def gen_rand_box_combo(self, pos_boxes): used_boxes = [] pos_indices = [i for i in range(len(pos_boxes))] random.shuffle(pos_indices) temp = Backpack() temp.reset_boxes() count = 0 index = pos_indices[count] while temp.add_box(pos_boxes[index]): used_boxes.append(pos_boxes[index]) while pos_boxes[index] in used_boxes: count += 1 if count >= len(pos_boxes): return temp.boxes index = pos_indices[count] assert count < len(pos_indices) return temp.boxes
class BackpackTests(unittest.TestCase): def setUp(self): self.backpack = Backpack(7) self.backpack.clear() def test_empty(self): self.assertEqual(self.backpack.current_weight, 0) def test_insert(self): self.backpack.insert(Item('1', 2, 4)) self.assertEqual(self.backpack.full_price, 4) self.assertEqual(self.backpack.current_weight, 2) self.assertEqual(self.backpack.items[0].name, '1') def test_clear(self): self.backpack.insert(Item('1', 2, 4)) self.backpack.clear() self.assertEqual(self.backpack.full_price, 0) self.assertEqual(self.backpack.current_weight, 0) self.assertEqual(self.backpack.items, []) def test_greedy_alg(self): list_of_items = [ Item('1', 2, 1), Item('2', 2, 3), Item('3', 3, 2), Item('4', 1, 1) ] self.assertEqual(DZ.greedy(list_of_items, self.backpack), (7, 10)) def test_standart_alg(self): list_of_items = [ Item('1', 2, 1), Item('T.E.A.', 2, 3), Item('3', 3, 2), Item('4', 1, 1) ] W = [list_of_items[i].weight for i in range(len(list_of_items))] P = [list_of_items[i].price for i in range(len(list_of_items))] self.assertEqual(DZ.standart(W, P, self.backpack, list_of_items), (7, 10))
def time_us(ns): """Prints time table for given functions and inputs. functions - dictionary of {func name: func(input)} - functions to time, ns - list of n for which generate input, generator - func(n) - input generation function, repeats - number of times to call functions for each given input.""" print(make_header(['standart', 'greedy_algorithm'])) for n in ns: list_of_items = [ Item("item " + str(i), random.randint(1, 100), random.randint(1, 100)) for i in range(10**n) ] W = [list_of_items[i].weight for i in range(10**n)] P = [list_of_items[i].price for i in range(10**n)] bp = Backpack(10000) times = [] timer = timeit.Timer(lambda: DZ.standart(W, P, bp, list_of_items)) times.append(timer.timeit(1)) timer = timeit.Timer(lambda: DZ.greedy(list_of_items, bp)) times.append(timer.timeit(1)) print(make_line(n, times))
def take(self, item): """If the knapsack is untied, use the Backpack take() method.""" if self.closed: print "Knapsack is closed!" else: Backpack.take(self, item)
items = l items.sort(key=lambda x: x.price_cooficient) for i in items: while True: if not backpack.insert(i): break return backpack.current_weight, backpack.full_price if __name__ == "__main__": list_of_items = [ Item('1', 2, 1), Item('2', 2, 3), Item('3', 3, 2), Item('4', 1, 1) ] W = [list_of_items[i].weight for i in range(len(list_of_items))] P = [list_of_items[i].price for i in range(len(list_of_items))] #list_of_items = [Item("item " + str(i), random.random() * 100, random.random() * 1000, i) for i in range(1000)] static_list = list_of_items[:] bp = Backpack(7) t1 = time.time() print(standart(W, P, bp, list_of_items)) t2 = time.time() print("sta algorythm time: " + str(t2 - t1)) bp.clear() #greedy t1 = time.time() print(greedy(list_of_items, bp)) t2 = time.time() print("greedy algorythm time: " + str(t2 - t1))
def dump(self): """ This function deletes all the items from the jetpack and removes all feul""" Backpack.dump(self) self.fuel = 0
# name this file 'solutions.py' """Volume II Lab 2: Object Oriented Programming Drew Pearson Class 321 9/10/15 """ import math from Backpack import Backpack my_backpack = Backpack() your_backpack = Backpack() my_backpack.put('tape') my_backpack.put('scissors') your_backpack.put('tape') my_backpack = your_backpack # Problem 1: Modify the 'Backpack' class in 'Backpack.py'. # Study the 'Knapsack' class in 'Backpack.py'. You should be able to create a # Knapsack object after finishing problem 1. # Problem 2: Write a 'Jetpack' class that inherits from the 'Backpack' class. class Jetpack(Backpack): """A Jetpack object class. Inherits from the Backpack class. However, it's initial color is silver and its initial max size is 2. A jetpack class also has an additional attribute - feul (int) which if not given is preset to 10. """ def __init__(self, color='silver', name='jetpack', max_size=2, fuel=10):
# name this file 'solutions.py' """Volume II Lab 2: Object Oriented Programming Donald Rex McArthur Math 320 Sept. 10, 2015 """ from Backpack import Backpack import copy # Problem 1: Modify the 'Backpack' class in 'Backpack.py'. A = Backpack('red', 'Rex', 9) # Study the 'Knapsack' class in 'Backpack.py'. You should be able to create a # Knapsack object after finishing problem 1. # Problem 2: Write a 'Jetpack' class that inherits from the 'Backpack' class. class Jetpack(Backpack): """A Jetpack object class. Inherits from the Backpack class. Attributes: color(str): color, defaults to silver name(str): name, defualts to jetback max_size(int): max number of elemetns, defaults to 2 contents(list): Contents of jetback Fuel(int): Defaults to 10. Amount of fuel left in jetpack """ def __init__(self, color='silver', name='jetpack', max_size=2, fuel=10): """ Constructor for jetpacks. Holds 2 items, has fuel attribute.
# test.py # Execution directions for testing items and backpacks from Backpack import Backpack, Item item = Item("Apple", True) backpack = Backpack("Theo") backpack.addItem(item) backpack.addItem(item) print(item) print(backpack)
def setUp(self): self.backpack = Backpack(7) self.backpack.clear()
def __init__(self, pop_size=20, max_gen=20, percent_mutate=20): self.pop_size = pop_size self.max_gen = max_gen self.mutate_chance = percent_mutate for _ in range(self.pop_size): self.backpacks.append(Backpack(self.gen_rand_box_combo(pos_boxes)))
from Eraser import Eraser # Eraser class (child class inherit with "Item" parent class) from Notebook import Notebook # Notebook class (child class inherit with "Item" parent class) from Pen import Pen # Pen class (child class inherit with "Item" parent class) from Pencil import Pencil # Pencil class (child class inherit with "Item" parent class) from Ruler import Ruler # Ruler class (child class inherit with "Item" parent class) # credit print("#" * 34) print("Nama : Bintang Muhammad Agastya") print("NIM : 081911633039 ") print("Hari/Tgl: Kamis, 14 Januari 2021 ") print("#" * 34) # main program print("\n-> Create Object Backpack") item_list = Backpack(owner="Bintang", capacity=3) item_list.detail() print("\n-> Backpack.remove()") item_list.remove() print("\n-> Backpack.add() menambahkan Object Eraser") item_list.add(Eraser(owner="Bintang")) print("\n-> Backpack.add() menambahkan Object Pen") item_list.add(Pen(owner="Agastya")) print("\n-> Backpack.add() menambahkan Object Notebook") item_list.add(Notebook(owner="Bintang")) print("\n-> Backpack.add() menambahkan Object Ruler")