From 57e48b9c3016d66e8172084a9b8e51071bb43440 Mon Sep 17 00:00:00 2001 From: Serhii Horodilov Date: Thu, 24 Nov 2022 14:18:38 +0200 Subject: [PATCH 01/10] implemented conv store task --- 01/task_01.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 01/task_01.py diff --git a/01/task_01.py b/01/task_01.py new file mode 100644 index 0000000..f0da4a4 --- /dev/null +++ b/01/task_01.py @@ -0,0 +1,40 @@ +from collections import defaultdict + +class Product: + """Describing product""" + + def __init__(self, name: str, price: float): + self.name = name + self.price = price + + def get_total(self, count: float): + """Calculating total price for the product""" + return self.price * count + + +class ShoppingCart: + """Describing shopping cart""" + + def __init__(self): + self.products = defaultdict(int) + self.total = 0 + + def add(self, product: Product, count: int = 1): + """Adding products and calculating total sum""" + + self.products[product] += count + self.total += product.price * count + + +prod1 = Product('lemon', 50) +print(prod1.get_total(10)) +prod2 = Product('strawberry', 150) +print(prod2.get_total(20)) +prod3 = Product('cucumber', 100) +print(prod2.get_total(20)) +cart1 = ShoppingCart() +cart1.add(prod1, 5) +cart1.add(prod2, 10) +cart1.add(prod3, 20) +cart1.add(prod1, 5) +print(cart1.total) From 0e6749d0f293dc1a583a40448fbfed4b187fdff4 Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Fri, 25 Nov 2022 09:07:13 +0200 Subject: [PATCH 02/10] Code refactoring --- 01/task_01.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index f0da4a4..4029210 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -1,5 +1,3 @@ -from collections import defaultdict - class Product: """Describing product""" @@ -9,32 +7,35 @@ def __init__(self, name: str, price: float): def get_total(self, count: float): """Calculating total price for the product""" - return self.price * count + return round(self.price * count, 2) class ShoppingCart: - """Describing shopping cart""" + """Describing the shopping cart""" def __init__(self): - self.products = defaultdict(int) - self.total = 0 + self.total_cart = [] + + def add(self, product: Product, count: float = 1): + """Adding products to the cart""" - def add(self, product: Product, count: int = 1): - """Adding products and calculating total sum""" + total_product_price = product.get_total(count) + self.total_cart.append(total_product_price) - self.products[product] += count - self.total += product.price * count + def total_cart_sum(self): + """Calculating the total cart sum""" + return sum(self.total_cart) -prod1 = Product('lemon', 50) -print(prod1.get_total(10)) + +prod1 = Product('lemon', 15.10) +print(prod1.get_total(0.7)) prod2 = Product('strawberry', 150) -print(prod2.get_total(20)) prod3 = Product('cucumber', 100) -print(prod2.get_total(20)) cart1 = ShoppingCart() -cart1.add(prod1, 5) -cart1.add(prod2, 10) +cart1.add(prod1, 0.7) +cart1.add(prod2, 20) cart1.add(prod3, 20) cart1.add(prod1, 5) -print(cart1.total) +print(cart1.total_cart_sum()) +print(cart1.total_cart) From 25699b0d92d045d2ad54934019a90b595b696fc5 Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Fri, 25 Nov 2022 17:25:12 +0200 Subject: [PATCH 03/10] Add total_coun into task_01.py --- 01/task_01.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index 4029210..7490daa 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -15,12 +15,14 @@ class ShoppingCart: def __init__(self): self.total_cart = [] + self.total_count = 0 def add(self, product: Product, count: float = 1): - """Adding products to the cart""" + """Adding products to the cart, count products""" total_product_price = product.get_total(count) self.total_cart.append(total_product_price) + self.total_count += count def total_cart_sum(self): """Calculating the total cart sum""" @@ -38,4 +40,4 @@ def total_cart_sum(self): cart1.add(prod3, 20) cart1.add(prod1, 5) print(cart1.total_cart_sum()) -print(cart1.total_cart) +print(cart1.total_count) From b5f411549ae16f89701ae3780e98797a24979224 Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Fri, 25 Nov 2022 20:05:06 +0200 Subject: [PATCH 04/10] Add all data for check creating --- 01/task_01.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index 7490daa..e4c3739 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -14,20 +14,26 @@ class ShoppingCart: """Describing the shopping cart""" def __init__(self): - self.total_cart = [] - self.total_count = 0 + self.products_list = {} def add(self, product: Product, count: float = 1): - """Adding products to the cart, count products""" + """Adding products to the cart""" - total_product_price = product.get_total(count) - self.total_cart.append(total_product_price) - self.total_count += count + if product.name in self.products_list: + quantity = self.products_list.get(product.name, 0)["quantity"] + count + else: + quantity = count + product_sum = product.get_total(quantity) + price = product.price + self.products_list[product.name] = {"quantity": quantity, "price": price, "product_sum": product_sum} def total_cart_sum(self): """Calculating the total cart sum""" - return sum(self.total_cart) + product_list_sum = 0 + for product in self.products_list: + product_list_sum += self.products_list[product]['product_sum'] + return product_list_sum prod1 = Product('lemon', 15.10) @@ -39,5 +45,5 @@ def total_cart_sum(self): cart1.add(prod2, 20) cart1.add(prod3, 20) cart1.add(prod1, 5) +print(cart1.products_list) print(cart1.total_cart_sum()) -print(cart1.total_count) From 94ce2d00b174320f96a49d33f31ba0aca0363bb3 Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Fri, 25 Nov 2022 21:13:23 +0200 Subject: [PATCH 05/10] add product into dict into task_01.py --- 01/task_01.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index e4c3739..fd3b41e 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -20,19 +20,22 @@ def add(self, product: Product, count: float = 1): """Adding products to the cart""" if product.name in self.products_list: - quantity = self.products_list.get(product.name, 0)["quantity"] + count + quantity = \ + self.products_list.get( + product.name, 0 + )["quantity"] + count else: quantity = count - product_sum = product.get_total(quantity) - price = product.price - self.products_list[product.name] = {"quantity": quantity, "price": price, "product_sum": product_sum} + self.products_list[product.name] = {"product": product, "quantity": quantity} def total_cart_sum(self): """Calculating the total cart sum""" product_list_sum = 0 - for product in self.products_list: - product_list_sum += self.products_list[product]['product_sum'] + for item in self.products_list: + p_price = self.products_list[item]['product'].price + quantity = self.products_list[item]["quantity"] + product_list_sum += p_price * quantity return product_list_sum From f353542feeb3e5d7e239480790b8cacf661e04f9 Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Sat, 26 Nov 2022 10:04:38 +0200 Subject: [PATCH 06/10] Correct total_cart_sum in task_01.py --- 01/task_01.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index fd3b41e..560743d 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -33,19 +33,19 @@ def total_cart_sum(self): product_list_sum = 0 for item in self.products_list: - p_price = self.products_list[item]['product'].price + product = self.products_list[item]['product'] quantity = self.products_list[item]["quantity"] - product_list_sum += p_price * quantity + product_list_sum += product.get_total(quantity) return product_list_sum -prod1 = Product('lemon', 15.10) +prod1 = Product('lemon', 10.59) print(prod1.get_total(0.7)) -prod2 = Product('strawberry', 150) +prod2 = Product('strawberry', 36.55) prod3 = Product('cucumber', 100) cart1 = ShoppingCart() cart1.add(prod1, 0.7) -cart1.add(prod2, 20) +cart1.add(prod2, 4) cart1.add(prod3, 20) cart1.add(prod1, 5) print(cart1.products_list) From d10695bf8d6cccf5fe8e91b06b2fdc6765919d7d Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Sat, 26 Nov 2022 16:26:59 +0200 Subject: [PATCH 07/10] Change dict into lists --- 01/task_01.py | 61 ++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index 560743d..d5940e4 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -14,39 +14,46 @@ class ShoppingCart: """Describing the shopping cart""" def __init__(self): - self.products_list = {} + self.products_list = [] + self.products_count = [] - def add(self, product: Product, count: float = 1): + def add_product(self, product: Product, count: float = 1): """Adding products to the cart""" - if product.name in self.products_list: - quantity = \ - self.products_list.get( - product.name, 0 - )["quantity"] + count + if product in self.products_list: + self.products_count[self.products_list.index(product)] += count else: - quantity = count - self.products_list[product.name] = {"product": product, "quantity": quantity} + self.products_list.append(product) + self.products_count.append(count) def total_cart_sum(self): """Calculating the total cart sum""" product_list_sum = 0 - for item in self.products_list: - product = self.products_list[item]['product'] - quantity = self.products_list[item]["quantity"] - product_list_sum += product.get_total(quantity) - return product_list_sum - - -prod1 = Product('lemon', 10.59) -print(prod1.get_total(0.7)) -prod2 = Product('strawberry', 36.55) -prod3 = Product('cucumber', 100) -cart1 = ShoppingCart() -cart1.add(prod1, 0.7) -cart1.add(prod2, 4) -cart1.add(prod3, 20) -cart1.add(prod1, 5) -print(cart1.products_list) -print(cart1.total_cart_sum()) + for i in range(len(self.products_list)): + product = self.products_list[i] + count = self.products_count[i] + product_list_sum += product.get_total(count) + return round(product_list_sum, 2) + + +# prod1 = Product('lemon', 10.59) +# print(prod1.get_total(0.7)) +# prod2 = Product('strawberry', 36.55) +# prod3 = Product('cucumber', 100) +# cart1 = ShoppingCart() +# cart1.add(prod1, 0.7) +# cart1.add(prod2, 4) +# cart1.add(prod3, 20) +# cart1.add(prod1, 5) +# print(cart1.products_list) +# print(cart1.total_cart_sum()) + +product_1 = Product("foo", 10.59) +product_2 = Product("bar", 36.55) +print(product_1.get_total(0.7)) +print(product_2.get_total(4)) +cart = ShoppingCart() +cart.add_product(product_1, 0.7) +cart.add_product(product_2, 4) +print(cart.total_cart_sum()) From bc72989257fb0b80753e2400de022bc77f4ec527 Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Sun, 27 Nov 2022 21:36:29 +0200 Subject: [PATCH 08/10] Add changes into task_01.py --- 01/task_01.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index d5940e4..7c36775 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -21,7 +21,8 @@ def add_product(self, product: Product, count: float = 1): """Adding products to the cart""" if product in self.products_list: - self.products_count[self.products_list.index(product)] += count + self.products_count[ + self.products_list.index(product)] += count else: self.products_list.append(product) self.products_count.append(count) @@ -30,9 +31,7 @@ def total_cart_sum(self): """Calculating the total cart sum""" product_list_sum = 0 - for i in range(len(self.products_list)): - product = self.products_list[i] - count = self.products_count[i] + for product, count in zip(self.products_list, self.products_count): product_list_sum += product.get_total(count) return round(product_list_sum, 2) @@ -42,10 +41,10 @@ def total_cart_sum(self): # prod2 = Product('strawberry', 36.55) # prod3 = Product('cucumber', 100) # cart1 = ShoppingCart() -# cart1.add(prod1, 0.7) -# cart1.add(prod2, 4) -# cart1.add(prod3, 20) -# cart1.add(prod1, 5) +# cart1.add_product(prod1, 0.7) +# cart1.add_product(prod2, 4) +# cart1.add_product(prod3, 20) +# cart1.add_product(prod1, 5) # print(cart1.products_list) # print(cart1.total_cart_sum()) From 7dcdd9b45b61d2ace60c237c3e61f8aabdb2e766 Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Sun, 4 Dec 2022 22:14:22 +0200 Subject: [PATCH 09/10] refresh homework_1 by adding dundle methods --- 01/task_01.py | 90 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index 7c36775..3447814 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -5,6 +5,18 @@ def __init__(self, name: str, price: float): self.name = name self.price = price + def __repr__(self): + return f'Product {self.name} - price {self.price}' + + def __float__(self): + return self.price + + def __str__(self): + return self.name + + def __eq__(self, other): + return self.name == other.name and self.price == other.price + def get_total(self, count: float): """Calculating total price for the product""" return round(self.price * count, 2) @@ -17,6 +29,31 @@ def __init__(self): self.products_list = [] self.products_count = [] + def __repr__(self): + cheque = '' + for i in range(len(self.products_list)): + cheque += f"" \ + f"product - {self.products_list[i].name}, " \ + f"price - {self.products_list[i].price}, " \ + f"count - {self.products_count[i]}\n" + cheque += f"total sum - {self.total_cart_sum()}" + return cheque + # return f'Cart - {self.products_list}, count - {self.products_count} sum - {self.total_cart_sum()}' + + def __float__(self): + return self.total_cart_sum() + + def __add__(self, other): + if isinstance(other, Product): + self.add_product(other, 1) + elif isinstance(other, ShoppingCart): + for product in other.products_list: + count = other.products_count[ + other.products_list.index(product) + ] + self.add_product(product, count) + return self + def add_product(self, product: Product, count: float = 1): """Adding products to the cart""" @@ -36,23 +73,36 @@ def total_cart_sum(self): return round(product_list_sum, 2) -# prod1 = Product('lemon', 10.59) -# print(prod1.get_total(0.7)) -# prod2 = Product('strawberry', 36.55) -# prod3 = Product('cucumber', 100) -# cart1 = ShoppingCart() -# cart1.add_product(prod1, 0.7) -# cart1.add_product(prod2, 4) -# cart1.add_product(prod3, 20) -# cart1.add_product(prod1, 5) -# print(cart1.products_list) -# print(cart1.total_cart_sum()) - -product_1 = Product("foo", 10.59) -product_2 = Product("bar", 36.55) -print(product_1.get_total(0.7)) -print(product_2.get_total(4)) -cart = ShoppingCart() -cart.add_product(product_1, 0.7) -cart.add_product(product_2, 4) -print(cart.total_cart_sum()) +prod1 = Product('lemon', 10.59) +print(prod1.get_total(0.7)) +prod2 = Product('strawberry', 36.55) +prod3 = Product('cucumber', 100) +prod4 = Product('cucumber', 100) +cart1 = ShoppingCart() +cart1.add_product(prod1, 0.7) +cart1.add_product(prod2, 4) +cart1.add_product(prod3, 20) +cart1.add_product(prod1, 5) +print(cart1.products_list) +print(cart1.total_cart_sum()) +print(prod1) +print(cart1) +print(float(prod1)) +print(str(prod1)) +print(float(cart1)) +cart2 = ShoppingCart() +cart2.add_product(prod1, 2) +cart2.add_product(prod2, 3) +print(cart2) +print(prod4 == prod3) +print(cart1 + prod1) +print(cart1 + cart2) + +# product_1 = Product("foo", 10.59) +# product_2 = Product("bar", 36.55) +# print(product_1.get_total(0.7)) +# print(product_2.get_total(4)) +# cart = ShoppingCart() +# cart.add_product(product_1, 0.7) +# cart.add_product(product_2, 4) +# print(cart.total_cart_sum()) From 0c4ea2a5d78adbf88c889870e9e762e211611d4f Mon Sep 17 00:00:00 2001 From: Zapadynsky Date: Mon, 5 Dec 2022 11:44:04 +0200 Subject: [PATCH 10/10] correct task_01.py --- 01/task_01.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/01/task_01.py b/01/task_01.py index 3447814..d7f48e3 100644 --- a/01/task_01.py +++ b/01/task_01.py @@ -1,3 +1,6 @@ +from copy import deepcopy + + class Product: """Describing product""" @@ -15,7 +18,9 @@ def __str__(self): return self.name def __eq__(self, other): - return self.name == other.name and self.price == other.price + if isinstance(other, Product): + return self.name == other.name and self.price == other.price + return False def get_total(self, count: float): """Calculating total price for the product""" @@ -38,28 +43,25 @@ def __repr__(self): f"count - {self.products_count[i]}\n" cheque += f"total sum - {self.total_cart_sum()}" return cheque - # return f'Cart - {self.products_list}, count - {self.products_count} sum - {self.total_cart_sum()}' def __float__(self): return self.total_cart_sum() def __add__(self, other): + added_cart = deepcopy(self) if isinstance(other, Product): - self.add_product(other, 1) + added_cart.add_product(other, 1) elif isinstance(other, ShoppingCart): - for product in other.products_list: - count = other.products_count[ - other.products_list.index(product) - ] - self.add_product(product, count) - return self + for product, count in zip(other.products_list, other.products_count): + added_cart.add_product(product, count) + return added_cart def add_product(self, product: Product, count: float = 1): """Adding products to the cart""" if product in self.products_list: - self.products_count[ - self.products_list.index(product)] += count + common_index = self.products_list.index(product) + self.products_count[common_index] += count else: self.products_list.append(product) self.products_count.append(count) @@ -86,17 +88,26 @@ def total_cart_sum(self): print(cart1.products_list) print(cart1.total_cart_sum()) print(prod1) -print(cart1) +print("___________") +print(f"{cart1=}") +print("___________") print(float(prod1)) print(str(prod1)) print(float(cart1)) cart2 = ShoppingCart() cart2.add_product(prod1, 2) cart2.add_product(prod2, 3) -print(cart2) print(prod4 == prod3) -print(cart1 + prod1) -print(cart1 + cart2) +print("___________") +print(f"{cart2=}") +cart3 = cart1 + prod1 +print("___________") +print(f"{cart3=}") +cart4 = cart1 + cart2 +print("___________") +print(f'{cart1=}') +print("___________") +print(f'{cart4=}') # product_1 = Product("foo", 10.59) # product_2 = Product("bar", 36.55)