Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Python/companies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


# Define a class to represent companies. Companies will have a name (given at creation),
# a list of all sales for the year (string of a product), and a dictionary of products (strings, keys)
# and their prices (float like, values), as well as a list of employees. Define a function to add a
# new product. The function will take the name of the product and the price. Define a function to add
# an employee, given an employee. Define an employee class. Employees will have a name as a string,
# and a company, both given at creation. They will also have a role given at creation.
# A role is represented by a number, with 1 being a new hire, and 10 the ceo.
# Make the role value optional, with the default set to 1.

# Make sure to test your code by writing some test cases!

class Company:
def __init__(self, name):
self.name = name
self.sales = []
self.products = dict() # product_name : product_price
self.employees = []

def add_employee(self, employee):
self.employees.append(employee)


class Employee:
def __init__(self, name, company, role=1):
self.name = name
self.company = company
self.role = role


def test():
amazon = Company("Amazon")
assert amazon.name == "Amazon"
ceo = Employee("Jeff Bezos", amazon, 10)
assert ceo.name == "Jeff Bezos"
assert ceo.role == 10
assert ceo.company.name == "Amazon"
amazon.add_employee(ceo)
assert amazon.employees == [ceo]
print("Tests passed")


if __name__ == "__main__":
test()
11 changes: 9 additions & 2 deletions Python/random_ints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import random


def random_ints():
# Your code here!
ls = []
r = 0
while r != 6:
r = random.randint(1, 10)
ls.append(r)
return ls


def test():
Expand All @@ -13,8 +19,9 @@ def test():
assert not 11 in l
assert l[-1] == 6
total_length += len(l)
assert abs(total_length / N - 10) < 1 # checks that the length of the random strings are reasonable.
assert abs(total_length / N - 10) < 1 # checks that the length of the random lists are reasonable.
print("Success!")


if __name__ == "__main__":
test()
16 changes: 11 additions & 5 deletions Python/rm_smallest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
def rm_smallest(d):
# Your code here!
return 0;
try:
c = dict(d)
del c[min(d, key=d.get)]
return c
except ValueError:
return d


def test():
assert 'a' in rm_smallest({'a':1,'b':-10}).keys()
assert not 'b' in rm_smallest({'a':1,'b':-10}).keys()
assert not 'a' in rm_smallest({'a':1,'b':5,'c':3}).keys()
assert 'a' in rm_smallest({'a': 1, 'b': -10}).keys()
assert not 'b' in rm_smallest({'a': 1, 'b': -10}).keys()
assert not 'a' in rm_smallest({'a': 1, 'b': 5, 'c': 3}).keys()
rm_smallest({})
print("Success!")


if __name__ == "__main__":
test()
9 changes: 7 additions & 2 deletions Python/square_root.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import math


def square_root(n):
# Your code here!
return -1;
if not isinstance(n, int) or n < 0:
return -1
else:
return math.sqrt(n)


def test():
assert square_root(4) == 2
Expand All @@ -11,5 +15,6 @@ def test():
assert square_root(-10) == -1
print("Success!")


if __name__ == "__main__":
test()
12 changes: 8 additions & 4 deletions Python/sum.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
def sum(lst, n):
# Your code here!
return False
total = 0
for i in lst:
total += i
return total == n


def test():
assert sum([-1, 1], 0)
assert not sum([0,2,3], 4)
assert sum([0,2,2], 4)
assert not sum([0, 2, 3], 4)
assert sum([0, 2, 2], 4)
print("Success!")


if __name__ == "__main__":
test()