Skip to content

Commit e4a5c04

Browse files
authored
Adding time.sleep for the printing
1 parent 188cfb3 commit e4a5c04

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

TheMasterTheorem

+41-28
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
import math
2+
import time
23

4+
def typeOn(str):
5+
#prints on the screen with a delay for the feeling of a typing animation
6+
for char in str:
7+
print(char, end="", flush=True)
8+
time.sleep(0.01) #the waiting time between each char
9+
310
def getRelation():
411
#The function gets from the user the a, b k and i parts of the recurrence relation.
512
#The function takes out a list that contains: a on its first element, b on its second element.
613
#f(n) is in format >> n^k ∙ (log(n))^i, so k on its third element and i on its fourth element.
714
#The fifth element is the full recurrence relation (for printing porpuses mainly).
815
relation = [0,0,0,0,0]
9-
print("This program gets variables of the T(n) relation and determines the asymptotic running time of the algorithm.")
10-
print("Your recurrence relation needs to be from the following form:\nT(n) = a∙T(n/b) + Θ(n^k∙(log n)^i).")
11-
print("Notes:\na >= 1\nb > 1\nk >= 0\ni >= 0")
12-
print("Please enter:")
16+
typeOn("This program gets variables of the T(n) relation and determines the asymptotic running time of the algorithm.\nYour recurrence relation needs to be from the following form:\nT(n) = a∙T(n/b) + Θ(n^k∙(log n)^i).\nNotes:\na >= 1\nb > 1\nk >= 0\ni >= 0\nPlease enter:\n")
1317
while True:
1418
#keeps getting input until all four inputs are valid according to instructions.
1519
try:
20+
time.sleep(0.5)
1621
relation[0] = float(input("a: "))
1722
relation[1] = float(input("b: "))
1823
relation[2] = float(input("k: "))
1924
relation[3] = float(input("i: "))
2025
#checking if one of the inputs is invalid
2126
if relation[0] < 1 or relation[1] <= 1 or relation[2] < 0 or relation[3] < 0:
22-
print("You've entered invalid inputs. Please make sure: a>=1, b>1, k>=0, i>=0.")
23-
print("Try again:\n")
27+
typeOn("\nYou've entered invalid inputs. Please make sure: a>=1, b>1, k>=0, i>=0.\nTry again:\n")
2428
continue
2529
break
2630
except: #in case the input is not valid.
27-
print("You've entered invalid inputs. Please make sure: a>=1, b>1, k>=0, i>=0.")
28-
print("Try again:\n")
31+
typeOn("\nYou've entered invalid inputs. Please make sure: a>=1, b>1, k>=0, i>=0.\nTry again:\n")
32+
2933

3034
#changing floats to ints if possible.
3135
if relation[0].is_integer():
@@ -67,7 +71,8 @@ def firstCase(relation):
6771
#2: f(n) is a logarithmic function
6872
#3: f(n) is a combination of polynomial and logarithmic function
6973
log_b_a = math.log(relation[0]) / math.log(relation[1])
70-
print(".\n.\n.\nTrying to solve with the first case")
74+
typeOn("\n.\n.\n.\nTrying to solve with the first case")
75+
time.sleep(0.5)
7176
if relation[2] != 0 and relation[3] == 0: #option 1: polynomial function
7277
#two options:
7378
#1: if log b (a) <= power exponent of n, solving with the first case is not possible.
@@ -116,12 +121,17 @@ def firstCase(relation):
116121
log_b_a = "{:.2f}".format(log_b_a)
117122

118123
if solution:
119-
print("It is possible to solve the relation with the first case.")
120-
print("It can be solved with ε =",epsilon)
124+
typeOn("\nIt is possible to solve the relation with the first case.")
125+
typeOn("\nIt can be solved with ε = " + str(epsilon))
126+
solution = "Θ(n"
127+
if float(log_b_a) != 1:
128+
solution += "^"+str(log_b_a)
129+
solution += ")"
130+
return solution
121131
return "Θ(n^" + str(log_b_a) + ")"
122132
else:
123-
print("It is impossible to solve with the first case,")
124-
print("since there is no ε>0 so the equation: f(n) = O(n^(log ",relation[1],"(",relation[0],")-ε)) is True.",sep='')
133+
typeOn("\nIt is impossible to solve with the first case,")
134+
typeOn("\nsince there is no ε>0 so the equation: f(n) = O(n^(log " + str(relation[1]) + "(" + str(relation[0]) + ")-ε)) is True.")
125135
return False
126136

127137
def secondCase(relation):
@@ -132,7 +142,8 @@ def secondCase(relation):
132142
#2: f(n) is a logarithmic function
133143
#3: f(n) is a combination of polynomial and logarithmic function
134144
log_b_a = math.log(relation[0]) / math.log(relation[1])
135-
print(".\n.\n.\nTrying to solve with the second case")
145+
typeOn("\n.\n.\n.\nTrying to solve with the second case")
146+
time.sleep(0.5)
136147
if relation[2] != 0 and relation[3] == 0: #option 1: polynomial function
137148
#two options:
138149
#1: if log b (a) != power exponent of n, solving with the second case is not possible.
@@ -152,14 +163,14 @@ def secondCase(relation):
152163
log_b_a = "{:.2f}".format(log_b_a)
153164

154165
if solution:
155-
print("It is possible to solve the relation with the second case.")
166+
typeOn("\nIt is possible to solve the relation with the second case.")
156167
solution = "Θ(n"
157168
if float(log_b_a) != 1:
158169
solution += "^"+str(log_b_a)
159170
solution += "∙log n)"
160171
return solution
161172
else:
162-
print("It is impossible to solve with the second case.")
173+
typeOn("\nIt is impossible to solve with the second case.")
163174
return False
164175

165176
def thirdCase(relation):
@@ -170,7 +181,8 @@ def thirdCase(relation):
170181
#2: f(n) is a logarithmic function
171182
#3: f(n) is a combination of polynomial and logarithmic function
172183
log_b_a = math.log(relation[0]) / math.log(relation[1])
173-
print(".\n.\n.\nTrying to solve with the third case")
184+
typeOn("\n.\n.\n.\nTrying to solve with the third case")
185+
time.sleep(0.5)
174186
#first condition of the third case
175187
if relation[2] != 0 and relation[3] == 0: #option 1: polynomial function
176188
#two options:
@@ -217,32 +229,33 @@ def thirdCase(relation):
217229
log_b_a = "{:.2f}".format(log_b_a)
218230

219231
if solution:
220-
print("It is possible to solve the relation with the third case.")
221-
print("It can be solved with ε =",epsilon,"and c =",c)
232+
typeOn("\nIt is possible to solve the relation with the third case.")
233+
typeOn("\nIt can be solved with ε = " + str(epsilon) + " and c = " + str(c))
222234
solution = (relation[4])[relation[4].index('Θ'):]
223235
return solution
224236
else:
225-
print("It is impossible to solve with the third case,")
226-
print("since there are no ε>0 and c<1 that fit on this relation")
237+
typeOn("\nIt is impossible to solve with the third case,")
238+
typeOn("\nsince there are no ε>0 and c<1 that fit on this relation")
227239
return False
228240

229241
def startAgain():
230242
#a function for letting the user to start again the program.
231-
choice = input("\nStart again? (y/n): ")
243+
choice = input("\n\nStart again? (y/n): ")
232244
choice = choice.lower()
233245
if choice == 'y':
234246
main()
235247
elif choice == 'n':
236248
return
237249
else:
238-
print("Type y or n only please.")
250+
typeOn("\nType y or n only please.")
239251
startAgain()
240252

241253
def main():
242254
relation = getRelation()
243255
flag = 0
244256
#start solving:
245-
print("Solving the recurrence relation:", relation[4])
257+
time.sleep(0.5)
258+
typeOn("\nSolving the recurrence relation: " + relation[4])
246259
solution = firstCase(relation)
247260
if solution != False:
248261
flag = 1 #solved by case number 1
@@ -258,12 +271,12 @@ def main():
258271
solution = thirdCase(relation)
259272
if solution != False:
260273
flag = 3 #solved by case number 3
261-
274+
275+
time.sleep(0.5)
262276
if flag == 0:
263-
print("\nThe relation: ", relation[4], "can't be solved using the Master Theorem.")
277+
typeOn("\n\nThe relation: " + relation[4] + " can't be solved using the Master Theorem.")
264278
else:
265-
print("The asymptotic running time of the algorithm represented by the relation", relation[4], "is:", solution)
279+
typeOn("\n\nThe asymptotic running time of the algorithm represented by the relation " + relation[4] + " is: " + solution)
266280
startAgain()
267-
print("Exit")
268281

269282
main()

0 commit comments

Comments
 (0)