Initial commit

This commit is contained in:
Arjun Patel
2019-02-20 15:09:59 -08:00
commit 6577e0cbea
59 changed files with 2480 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
# This is what a comment looks like
fruits = ['apples', 'oranges', 'pears', 'bananas']
for fruit in fruits:
print(fruit + ' for sale')
fruitPrices = {'apples': 2.00, 'oranges': 1.50, 'pears': 1.75}
for fruit, price in fruitPrices.items():
if price < 2.00:
print('%s cost %f a pound' % (fruit, price))
else:
print(fruit + ' are too expensive!')
+3
View File
@@ -0,0 +1,3 @@
# Comment: This is a fairly simple Python script
print('Hello, World!')
+5
View File
@@ -0,0 +1,5 @@
nums = [1, 2, 3, 4, 5, 6]
oddNums = [x for x in nums if x % 2 == 1]
print(oddNums)
oddNumsPlusOne = [x + 1 for x in nums if x % 2 == 1]
print(oddNumsPlusOne)
+2
View File
@@ -0,0 +1,2 @@
strings = ['Some string', 'Art', 'Music', 'Artificial Intelligence']
print([x.lower() for x in strings if len(x) > 5])
+12
View File
@@ -0,0 +1,12 @@
def quickSort(lst):
if len(lst) <= 1:
return lst
smaller = [x for x in lst[1:] if x < lst[0]]
larger = [x for x in lst[1:] if x >= lst[0]]
return quickSort(smaller) + [lst[0]] + quickSort(larger)
# Main Function
if __name__ == '__main__':
lst = [2, 4, 5, 1]
print(quickSort(lst))
+5
View File
@@ -0,0 +1,5 @@
def quicksort(arr):
pivot = arr[0]
for item in arr[1:]:
if item > pivot:
+43
View File
@@ -0,0 +1,43 @@
class FruitShop:
def __init__(self, name, fruitPrices):
"""
name: Name of the fruit shop
fruitPrices: Dictionary with keys as fruit
strings and prices for values e.g.
{'apples':2.00, 'oranges': 1.50, 'pears': 1.75}
"""
self.fruitPrices = fruitPrices
self.name = name
print('Welcome to %s fruit shop' % (name))
def getCostPerPound(self, fruit):
"""
fruit: Fruit string
Returns cost of 'fruit', assuming 'fruit'
is in our inventory or None otherwise
"""
if fruit not in self.fruitPrices:
print("Sorry we don't have %s" % (fruit))
return None
return self.fruitPrices[fruit]
def getPriceOfOrder(self, orderList):
"""
orderList: List of (fruit, numPounds) tuples
Returns cost of orderList. If any of the fruit are
"""
totalCost = 0.0
for fruit, numPounds in orderList:
costPerPound = self.getCostPerPound(fruit)
if costPerPound != None:
totalCost += numPounds * costPerPound
return totalCost
def getName(self):
return self.name
def __str__(self):
return "<FruitShop: %s>" % self.getName()
+16
View File
@@ -0,0 +1,16 @@
import shop
shopName = 'the Berkeley Bowl'
fruitPrices = {'apples': 1.00, 'oranges': 1.50, 'pears': 1.75}
berkeleyShop = shop.FruitShop(shopName, fruitPrices)
applePrice = berkeleyShop.getCostPerPound('apples')
print(applePrice)
print('Apples cost $%.2f at %s.' % (applePrice, shopName))
otherName = 'the Stanford Mall'
otherFruitPrices = {'kiwis': 6.00, 'apples': 4.50, 'peaches': 8.75}
otherFruitShop = shop.FruitShop(otherName, otherFruitPrices)
otherPrice = otherFruitShop.getCostPerPound('apples')
print(otherPrice)
print('Apples cost $%.2f at %s.' % (otherPrice, otherName))
print("My, that's expensive!")