diff --git a/python/Recursion/celebrity-problem.py b/python/Recursion/celebrity-problem.py index 197a63b..0fea665 100644 --- a/python/Recursion/celebrity-problem.py +++ b/python/Recursion/celebrity-problem.py @@ -1,4 +1,4 @@ -##!/usr/bin/env python +# !/usr/bin/env python # -*- coding: utf-8 -*- """ @@ -12,33 +12,42 @@ from random import randrange + def celeb(G): n = len(G) - - u,v = 0,1 - for c in range(2,n+1): - if G[u][v]: u=c - else: v=c - - if u==n: c=v - else: c=u - + + u, v = 0, 1 + for c in range(2, n + 1): + if G[u][v]: + u = c + else: + v = c + + if u == n: + c = v + else: + c = u + for v in range(n): - if c==v: continue - if G[c][v]: break - if not G[v][c]: break + if c == v: + continue + if G[c][v]: + break + if not G[v][c]: + break else: return c return None + if __name__ == "__main__": n = 100 - G = [[randrange(2) for i in range(n)] for i in range(n)] - + G = [[randrange(2) for i in range(n)] for i in range(n)] + c = randrange(n) - + for i in range(n): G[i][c] = True G[c][i] = False - - print(celeb(G)) \ No newline at end of file + + print(celeb(G)) diff --git a/python/Recursion/top-sort.py b/python/Recursion/top-sort.py index d315de9..fd64db2 100644 --- a/python/Recursion/top-sort.py +++ b/python/Recursion/top-sort.py @@ -1,4 +1,4 @@ -##!/usr/bin/env python +# !/usr/bin/env python # -*- coding: utf-8 -*- """ @@ -13,75 +13,71 @@ from random import randrange from collections import defaultdict -def naive_topsort(G,S=None): - if S is None: S = set(G) - - if len(S) == 1: return list(S) - + +def naive_topsort(G, S=None): + if S is None: + S = set(G) + + if len(S) == 1: + return list(S) + v = S.pop() - - seq = naive_topsort(G,S) - + + seq = naive_topsort(G, S) + min_i = 0 - for i,u in enumerate(seq): - if v in G[u]: min_i = i+1 - seq.insert(min_i,v) + for i, u in enumerate(seq): + if v in G[u]: + min_i = i + 1 + seq.insert(min_i, v) return seq def topsort(G): - count = dict((u, 0) for u in G) # The in-degree for each node + count = dict((u, 0) for u in G) # The in-degree for each node for u in G: for v in G[u]: - count[v] += 1 # Count every in-edge - Q = [u for u in G if count[u] == 0] # Valid initial nodes - S = [] # The result - while Q: # While we have start nodes... - u = Q.pop() # Pick one - S.append(u) # Use it as first of the rest + count[v] += 1 # Count every in-edge + Q = [u for u in G if count[u] == 0] # Valid initial nodes + S = [] # The result + while Q: # While we have start nodes... + u = Q.pop() # Pick one + S.append(u) # Use it as first of the rest for v in G[u]: - count[v] -= 1 # "Uncount" its out-edges - if count[v] == 0: # New valid start nodes? - Q.append(v) # Deal with them next + count[v] -= 1 # "Uncount" its out-edges + if count[v] == 0: # New valid start nodes? + Q.append(v) # Deal with them next return S class randRangesparse(object): """docstring for randRangesparse.""" + def __init__(self, n): super(randRangesparse, self).__init__() self.n = n self.weights = self.regenerate_weights() def regenerate_weights(self): - return {i:self.n-i for i in range(self.n)} - + return {i: self.n - i for i in range(self.n)} + def get_random(self): coin = randrange(self.n) while self.weights[coin] != 0: if self.weights[coin] != 0: - self.weights[coin]-=1 + self.weights[coin] -= 1 return coin else: - coin = randrange(self.n-1) + coin = randrange(self.n - 1) self.weights = self.regenerate_weights() return self.get_random() - - - - if __name__ == "__main__": - - rng = randRangesparse(10) - G = [[rng.get_random() for _ in range(10)] for _ in range(10)] - - print(naive_topsort(G)) - + rng = randRangesparse(10) + G = [[rng.get_random() for _ in range(10)] for _ in range(10)] - - + print(naive_topsort(G))