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
30 changes: 15 additions & 15 deletions Short-Answer/Algorithms_Answers.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#### Please add your answers to the ***Analysis of Algorithms*** exercises here.

## Exercise I

a)


b)


c)

## Exercise II


#### Please add your answers to the ***Analysis of Algorithms*** exercises here.
## Exercise I
a) It's a linear complexity, O(n) since there is one input and output
b) It's a quadratic complexity, O(n^2) as the for while iterates n * the number of for loops. n * n
c) It's a linear complexity as O(n) the function is called recursively n times before it reaches its base case
## Exercise II
72 changes: 36 additions & 36 deletions Short-Answer/Algorithms_Questions.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# Analysis of Algorithms

## Exercise I

Give an analysis of the running time of each snippet of
pseudocode with respect to the input size n of each of the following:

```python
a) a = 0
while (a < n * n * n):
a = a + n * n
```


```
b) sum = 0
for i in range(n):
j = 1
while j < n:
j *= 2
sum += 1
```

```
c) def bunnyEars(bunnies):
if bunnies == 0:
return 0

return 2 + bunnyEars(bunnies-1)
```

## Exercise II

Suppose that you have an n-story building and plenty of eggs. Suppose also that an egg gets broken if it is thrown off floor f or higher, and doesn't get broken if dropped off a floor less than floor f. Devise a strategy to determine the value of f such that the number of dropped + broken eggs is minimized.

Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution.
# Analysis of Algorithms
## Exercise I
Give an analysis of the running time of each snippet of
pseudocode with respect to the input size n of each of the following:
```python
a) a = 0
while (a < n * n * n):
a = a + n * n
```
```
b) sum = 0
for i in range(n):
j = 1
while j < n:
j *= 2
sum += 1
```
```
c) def bunnyEars(bunnies):
if bunnies == 0:
return 0
return 2 + bunnyEars(bunnies-1)
```
## Exercise II
Suppose that you have an n-story building and plenty of eggs. Suppose also that an egg gets broken if it is thrown off floor f or higher, and doesn't get broken if dropped off a floor less than floor f. Devise a strategy to determine the value of f such that the number of dropped + broken eggs is minimized.
Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution.
Binary file not shown.
25 changes: 15 additions & 10 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'''
Your function should take in a single parameter (a string `word`)
Your function should return a count of how many occurences of ***"th"*** occur within `word`. Case matters.
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):

# TBC

pass
'''
Your function should take in a single parameter (a string `word`)
Your function should return a count of how many occurences of ***"th"*** occur within `word`. Case matters.
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):

# TBC
if len(word) <= 1:
return 0
elif word[0] == "t" and word[1] == "h":
return count_th(word[1:]) + 1
else:
return count_th(word[1:])
pass
74 changes: 37 additions & 37 deletions recursive_count_th/test_count_th.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import unittest
import random
from count_th import *

class Test(unittest.TestCase):

def setUp(self):
self.word = ""

def test_count_th_empty(self):
self.word = ""
count = count_th(self.word)
self.assertEqual(0, count)

def test_count_th_single(self):
self.word = "abcthxyz"
count = count_th(self.word)
self.assertEqual(1, count)

def test_count_th_multiple(self):
self.word = "abcthefthghith"
count = count_th(self.word)
self.assertEqual(3, count)

def test_count_backwards(self):
self.word = "thhtthht"
count = count_th(self.word)
self.assertEqual(2, count)

def test_count_th_mixedcase(self):
self.word = "THtHThth"
count = count_th(self.word)
self.assertEqual(1, count)


if __name__ == '__main__':
unittest.main()
import unittest
import random
from count_th import *
class Test(unittest.TestCase):
def setUp(self):
self.word = ""
def test_count_th_empty(self):
self.word = ""
count = count_th(self.word)
self.assertEqual(0, count)
def test_count_th_single(self):
self.word = "abcthxyz"
count = count_th(self.word)
self.assertEqual(1, count)
def test_count_th_multiple(self):
self.word = "abcthefthghith"
count = count_th(self.word)
self.assertEqual(3, count)
def test_count_backwards(self):
self.word = "thhtthht"
count = count_th(self.word)
self.assertEqual(2, count)
def test_count_th_mixedcase(self):
self.word = "THtHThth"
count = count_th(self.word)
self.assertEqual(1, count)
if __name__ == '__main__':
unittest.main()
Binary file added robot_sort/__pycache__/robot_sort.cpython-37.pyc
Binary file not shown.
Loading