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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__pycache__/
__pycache__/
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\Christopher\\AppData\\Local\\Programs\\Python\\Python38-32\\python.exe"
}
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Exercises

| Module ID | Module Name |
|:-----------:|:--------:|
| 1 | [Introduction and Environment](https://github.com/ByteAcademyCo/Exercises/tree/master/introduction_and_environment) |
| 2 | [Introduction to Python](https://github.com/ByteAcademyCo/Exercises/tree/master/introduction_to_python) |
| 3 | [Data Structures](https://github.com/ByteAcademyCo/Exercises/tree/master/data_structures) |
| 4 | [Algorithms](https://github.com/ByteAcademyCo/Exercises/tree/master/algorithms) |
| 5 | [Software Theory](https://github.com/ByteAcademyCo/Exercises/tree/master/software_theory) |
# Exercises
| Module ID | Module Name |
|:-----------:|:--------:|
| 1 | [Introduction and Environment](https://github.com/ByteAcademyCo/Exercises/tree/master/introduction_and_environment) |
| 2 | [Introduction to Python](https://github.com/ByteAcademyCo/Exercises/tree/master/introduction_to_python) |
| 3 | [Data Structures](https://github.com/ByteAcademyCo/Exercises/tree/master/data_structures) |
| 4 | [Algorithms](https://github.com/ByteAcademyCo/Exercises/tree/master/algorithms) |
| 5 | [Software Theory](https://github.com/ByteAcademyCo/Exercises/tree/master/software_theory) |
16 changes: 8 additions & 8 deletions algorithms/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Exercise Organization

## Exercise Sections
* Graph Traversal
* Sorting
* Bit Manipulation
* String Manipulation
* Dynamic Programming
# Exercise Organization
## Exercise Sections
* Graph Traversal
* Sorting
* Bit Manipulation
* String Manipulation
* Dynamic Programming
4 changes: 2 additions & 2 deletions algorithms/dynamic_programming/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"python.pythonPath": "/usr/local/bin/python3"
{
"python.pythonPath": "/usr/local/bin/python3"
}
16 changes: 8 additions & 8 deletions algorithms/dynamic_programming/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Exercises for Dynamic Programming

* 1_Dynamic_Programming1
* 1_Dynamic_Programming2
* 1_Dynamic_Programming3
* 2_Dynamic_Programming1
* 2_Dynamic_Programming2
* 3_Dynamic_Programming1
# Exercises for Dynamic Programming
* 1_Dynamic_Programming1
* 1_Dynamic_Programming2
* 1_Dynamic_Programming3
* 2_Dynamic_Programming1
* 2_Dynamic_Programming2
* 3_Dynamic_Programming1
40 changes: 20 additions & 20 deletions algorithms/dynamic_programming/Tribonacci/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Dynamic Programming Recursion - Tribonacci Sequence

## Motivation
We use Dynamic Programming algorithms when we have overlapping subproblems. If we have a naive recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. We can store the results of the subproblems, so that we do not have to re-compute them when needed later.
Dynamic programing can optimize the time complexity of a problem from exponential `O(2^n)` to polynomial `O(n^c)`, for some constant `c`.

## Problem Description
The Tribonacci sequence is defined as `T(0) = 0`, `T(1) = 0`, `T(2) = 1`, and `T(n) = T(n-1) + T(n-2) + T(n-3)` for `n>=3`. In the *solution.py* file, using dynamic programming, define a recursive function `tribonacci` that consumes a positive integer `n` and returns the `nth` tribonacci number.

## Example
```
tribonacci(3) == 1
tribonacci(6) == 7
tribonacci(10) == 81
```

## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory

## Submission
# Dynamic Programming Recursion - Tribonacci Sequence
## Motivation
We use Dynamic Programming algorithms when we have overlapping subproblems. If we have a naive recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. We can store the results of the subproblems, so that we do not have to re-compute them when needed later.
Dynamic programing can optimize the time complexity of a problem from exponential `O(2^n)` to polynomial `O(n^c)`, for some constant `c`.
## Problem Description
The Tribonacci sequence is defined as `T(0) = 0`, `T(1) = 0`, `T(2) = 1`, and `T(n) = T(n-1) + T(n-2) + T(n-3)` for `n>=3`. In the *solution.py* file, using dynamic programming, define a recursive function `tribonacci` that consumes a positive integer `n` and returns the `nth` tribonacci number.
## Example
```
tribonacci(3) == 1
tribonacci(6) == 7
tribonacci(10) == 81
```
## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory
## Submission
* Submit your answers in the *solution.py* file within the *solution* subdirectory within this directory
28 changes: 14 additions & 14 deletions algorithms/dynamic_programming/Tribonacci/solution/solution.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
def tribonacci(n):
if n == 0:
return 0
elif n == 1:
return 0
elif n == 2:
return 1
# create a memo list to store the ith tribonacci number at memo[i]
memo = [-1 for i in range(n+1)]
memo[0] = 0
memo[1] = 0
memo[2] = 1
for i in range(3, n+1):
memo[i] = memo[i-3] + memo[i-2] + memo[i-1]
def tribonacci(n):
if n == 0:
return 0
elif n == 1:
return 0
elif n == 2:
return 1
# create a memo list to store the ith tribonacci number at memo[i]
memo = [-1 for i in range(n+1)]
memo[0] = 0
memo[1] = 0
memo[2] = 1
for i in range(3, n+1):
memo[i] = memo[i-3] + memo[i-2] + memo[i-1]
return memo[i]
22 changes: 11 additions & 11 deletions algorithms/dynamic_programming/Tribonacci/solution/test_solution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
def test_solution():
import solution

assert solution.tribonacci(0) == 0
assert solution.tribonacci(1) == 0
assert solution.tribonacci(2) == 1
assert solution.tribonacci(3) == 1
assert solution.tribonacci(4) == 2
assert solution.tribonacci(5) == 4
assert solution.tribonacci(10) == 81
assert solution.tribonacci(15) == 1705
def test_solution():
import solution
assert solution.tribonacci(0) == 0
assert solution.tribonacci(1) == 0
assert solution.tribonacci(2) == 1
assert solution.tribonacci(3) == 1
assert solution.tribonacci(4) == 2
assert solution.tribonacci(5) == 4
assert solution.tribonacci(10) == 81
assert solution.tribonacci(15) == 1705
assert solution.tribonacci(19) == 19513
36 changes: 18 additions & 18 deletions algorithms/dynamic_programming/factorial/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Simple Recursion - Factorial

## Problem Description
In the *solution.py* file, define a recursive function `factorial` that consumes a positive integer `n` and returns the factorial of `n`. We define the factorial of `n` as `n! = n*(n-1)*(n-2)* ... *1`.

## Example
```
factorial(1) == 1
factorial(2) == 2
factorial(5) == 120
```


## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory

## Submission
* Submit your answers in the *solution.py* file within the *solution* subdirectory within this directory
# Simple Recursion - Factorial
## Problem Description
In the *solution.py* file, define a recursive function `factorial` that consumes a positive integer `n` and returns the factorial of `n`. We define the factorial of `n` as `n! = n*(n-1)*(n-2)* ... *1`.
## Example
```
factorial(1) == 1
factorial(2) == 2
factorial(5) == 120
```
## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory
## Submission
* Submit your answers in the *solution.py* file within the *solution* subdirectory within this directory
8 changes: 4 additions & 4 deletions algorithms/dynamic_programming/factorial/solution/solution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def factorial(n):
if n <= 1:
return 1
else:
def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n-1)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def test_solution():
import solution

assert solution.factorial(1) == 1
assert solution.factorial(2) == 2
assert solution.factorial(5) == 120
assert solution.factorial(10) == 3628800

def test_solution():
import solution
assert solution.factorial(1) == 1
assert solution.factorial(2) == 2
assert solution.factorial(5) == 120
assert solution.factorial(10) == 3628800
36 changes: 18 additions & 18 deletions algorithms/dynamic_programming/is_palindrome/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Simple Recursion - Is Palindrome

## Problem Description
In the *solution.py* file, define a recursive function `is_palindrome` that consumes a string `str` and returns the boolean value `True` if the string is a palindrome and `False` otherwise. A palindrome is defined as a string of letters that reads the same forwards and backwards.

## Example
```
is_palindrome("racecar") == True
is_palindrome("mom") == True
is_palindrome("shrek") == False
```


## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory

## Submission
* Submit your answers in the *solution.py* file within the *solution* subdirectory within this directory
# Simple Recursion - Is Palindrome
## Problem Description
In the *solution.py* file, define a recursive function `is_palindrome` that consumes a string `str` and returns the boolean value `True` if the string is a palindrome and `False` otherwise. A palindrome is defined as a string of letters that reads the same forwards and backwards.
## Example
```
is_palindrome("racecar") == True
is_palindrome("mom") == True
is_palindrome("shrek") == False
```
## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory
## Submission
* Submit your answers in the *solution.py* file within the *solution* subdirectory within this directory
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def is_palindrome(string):
# a string of len 1 is by defnition a palindrome so we return True
if len(string) <= 1:
return True
else:
# if the first and last charaters are equal
# we recurse in the rest of the string removing the
# first and last character to check if the rest of
# the string is a palindrome.
def is_palindrome(string):
# a string of len 1 is by defnition a palindrome so we return True
if len(string) <= 1:
return True
else:
# if the first and last charaters are equal
# we recurse in the rest of the string removing the
# first and last character to check if the rest of
# the string is a palindrome.
return string[0] == string[-1] and is_palindrome(string[1:-1])
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
def test_solution():
import solution

assert solution.is_palindrome("racecar") == True
assert solution.is_palindrome("a") == True
assert solution.is_palindrome("racecars") == False
assert solution.is_palindrome("lol") == True
assert solution.is_palindrome("noon") == True
assert solution.is_palindrome("at") == False
assert solution.is_palindrome("shrek") == False

def test_solution():
import solution
assert solution.is_palindrome("racecar") == True
assert solution.is_palindrome("a") == True
assert solution.is_palindrome("racecars") == False
assert solution.is_palindrome("lol") == True
assert solution.is_palindrome("noon") == True
assert solution.is_palindrome("at") == False
assert solution.is_palindrome("shrek") == False
44 changes: 22 additions & 22 deletions algorithms/dynamic_programming/knap_sack_problem/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Dynamic Programming Recursion - Knapsack Problem

## Motivation
We use Dynamic Programming algorithms when we have overlapping subproblems. If we have a naive recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. We can store the results of the subproblems, so that we do not have to re-compute them when needed later.
Dynamic programing can optimize the time complexity of a problem from exponential `O(2^n)` to polynomial `O(n^c)`, for some constant `c`.

## Problem Description
You are going on a camping trip and you have a knapsack you wish to pack with a weight capacity of `x` lbs. You are given a dictiomary where the keys are strings containing the names of items which you wish bring on your trip, which are mapped to a touple, containing the value of the given item and weight in lbs of the given item. The higher the value of the item, the more important it is to you to bring on your trip.

In the *solution.py* file, using dynamic programming, define a function `knap_sack` that consumes a positive integer `x` and a dictionary `items`, and returns the maximal value of items you can bring in your knapsack without exceeding the maximal capacity `x`.

## Example
```
items = {"hat": (1,2), "sunscreen": (3,2), "food": (6,4), "water": (5,3)}
knap_sack(5, items) == 8
```
You could either bring a hat and water on your trip, which would give you a total weight of 5lbs in your knapsack, and a value of 6. Or you could only bring food on your trip, which would give you total weight of 4lbs in your knapsack and a value of 6.

## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory

## Submission
# Dynamic Programming Recursion - Knapsack Problem
## Motivation
We use Dynamic Programming algorithms when we have overlapping subproblems. If we have a naive recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. We can store the results of the subproblems, so that we do not have to re-compute them when needed later.
Dynamic programing can optimize the time complexity of a problem from exponential `O(2^n)` to polynomial `O(n^c)`, for some constant `c`.
## Problem Description
You are going on a camping trip and you have a knapsack you wish to pack with a weight capacity of `x` lbs. You are given a dictiomary where the keys are strings containing the names of items which you wish bring on your trip, which are mapped to a touple, containing the value of the given item and weight in lbs of the given item. The higher the value of the item, the more important it is to you to bring on your trip.
In the *solution.py* file, using dynamic programming, define a function `knap_sack` that consumes a positive integer `x` and a dictionary `items`, and returns the maximal value of items you can bring in your knapsack without exceeding the maximal capacity `x`.
## Example
```
items = {"hat": (1,2), "sunscreen": (3,2), "food": (6,4), "water": (5,3)}
knap_sack(5, items) == 8
```
You could either bring a hat and water on your trip, which would give you a total weight of 5lbs in your knapsack, and a value of 6. Or you could only bring food on your trip, which would give you total weight of 4lbs in your knapsack and a value of 6.
## Testing
* To test your solution, type 'pytest' within the **solution** subdirectory
## Submission
* Submit your answers in the *solution.py* file within the *solution* subdirectory within this directory
Loading