Skip to content

Commit

Permalink
Merge pull request #170 from code4tomorrow/dsa-practice
Browse files Browse the repository at this point in the history
DSA Practice Problems
  • Loading branch information
hpanchal092 authored Feb 4, 2023
2 parents 86e6733 + 5499f49 commit 3e45bba
Show file tree
Hide file tree
Showing 44 changed files with 2,567 additions and 147 deletions.
1 change: 1 addition & 0 deletions 3_advanced/chapter17/solutions/min_superset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# of minimum size which is the superset of all the given sets.
# Implement the following method:


# superset calcuated using Principle of Inclusion and Exclusion
# sets: a vector containing 3 sets
def findMinSupersetLength(sets):
Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions dsa/chapter1/practice/time_complexity.py
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
74 changes: 74 additions & 0 deletions dsa/chapter1/practice/time_complexity_questions.py
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
99 changes: 99 additions & 0 deletions dsa/chapter1/solutions/time_complexity.py
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)
84 changes: 84 additions & 0 deletions dsa/chapter1/solutions/time_complexity_questions.py
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
Loading

0 comments on commit 3e45bba

Please sign in to comment.