-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from code4tomorrow/dsa-practice
DSA Practice Problems
- Loading branch information
Showing
44 changed files
with
2,567 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" | ||
For each of the following time complexities, create | ||
a function that has that time complexity. | ||
""" | ||
|
||
# time complexity: O(1) | ||
# your code here | ||
|
||
|
||
# time complexity: O(n) | ||
# your code here | ||
|
||
|
||
# time complexity: O(n^2) | ||
# your code here | ||
|
||
|
||
# time complexity: O(log(n)) | ||
# your code here | ||
|
||
# time complexity: O(n * log(n)) | ||
# your code here | ||
|
||
# time complexity: O(2**n) | ||
# your code here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
Classify the following code examples with their | ||
runtimes. Write your responses as comments. | ||
""" | ||
|
||
|
||
def do_something(): | ||
# runtime for do_something() is O(1) | ||
pass | ||
|
||
|
||
# what is the runtime for example 1? | ||
def example_one(n): | ||
for i in range(n): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 2? | ||
def example_two(n): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 3? | ||
def example_three(n): | ||
for i in range(n): | ||
for x in range(i): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 4? | ||
def example_four(n): | ||
for i in range(n // 2): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 5? | ||
def example_five(n): | ||
i = 0 | ||
while i < n: | ||
do_something() | ||
i *= 2 | ||
|
||
|
||
# what is the runtime for example 6? | ||
def example_six(n): | ||
for i in range(10): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 7? | ||
def example_seven(n): | ||
for i in range(2**n): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 8? | ||
def example_eight(n): | ||
for i in range(n): | ||
for x in range(7): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 9? | ||
def example_nine(n): | ||
for i in range(n): | ||
example_one(n) | ||
|
||
|
||
# what is the runtime for example 10? | ||
def example_ten(n): | ||
i = 0 | ||
while i < n: | ||
do_something() | ||
i += 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
For each of the following time complexities, create | ||
a function that has that time complexity. The following solutions | ||
are examples and not the only ways to have done this problem. | ||
""" | ||
|
||
|
||
# time complexity: O(1) | ||
def double_my_number(number): | ||
x = number | ||
x *= 2 | ||
return x | ||
|
||
|
||
# time complexity: O(n) | ||
def sum_till_n(n): | ||
total = 0 | ||
for i in range(n): | ||
total += i | ||
|
||
return total | ||
|
||
|
||
# time complexity: O(n^2) | ||
def print_triangle(n): | ||
for row in range(n): | ||
for column in range(row): | ||
print("* ", end="") | ||
print() | ||
|
||
|
||
# time complexity: O(log(n)) | ||
def sum_powers_of_two(max_number): | ||
power_of_two = 1 | ||
total = 0 | ||
|
||
while power_of_two < max_number: | ||
total += power_of_two | ||
power_of_two *= 2 | ||
|
||
return total | ||
|
||
|
||
# time complexity: O(n * log(n)) | ||
def sum_many_powers_of_two(number_of_times): | ||
total = 0 | ||
for i in range(number_of_times): | ||
# since sum_powers_of_two is O(log(n))and this for loop is O(n), | ||
# the resulting time complexity is O(n * log(n)) | ||
total += sum_powers_of_two(i) | ||
|
||
return total | ||
|
||
|
||
# time complexity: O(2**n) | ||
def get_binary_combinations(number_of_digits): | ||
""" | ||
Gets the combinations of binary numbers with number_of_digits digits | ||
For example, get_binary_combinations(2) should give | ||
["00", "01", "10", "11"]. | ||
""" | ||
cur_options = ["0", "1"] | ||
next_options = [] | ||
|
||
operations = 0 | ||
|
||
# In total, this is O(2**n). It may be a bit confusing, but | ||
# this is O(2**n) because of the fact that the current options | ||
# doubles each time we go through the for loop, so it has to | ||
# spend twice as long each time. | ||
for i in range(number_of_digits - 1): | ||
for option in cur_options: | ||
next_options.append(option + "0") | ||
next_options.append(option + "1") | ||
operations += 1 | ||
cur_options = next_options | ||
next_options = [] | ||
|
||
print(f"took {operations} operations") | ||
return cur_options | ||
|
||
|
||
# for comparison, here's a very easy to understand | ||
# function with O(2**n) runtime. | ||
def regular_o_2_to_the_n(n): | ||
operations = 0 | ||
for i in range(2**n): | ||
operations += 1 | ||
print(f"took {operations} operations") | ||
|
||
|
||
# if you actually don't believe that get_binary_combinations is O(2**n), | ||
# try running the below. | ||
# as you can see, they have similar operational cost, | ||
# meaning that get_binary_combinations really is O(2**n) | ||
# times = 20 | ||
# get_binary_combinations(times) | ||
# regular_o_2_to_the_n(times) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
Classify the following code examples with their | ||
runtimes. Write your responses as comments. | ||
""" | ||
|
||
|
||
def do_something(): | ||
# runtime for do_something() is O(1) | ||
pass | ||
|
||
|
||
# what is the runtime for example 1? | ||
# runtime is O(n) | ||
def example_one(n): | ||
for i in range(n): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 2? | ||
# runtime is O(1) | ||
def example_two(n): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 3? | ||
# runtime is O(n^2) | ||
def example_three(n): | ||
for i in range(n): | ||
for x in range(i): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 4? | ||
# runtime is O(n) | ||
def example_four(n): | ||
for i in range(n // 2): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 5? | ||
# runtime is O(log(n)) | ||
def example_five(n): | ||
i = 0 | ||
while i < n: | ||
do_something() | ||
i *= 2 | ||
|
||
|
||
# what is the runtime for example 6? | ||
# runtime is O(1) | ||
def example_six(n): | ||
for i in range(10): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 7? | ||
# runtime is O(2**n) | ||
def example_seven(n): | ||
for i in range(2**n): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 8? | ||
# runtime is O(n) | ||
def example_eight(n): | ||
for i in range(n): | ||
for x in range(7): | ||
do_something() | ||
|
||
|
||
# what is the runtime for example 9? | ||
# runtime is O(n^2) | ||
def example_nine(n): | ||
for i in range(n): | ||
example_one(n) | ||
|
||
|
||
# what is the runtime for example 10? | ||
# runtime is O(n) | ||
def example_ten(n): | ||
i = 0 | ||
while i < n: | ||
do_something() | ||
i += 2 |
Oops, something went wrong.