-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWork.py
97 lines (74 loc) · 2.25 KB
/
Work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File: Work.py
# Description: This program calculates the minimum number of lines of code the user writes before
# having to drink a cup of coffee to write n lines before going to bed.
# We use linear and binary searches to find this number.
# Student Name: Nicholas Webb
# Student UT EID: nw6887
# Partner Name: EJ Porras
# Partner UT EID: ejp2488
# Course Name: CS 313E
# Unique Number: 51135
# Date Created: 2/28/2022
# Date Last Modified: 3/2/2022
import sys
import time
# Input: v an integer representing the minimum lines of code and
# k an integer representing the productivity factor
# Output: computes the sum of the series (v + v // k + v // k**2 + ...)
# returns the sum of the series
def sum_series (v, k):
total = 0
i = 0
term = v
while term > 0:
term = v//(k**i)
total += term
i+=1
return total
# Input: n an integer representing the total number of lines of code
# k an integer representing the productivity factor
# Output: returns v the minimum lines of code to write using linear search
def linear_search (n, k):
for i in range(1,n+1):
if sum_series(i,k) >= n:
return i
return 0
# Input: n an integer representing the total number of lines of code
# k an integer representing the productivity factor
# Output: returns v the minimum lines of code to write using binary search
def binary_search (n, k):
beg = 1
end = n
while (beg <= end):
mid = (end+beg)//2
if sum_series(mid, k) >=n and sum_series(mid-1, k) < n:
return mid
elif(sum_series(mid, k) >= n):
end = mid - 1
else:
beg = mid + 1
return 0
def main():
# read number of cases
line = sys.stdin.readline()
line = line.strip()
num_cases = int (line)
for i in range (num_cases):
line = sys.stdin.readline()
line = line.strip()
inp = line.split()
n = int(inp[0])
k = int(inp[1])
start = time.time()
print("Binary Search: " + str(binary_search(n, k)))
finish = time.time()
print("Time: " + str(finish - start))
print()
start = time.time()
print("Linear Search: " + str(linear_search(n, k)))
finish = time.time()
print("Time: " + str(finish - start))
print()
print()
if __name__ == "__main__":
main()