Skip to content

Commit

Permalink
PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
aryeguy committed Oct 2, 2018
1 parent 0b77c54 commit 9dc63f6
Show file tree
Hide file tree
Showing 51 changed files with 469 additions and 320 deletions.
2 changes: 1 addition & 1 deletion algorithmic-toolbox/1-intro-starter-files/fib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
def calc_fib(n):
a, b = 0, 1
for i in range(n):
a, b = b, a+b
a, b = b, a + b
return a


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def picasa_number_length(m):
def calc_fib(n):
a, b = 0, 1
for i in range(n):
a, b = b, a+b
a, b = b, a + b
return a


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def optimal_points(segments):
for i in range(len(segments)):
if (i == 0) or (segments[i][0] != 0 and segments[i][1] != 0):
points.append(segments[i][1])
for x in segments[i+1:]:
for x in segments[i + 1:]:
if x[0] <= segments[i][1] and segments[i][1] <= x[1]:
x[0] = 0
x[1] = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def optimal_summands(n):
summands = []
l = 1

while n > 2*l:
while n > 2 * l:
summands.append(l)
n -= l
l += 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Uses python3
# Uses python3
import sys


def min_dot_product(a, b):
a.sort()
b.sort(reverse=True)
Expand All @@ -9,6 +10,7 @@ def min_dot_product(a, b):
res += a[i] * b[i]
return res


if __name__ == '__main__':
input = sys.stdin.read()
data = list(map(int, input.split()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
def get_optimal_value(capacity, weights, values):
opt_value = 0
items = list(zip(values, weights))
items.sort(key=lambda item: item[0]/item[1], reverse=True)
items.sort(key=lambda item: item[0] / item[1], reverse=True)
values = [item[0] for item in items]
weights = [item[1] for item in items]

for value, weight in zip(values, weights):
if capacity == 0:
return opt_value
min_weight = min(weight, capacity)
opt_value += min_weight*(value/weight)
opt_value += min_weight * (value / weight)
weight -= min_weight
capacity -= min_weight

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Uses python3
import sys


def binary_search(a, x):
left, right = 0, len(a) - 1

while left <= right:
mid = (left+right) // 2
mid = (left + right) // 2
if a[mid] == x:
return mid
elif a[mid] < x:
Expand All @@ -21,7 +22,7 @@ def binary_search(a, x):
data = list(map(int, input.split()))
n = data[0]
m = data[n + 1]
a = data[1 : n + 1]
a = data[1: n + 1]
for x in data[n + 2:]:
# replace with the call to binary_search when implemented
print(binary_search(a, x), end = ' ')
print(binary_search(a, x), end=' ')
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Uses python3
import sys


def merge(a, b, count):
res = []
i = j = 0
Expand All @@ -16,17 +17,20 @@ def merge(a, b, count):
res.extend(b[j:])
return res


def merge_sort(a, count):
if len(a) < 2:
return a
mid = len(a) // 2
return merge(merge_sort(a[:mid], count), merge_sort(a[mid:], count), count)


def get_number_of_inversions(a):
count = [0]
sorted = merge_sort(a, count)
return count[0]


if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Uses python3
import sys


def fast_count_segments(starts, ends, points):
cnt = {}
segments_num = 0
Expand All @@ -21,13 +22,14 @@ def fast_count_segments(starts, ends, points):

return [cnt[x] for x in points]


if __name__ == '__main__':
input = sys.stdin.read()
data = list(map(int, input.split()))
n = data[0]
m = data[1]
starts = data[2:2 * n + 2:2]
ends = data[3:2 * n + 2:2]
ends = data[3:2 * n + 2:2]
points = data[2 * n + 2:]
# use fast_count_segments
cnt = fast_count_segments(starts, ends, points)
Expand Down
13 changes: 7 additions & 6 deletions algorithmic-toolbox/4-dynprog-starter-files/edit_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ def edit_distance(s, t):

for j in range(1, m):
for i in range(1, n):
insertion = D[i][j-1] + 1
deletion = D[i-1][j] + 1
mismatch = D[i-1][j-1] + 1
match = D[i-1][j-1]
if s[i-1] == t[j-1]:
insertion = D[i][j - 1] + 1
deletion = D[i - 1][j] + 1
mismatch = D[i - 1][j - 1] + 1
match = D[i - 1][j - 1]
if s[i - 1] == t[j - 1]:
D[i][j] = min(insertion, deletion, match)
else:
D[i][j] = min(insertion, deletion, mismatch)

return D[n-1][m-1]
return D[n - 1][m - 1]


if __name__ == "__main__":
print(edit_distance(input(), input()))
15 changes: 9 additions & 6 deletions algorithmic-toolbox/4-dynprog-starter-files/knapsack.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Uses python3
import sys


def optimal_weight(W, w):
res = [[0 for x in range(W+1)] for x in range(len(w)+1)]
res = [[0 for x in range(W + 1)] for x in range(len(w) + 1)]

for i in range(0, len(w)+1):
for j in range(0, W+1):
for i in range(0, len(w) + 1):
for j in range(0, W + 1):
if i == 0 or j == 0:
res[i][j] = 0
elif w[i-1] > j:
res[i][j] = res[i-1][j]
elif w[i - 1] > j:
res[i][j] = res[i - 1][j]
else:
res[i][j] = max(w[i-1] + res[i-1][j-w[i-1]], res[i-1][j])
res[i][j] = max(w[i - 1] + res[i - 1]
[j - w[i - 1]], res[i - 1][j])

return res[len(w)][W]


if __name__ == '__main__':
input = sys.stdin.read()
W, n, *w = list(map(int, input.split()))
Expand Down
6 changes: 4 additions & 2 deletions algorithmic-toolbox/4-dynprog-starter-files/lcs3.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#Uses python3
# Uses python3

import sys


def lcs3(a, b, c):
#write your code here
# write your code here
return min(len(a), len(b), len(c))


if __name__ == '__main__':
input = sys.stdin.read()
data = list(map(int, input.split()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def MinAndMax(M, m, i, j, operators):
min_value = 10000000
max_value = -10000000
for k in range(i, j):
a = evalt(M[i][k], M[k+1][j], operators[k])
b = evalt(M[i][k], m[k+1][j], operators[k])
c = evalt(m[i][k], M[k+1][j], operators[k])
d = evalt(m[i][k], m[k+1][j], operators[k])
a = evalt(M[i][k], M[k + 1][j], operators[k])
b = evalt(M[i][k], m[k + 1][j], operators[k])
c = evalt(m[i][k], M[k + 1][j], operators[k])
d = evalt(m[i][k], m[k + 1][j], operators[k])
min_value = min(min_value, a, b, c, d)
max_value = max(max_value, a, b, c, d)
return min_value, max_value
Expand All @@ -33,12 +33,12 @@ def get_maximum_value(digits, operators):
M[i][i] = digits[i]

for s in range(0, n):
for i in range(0, n-s):
for i in range(0, n - s):
j = i + s
if i != j:
m[i][j], M[i][j] = MinAndMax(M, m, i, j, operators)

return M[0][n-1]
return M[0][n - 1]


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# Uses python3
import sys


def optimal_sequence(n):
sequence = []

arr = [0]*(n+1)
for i in range(1, n+1):
arr[i] = arr[i-1] + 1;
arr = [0] * (n + 1)
for i in range(1, n + 1):
arr[i] = arr[i - 1] + 1
if i % 2 == 0:
arr[i] = min(arr[i//2]+1, arr[i])
arr[i] = min(arr[i // 2] + 1, arr[i])
if i % 3 == 0:
arr[i] = min(arr[i//3]+1, arr[i])
arr[i] = min(arr[i // 3] + 1, arr[i])

while n >= 1:
sequence.append(n)
if arr[n] == arr[n-1] + 1:
if arr[n] == arr[n - 1] + 1:
n = n - 1
elif n % 2 == 0 and arr[n//2] == arr[n] - 1:
elif n % 2 == 0 and arr[n // 2] == arr[n] - 1:
n = n // 2
elif n % 3 == 0 and arr[n//3] == arr[n] - 1:
elif n % 3 == 0 and arr[n // 3] == arr[n] - 1:
n = n // 3

return reversed(sequence)


input = sys.stdin.read()
n = int(input)
sequence = list(optimal_sequence(n))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

class DistPreprocessLarge:
def __init__(self, n, adj, cost):
# See description of these parameters in the starter for friend_suggestion
# See description of these parameters in the starter for
# friend_suggestion
self.n = n
self.INFINITY = n * maxlen
self.adj = adj
Expand Down Expand Up @@ -54,15 +55,17 @@ def shortcut(self, v):
neighbors = 0
shortcut_cover = 0
level = 0
# Compute correctly the values for the above heuristics before computing the node importance
importance = (shortcut_count - len(self.adj[0][v]) - len(self.adj[1][v])) + neighbors + shortcut_cover + level
# Compute correctly the values for the above heuristics before
# computing the node importance
importance = (shortcut_count - len(self.adj[0][v]) - len(
self.adj[1][v])) + neighbors + shortcut_cover + level
return importance, shortcuts, level

# See description of this method in the starter for friend_suggestion
def clear():
for v in self.visited:
self.bidistance[0][v] = self.bidistance[1][v] = self.INFINITY
self.visited[v] = False;
self.visited[v] = False
del self.visited[:]

# See description of this method in the starter for friend_suggestion
Expand All @@ -86,20 +89,20 @@ def readl():


if __name__ == '__main__':
n,m = readl()
n, m = readl()
adj = [[[] for _ in range(n)], [[] for _ in range(n)]]
cost = [[[] for _ in range(n)], [[] for _ in range(n)]]
for e in range(m):
u,v,c = readl()
adj[0][u-1].append(v-1)
cost[0][u-1].append(c)
adj[1][v-1].append(u-1)
cost[1][v-1].append(c)
u, v, c = readl()
adj[0][u - 1].append(v - 1)
cost[0][u - 1].append(c)
adj[1][v - 1].append(u - 1)
cost[1][v - 1].append(c)

ch = DistPreprocessLarge(n, adj, cost)
print("Ready")
sys.stdout.flush()
t, = readl()
for i in range(t):
s, t = readl()
print(ch.query(s-1, t-1))
print(ch.query(s - 1, t - 1))
Loading

0 comments on commit 9dc63f6

Please sign in to comment.