Skip to content

Commit

Permalink
Merge pull request #47 from prakashsellathurai/deepsource-transform-5…
Browse files Browse the repository at this point in the history
…f741440

Format code with black and autopep8
  • Loading branch information
prakashsellathurai authored Sep 22, 2021
2 parents 53262c5 + 8b72810 commit f688734
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 56 deletions.
45 changes: 27 additions & 18 deletions python/Recursion/celebrity-problem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##!/usr/bin/env python
# !/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Expand All @@ -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))

print(celeb(G))
72 changes: 34 additions & 38 deletions python/Recursion/top-sort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##!/usr/bin/env python
# !/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Expand All @@ -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))

0 comments on commit f688734

Please sign in to comment.