Skip to content

Instantly share code, notes, and snippets.

@ZakriaJanjua
Created August 8, 2022 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZakriaJanjua/95844e774d54cfb56796defe016ff753 to your computer and use it in GitHub Desktop.
Save ZakriaJanjua/95844e774d54cfb56796defe016ff753 to your computer and use it in GitHub Desktop.
Hackerrank Python(Basic) certification question. Needed to create a shopping cart
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