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
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/Algorithms.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.7"
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion eating_cookies/eating_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
# a solution that is more efficient than the naive
# recursive solution
def eating_cookies(n, cache=None):
pass
if n < 0:
return 0
elif n == 0:
return 1
elif n == 1:
return 1
elif n == 2:
return 2
else:
return eating_cookies(n - 1) + eating_cookies(n - 2) + eating_cookies(n - 3)

print(eating_cookies(5))

if __name__ == "__main__":
if len(sys.argv) > 1:
Expand Down
23 changes: 18 additions & 5 deletions knapsack/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# The Knapsack Problem

Suppose you are Indiana Jones, and you have found the secret entrance to the Temple of Doom. Before you is a multitude of artifacts and treasures - pots, gemstones, works of art, and more. These belong in a museum! But there are soldiers hot on your heels, and you can only carry so much...
Suppose you are Indiana Jones, and you have found the secret entrance to the Temple of Doom.
Before you is a multitude of artifacts and treasures - pots, gemstones, works of art, and more.
These belong in a museum! But there are soldiers hot on your heels, and you can only carry so much...

You, brave explorer, are facing the knapsack problem - maximizing the value of a set of items you select that are constrained by total size/weight. The size and the value of an item need not be correlated - the most precious item may be a tiny gemstone. But it turns out it's pretty tricky to get a truly optimal solution, and that a bruteforce approach really doesn't scale.
You, brave explorer, are facing the knapsack problem - maximizing the value of a set of items you select that
are constrained by total size/weight. The size and the value of an item need not be correlated
- the most precious item may be a tiny gemstone. But it turns out it's pretty tricky to get a truly
optimal solution, and that a bruteforce approach really doesn't scale.

A bit more motivation - this is a very general optimization problem that can be applied in a multitude of situations, from resource selection and allocation to stuffing stolen goods in knapsacks.
A bit more motivation - this is a very general optimization problem that can be applied in a multitude of
situations, from resource selection and allocation to stuffing stolen goods in knapsacks.

![xkcd "NP-Complete"](https://imgs.xkcd.com/comics/np_complete.png "General solutions get you a 50% tip.")

Expand All @@ -23,9 +29,16 @@ The specific goal of this exercise is to write a program that takes input files
10 34 12
```

The first row number is just a row/observation number, to facilitate reading and referring to items. The second number is the size/cost of the item, i.e. the cost of putting it in your knapsack. The third number is the value, i.e. the utility/payoff you get for selecting that item. The program should also take as input a total size, which can just be a number passed from the command line. So execution may look like this: `python knapsack.py input.txt 100`.
The first row number is just a row/observation number, to facilitate reading and referring to items.
The second number is the size/cost of the item, i.e. the cost of putting it in your knapsack.
The third number is the value, i.e. the utility/payoff you get for selecting that item.
The program should also take as input a total size, which can just be a number passed from the command line.
So execution may look like this: `python knapsack.py input.txt 100`.

The goal is to select a subset of the items to maximize the payoff such that the cost is below some threshold. That is, the output should be a set of items (identified by number) that solves the Knapsack problem. It's also worth outputting the total cost and value of these items. This can all just be printed and may look something like below.
The goal is to select a subset of the items to maximize the payoff such that the cost is below some threshold.
That is, the output should be a set of items (identified by number) that solves the Knapsack problem.
It's also worth outputting the total cost and value of these items. This can all just be printed and
may look something like below.

This is *not* a solution, just an example:

Expand Down
30 changes: 30 additions & 0 deletions notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# #!/usr/bin/python
#
# import argparse
#
# # track of the `current_min_price_so_far` and the `max_profit_so_far`?
#
# prices = [10, 7, 5, 8, 11, 9]
# # # return 6 === (11 - 5)
# # print('prices in', prices)
#
# def find_max_profit(prices):
# max_profit = 0
# for x in prices:
# for y in prices:
# print(y)
#
# return max_profit
#
# find_max_profit(prices)




def recursiveFunction(n):
if n == 0:
return 1
print(n)
return n * recursiveFunction(n - 1)

recursiveFunction(9)
10 changes: 7 additions & 3 deletions recipe_batches/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Recipe Batches

Write a function that receives a recipe in the form of a dictionary, as well as all of the ingredients you have available to you, also in the form of a dictionary. Both of these dictionaries will have the same form, and might look something like this:
Write a function that receives a recipe in the form of a dictionary, as well as all of the ingredients you have
available to you, also in the form of a dictionary. Both of these dictionaries will have the same form, ex:

```python
{
Expand All @@ -11,9 +12,12 @@ Write a function that receives a recipe in the form of a dictionary, as well as
}
```

The keys will be the ingredient names, with their associated values being the amounts. In the case of the recipe dictionary, these amounts will represent the amount of each ingredient needed for the recipe, while in the case of the ingredients dictionary, the amounts represent the amounts available to you.
The keys will be the ingredient names, with their associated values being the amounts. In the case of the recipe
dictionary, these amounts will represent the amount of each ingredient needed for the recipe, while in the case of the
ingredients dictionary, the amounts represent the amounts available to you.

Your function should output the maximum number of whole batches that can be made for the supplied recipe using the ingredients available to you, as indicated by the second dictionary.
Your function should output the maximum number of whole batches that can be made for the supplied recipe using the
ingredients available to you, as indicated by the second dictionary.

For example

Expand Down
85 changes: 77 additions & 8 deletions recipe_batches/recipe_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,82 @@

import math

def recipe_batches(recipe, ingredients):
pass
# 'milk': 100, 'flour': 4, 'sugar': 10, 'butter': 5 }, { 'milk': 1288, 'flour': 9, 'sugar': 95

# SAMPLE INPUT
recipes = {
'milk': 100, 'butter': 50, 'cheese': 10
}

if __name__ == '__main__':
# Change the entries of these dictionaries to test
# your implementation with different inputs
recipe = { 'milk': 100, 'butter': 50, 'flour': 5 }
ingredients = { 'milk': 132, 'butter': 48, 'flour': 51 }
print("{batches} batches can be made from the available ingredients: {ingredients}.".format(batches=recipe_batches(recipe, ingredients), ingredients=ingredients))
ingredients = {
'milk': 1948, 'butter': 542, 'cheese': 20
}
# EXPECTED SAMPLE RETURN === 0

def recipe_batches(recipes, ingredients):
# UPER
# - UNDERSTAND
# TAKE IN RECIPE AND INGREDIENTS DICTIONARIES
# SEE HOW MANY DISHES CAN BE MADE FROM COMPARING INPUTS
# RETURN INT FOR HOW MANY DISHES CAN BE MADE
# P - PLAN
# compare each position of the dictionary(a) to the next dictionary(b)
# return a // b
# do for each item in dictionary
# final return smallest number from above
# x = 0
# recipes[x] - ingredients[x]
numOfBatches = 0

# for recipe in recipes:
# r = recipe
# # print(r)
# print(recipes.get(r))
#
# for ingredient in ingredients:
# i = ingredient
# print(ingredients.get(i))
#
# recipes[r] - ingredients[i]
# if recipes[r] > 0:
# print('r', recipes[r])

max_batches = 0
for recipe_ingredient in recipes:
if recipe_ingredient not in ingredients:
print('nothing')
return 0
elif recipe_ingredient in ingredients:
current_max = ingredients[recipe_ingredient] // recipes[recipe_ingredient]
print('x',current_max)
if current_max <= 0:
return 0
elif max_batches == 0 or current_max < max_batches and recipe_ingredient not in ingredients:
max_batches = current_max
print(max_batches)
return max_batches

recipe_batches(recipes, ingredients)















# PUT BACK IN TO RUN TESTS

# if __name__ == '__main__':
# # Change the entries of these dictionaries to test
# # your implementation with different inputs
# recipe = { 'milk': 100, 'butter': 50, 'flour': 5 }
# ingredients = { 'milk': 132, 'butter': 48, 'flour': 51 }
# print("{batches} batches can be made from the available ingredients: {ingredients}.".format(batches=recipe_batches(recipe, ingredients), ingredients=ingredients))
19 changes: 19 additions & 0 deletions recipe_batches/runtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# O(n)
# o(1)
# 0(log(n))
# O(n^2)
# O(nlog(n))
# O(2^n)
# O(n^n)

# O(n)
def functionTest(x):
y = x^2 * 3 ^x
return y

#O(1)
def funky(arr):
y = 1
for x in arr:
y += x
return y
30 changes: 28 additions & 2 deletions rock_paper_scissors/rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,34 @@

import sys

def rock_paper_scissors(n):
pass
def rock_paper_scissors(number_of_plays):
all_moves = []
move_options = ['rock', 'paper', 'scissors']

def build_moves(countdown, piece_being_built):
if countdown == 0:
all_moves.append(piece_being_built)
return

for move in move_options:
new_piece_result = piece_being_built + [move]
build_moves(countdown -1, new_piece_result)
build_moves(number_of_plays, [])

return all_moves

print(rock_paper_scissors(2))













if __name__ == "__main__":
Expand Down
13 changes: 9 additions & 4 deletions stock_prices/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Stock Prices

You want to write a bot that will automate the task of day-trading for you while you're going through Lambda. You decide to have your bot just focus on buying and selling Amazon stock.
You want to write a bot that will automate the task of day-trading for you while you're going through Lambda.
You decide to have your bot just focus on buying and selling Amazon stock.

Write a function `find_max_profit` that receives as input a list of stock prices. Your function should return the maximum profit that can be made from a single buy and sell. You must buy first before selling; no shorting is allowed here.
Write a function `find_max_profit` that receives as input a list of stock prices. Your function should return the
maximum profit that can be made from a single buy and sell. You must buy first before selling; no shorting is allowed

For example, `find_max_profit([1050, 270, 1540, 3800, 2])` should return 3530, which is the maximum profit that can be made from a single buy and then sell of these stock prices.
For example, `find_max_profit([1050, 270, 1540, 3800, 2])` should return 3530, which is the maximum profit that can be
made from a single buy and then sell of these stock prices.

## Testing

Expand All @@ -14,6 +17,8 @@ You can also test your implementation manually by executing `python stock_prices

## Hints

For this problem, we essentially want to find the maximum difference between the smallest and largest prices in the list of prices, but we also have to make sure that the max profit is computed by subtracting some price by another price that comes _before_ it; it can't come after it in the list of prices.
For this problem, we essentially want to find the maximum difference between the smallest and largest prices in the
list of prices, but we also have to make sure that the max profit is computed by subtracting some price by another
price that comes _before_ it; it can't come after it in the list of prices.

So what if we kept track of the `current_min_price_so_far` and the `max_profit_so_far`?
Loading