Created
August 8, 2022 17:04
-
-
Save ZakriaJanjua/95844e774d54cfb56796defe016ff753 to your computer and use it in GitHub Desktop.
Hackerrank Python(Basic) certification question. Needed to create a shopping cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item: | |
def __init__(self, name, price): | |
self.name = name | |
self.price = price | |
item = Item('Bike', 1000) | |
class ShoppingCart: | |
# Implement the ShoppingCart here | |
def __init__(self): | |
self.cart = {} | |
pass | |
def add(self, item): | |
if (item.name in self.cart): | |
self.cart[item.name] = [self.cart[item.name][0] + 1, self.cart[item.name][1] + item.price] | |
else: | |
self.cart[item.name] = [1, item.price] | |
def total(self): | |
count = 0 | |
for i in self.cart: | |
# count += i[1] | |
print(self.cart[i][1]) | |
return count | |
def __len__(self): | |
length = 0 | |
for i in self.cart: | |
length += self.cart[i][0] | |
return length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment