mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-19 11:38:05 +02:00
Adjust Python code to follow PEP8
This commit is contained in:
parent
b36776fc27
commit
de1ad26035
14 changed files with 349 additions and 311 deletions
|
@ -1,28 +1,29 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
@source: http://codehiker.wordpress.com/2012/04/06/kosarajus-scc/
|
||||
I made minor changs
|
||||
"""
|
||||
@source: http://codehiker.wordpress.com/2012/04/06/kosarajus-scc/
|
||||
I made minor changs
|
||||
"""
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(300000)
|
||||
|
||||
source = 'SCC.txt'
|
||||
N = 875714
|
||||
source = 'SCC.txt'
|
||||
N = 875714
|
||||
|
||||
#globals
|
||||
# globals
|
||||
visited = {}
|
||||
finish = {}
|
||||
leader = {}
|
||||
finish = {}
|
||||
leader = {}
|
||||
|
||||
def getG(source):
|
||||
|
||||
def get_g(source):
|
||||
""" Read the Graph from a textfile """
|
||||
G = {}
|
||||
G = {}
|
||||
Grev = {}
|
||||
for i in range(1,N+1):
|
||||
G[i] = []
|
||||
for i in range(1, N+1):
|
||||
G[i] = []
|
||||
Grev[i] = []
|
||||
fin = open(source)
|
||||
for line in fin:
|
||||
|
@ -33,56 +34,61 @@ def getG(source):
|
|||
fin.close()
|
||||
return G, Grev
|
||||
|
||||
|
||||
def init():
|
||||
for i in range(1,N+1):
|
||||
visited[i] = 0
|
||||
finish[i] = 0
|
||||
leader[i] = 0
|
||||
for i in range(1, N+1):
|
||||
visited[i] = 0
|
||||
finish[i] = 0
|
||||
leader[i] = 0
|
||||
|
||||
|
||||
def dfs(G, i):
|
||||
global t
|
||||
visited[i] = 1
|
||||
leader[i] = s
|
||||
visited[i] = 1
|
||||
leader[i] = s
|
||||
for j in G[i]:
|
||||
if(visited[j]==0): dfs(G,j)
|
||||
if(visited[j] == 0):
|
||||
dfs(G, j)
|
||||
t = t + 1
|
||||
finish[i] = t
|
||||
|
||||
|
||||
def dfs_loop(G):
|
||||
global t
|
||||
global s
|
||||
t = 0 #number of nodes processed so far
|
||||
s = 0 #current source vertex
|
||||
t = 0 # number of nodes processed so far
|
||||
s = 0 # current source vertex
|
||||
i = N
|
||||
while(i>0):
|
||||
if(visited[i]==0):
|
||||
s=i
|
||||
dfs(G,i)
|
||||
i=i-1
|
||||
while(i > 0):
|
||||
if(visited[i] == 0):
|
||||
s = i
|
||||
dfs(G, i)
|
||||
i = i-1
|
||||
|
||||
if __name__ == "__main__":
|
||||
init()
|
||||
g, grev=getG()
|
||||
dfs_loop(grev) #THE FIRST LOOP ON REVERSE GRAPH
|
||||
g, grev = get_g()
|
||||
dfs_loop(grev) # THE FIRST LOOP ON REVERSE GRAPH
|
||||
|
||||
# construct new graph
|
||||
newGraph = {}
|
||||
for i in range(1, N+1):
|
||||
temp = []
|
||||
for x in g[i]: temp.append(finish[x])
|
||||
for x in g[i]:
|
||||
temp.append(finish[x])
|
||||
newGraph[finish[i]] = temp
|
||||
|
||||
init()
|
||||
dfs_loop(newGraph) #THE SECOND LOOP
|
||||
dfs_loop(newGraph) # THE SECOND LOOP
|
||||
|
||||
# statistics
|
||||
lst = sorted(leader.values())
|
||||
lst = sorted(leader.values())
|
||||
stat = []
|
||||
pre = 0
|
||||
for i in range(0,N-1):
|
||||
pre = 0
|
||||
for i in range(0, N-1):
|
||||
if lst[i] != lst[i+1]:
|
||||
stat.append(i + 1 - pre)
|
||||
pre=i+1
|
||||
pre = i+1
|
||||
stat.append(N-pre)
|
||||
L = sorted(stat)
|
||||
L.reverse()
|
||||
|
|
|
@ -1,93 +1,98 @@
|
|||
def strongly_connected_components(graph):
|
||||
"""
|
||||
Tarjan's Algorithm (named for its discoverer, Robert Tarjan) is a
|
||||
graph theory algorithm for finding the strongly connected
|
||||
components of a graph.
|
||||
|
||||
Based on: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
|
||||
@author: Dries Verdegem, some minor edits by Martin Thoma
|
||||
@source: http://www.logarithmic.net/pfh/blog/01208083168
|
||||
"""
|
||||
"""
|
||||
Tarjan's Algorithm (named for its discoverer, Robert Tarjan) is a
|
||||
graph theory algorithm for finding the strongly connected
|
||||
components of a graph.
|
||||
|
||||
index_counter = 0
|
||||
stack = []
|
||||
lowlinks = {}
|
||||
index = {}
|
||||
result = []
|
||||
|
||||
def strongconnect(node, index_counter):
|
||||
print("Start with node: %s###########################" % node)
|
||||
# set the depth index for this node to the smallest unused index
|
||||
print("lowlinks:\t%s" % lowlinks)
|
||||
print("index:\t%s" % index)
|
||||
print("stack:\t%s" % stack)
|
||||
Based on: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
|
||||
@author: Dries Verdegem, some minor edits by Martin Thoma
|
||||
@source: http://www.logarithmic.net/pfh/blog/01208083168
|
||||
"""
|
||||
|
||||
index[node] = index_counter
|
||||
lowlinks[node] = index_counter
|
||||
index_counter += 1
|
||||
stack.append(node)
|
||||
|
||||
# Consider successors of `node`
|
||||
try:
|
||||
successors = graph[node]
|
||||
except:
|
||||
successors = []
|
||||
index_counter = 0
|
||||
stack = []
|
||||
lowlinks = {}
|
||||
index = {}
|
||||
result = []
|
||||
|
||||
# Depth first search
|
||||
for successor in successors:
|
||||
# Does the current node point to a node that was already
|
||||
# visited?
|
||||
if successor not in lowlinks:
|
||||
print("successor not in lowlinks: %s -> %s (node, successor)" % (node, successor))
|
||||
# Successor has not yet been visited; recurse on it
|
||||
strongconnect(successor, index_counter)
|
||||
lowlinks[node] = min(lowlinks[node],lowlinks[successor])
|
||||
elif successor in stack:
|
||||
#else:
|
||||
print("successor in stack: %s -> %s" % (node, successor));
|
||||
# the successor is in the stack and hence in the
|
||||
# current strongly connected component (SCC)
|
||||
lowlinks[node] = min(lowlinks[node],index[successor])
|
||||
else:
|
||||
print("This happens sometimes. node: %s, successor: %s" % (node, successor))
|
||||
print("Lowlinks: %s" %lowlinks)
|
||||
print("stack: %s" % stack)
|
||||
|
||||
# If `node` is a root node, pop the stack and generate an SCC
|
||||
if lowlinks[node] == index[node]:
|
||||
print("Got root node: %s (index/lowlink: %i)" % (node, lowlinks[node]))
|
||||
connected_component = []
|
||||
|
||||
while True:
|
||||
successor = stack.pop()
|
||||
print("pop: %s" % successor)
|
||||
connected_component.append(successor)
|
||||
if successor == node: break
|
||||
def strongconnect(node, index_counter):
|
||||
print("Start with node: %s###########################" % node)
|
||||
# set the depth index for this node to the smallest unused index
|
||||
print("lowlinks:\t%s" % lowlinks)
|
||||
print("index:\t%s" % index)
|
||||
print("stack:\t%s" % stack)
|
||||
|
||||
component = tuple(connected_component)
|
||||
index[node] = index_counter
|
||||
lowlinks[node] = index_counter
|
||||
index_counter += 1
|
||||
stack.append(node)
|
||||
|
||||
# storing the result
|
||||
result.append(component)
|
||||
else:
|
||||
print("Node: %s, lowlink: %i, index: %i" % (node, lowlinks[node], index[node]))
|
||||
|
||||
for node in graph:
|
||||
if node not in lowlinks:
|
||||
strongconnect(node, index_counter)
|
||||
|
||||
return result
|
||||
# Consider successors of `node`
|
||||
try:
|
||||
successors = graph[node]
|
||||
except:
|
||||
successors = []
|
||||
|
||||
# Depth first search
|
||||
for successor in successors:
|
||||
# Does the current node point to a node that was already
|
||||
# visited?
|
||||
if successor not in lowlinks:
|
||||
print("successor not in lowlinks: %s -> %s (node, successor)" %
|
||||
(node, successor))
|
||||
# Successor has not yet been visited; recurse on it
|
||||
strongconnect(successor, index_counter)
|
||||
lowlinks[node] = min(lowlinks[node], lowlinks[successor])
|
||||
elif successor in stack:
|
||||
# else:
|
||||
print("successor in stack: %s -> %s" % (node, successor))
|
||||
# the successor is in the stack and hence in the
|
||||
# current strongly connected component (SCC)
|
||||
lowlinks[node] = min(lowlinks[node], index[successor])
|
||||
else:
|
||||
print("This happens sometimes. node: %s, successor: %s" %
|
||||
(node, successor))
|
||||
print("Lowlinks: %s" % lowlinks)
|
||||
print("stack: %s" % stack)
|
||||
|
||||
# If `node` is a root node, pop the stack and generate an SCC
|
||||
if lowlinks[node] == index[node]:
|
||||
print("Got root node: %s (index/lowlink: %i)" %
|
||||
(node, lowlinks[node]))
|
||||
connected_component = []
|
||||
|
||||
while True:
|
||||
successor = stack.pop()
|
||||
print("pop: %s" % successor)
|
||||
connected_component.append(successor)
|
||||
if successor == node:
|
||||
break
|
||||
|
||||
component = tuple(connected_component)
|
||||
|
||||
# storing the result
|
||||
result.append(component)
|
||||
else:
|
||||
print("Node: %s, lowlink: %i, index: %i" %
|
||||
(node, lowlinks[node], index[node]))
|
||||
|
||||
for node in graph:
|
||||
if node not in lowlinks:
|
||||
strongconnect(node, index_counter)
|
||||
|
||||
return result
|
||||
|
||||
graph = {'a': ['b'],
|
||||
'b': ['c'],
|
||||
'c': ['d', 'e'],
|
||||
'd': ['a', 'e', 'h'],
|
||||
'e': ['c', 'f'],
|
||||
'f': ['g', 'i'],
|
||||
'g': ['h', 'f'],
|
||||
'h': ['j'],
|
||||
'i': ['g', 'f'],
|
||||
'j': ['i'],
|
||||
'k': [],
|
||||
'h': []}
|
||||
'b': ['c'],
|
||||
'c': ['d', 'e'],
|
||||
'd': ['a', 'e', 'h'],
|
||||
'e': ['c', 'f'],
|
||||
'f': ['g', 'i'],
|
||||
'g': ['h', 'f'],
|
||||
'h': ['j'],
|
||||
'i': ['g', 'f'],
|
||||
'j': ['i'],
|
||||
'k': [],
|
||||
'h': []}
|
||||
|
||||
print strongly_connected_components(graph)
|
||||
|
|
|
@ -1,50 +1,51 @@
|
|||
def strongly_connected_components(graph):
|
||||
index_counter = 0
|
||||
stack = []
|
||||
lowlinks = {}
|
||||
index = {}
|
||||
result = []
|
||||
|
||||
def strongconnect(node, index_counter):
|
||||
index[node] = index_counter
|
||||
lowlinks[node] = index_counter
|
||||
index_counter += 1
|
||||
stack.append(node)
|
||||
|
||||
try:
|
||||
successors = graph[node]
|
||||
except:
|
||||
successors = []
|
||||
index_counter = 0
|
||||
stack = []
|
||||
lowlinks = {}
|
||||
index = {}
|
||||
result = []
|
||||
|
||||
# Depth first search
|
||||
for successor in successors:
|
||||
# Does the current node point to a node that was already
|
||||
# visited?
|
||||
if successor not in lowlinks:
|
||||
# Successor has not yet been visited; recurse on it
|
||||
strongconnect(successor, index_counter)
|
||||
lowlinks[node] = min(lowlinks[node],lowlinks[successor])
|
||||
elif successor in stack:
|
||||
# the successor is in the stack and hence in the
|
||||
# current strongly connected component (SCC)
|
||||
lowlinks[node] = min(lowlinks[node],index[successor])
|
||||
|
||||
# If `node` is a root node, pop the stack and generate an SCC
|
||||
if lowlinks[node] == index[node]:
|
||||
connected_component = []
|
||||
|
||||
while True:
|
||||
successor = stack.pop()
|
||||
connected_component.append(successor)
|
||||
if successor == node: break
|
||||
def strongconnect(node, index_counter):
|
||||
index[node] = index_counter
|
||||
lowlinks[node] = index_counter
|
||||
index_counter += 1
|
||||
stack.append(node)
|
||||
|
||||
component = tuple(connected_component)
|
||||
try:
|
||||
successors = graph[node]
|
||||
except:
|
||||
successors = []
|
||||
|
||||
# storing the result
|
||||
result.append(component)
|
||||
|
||||
for node in graph:
|
||||
if node not in lowlinks:
|
||||
strongconnect(node, index_counter)
|
||||
|
||||
return result
|
||||
# Depth first search
|
||||
for successor in successors:
|
||||
# Does the current node point to a node that was already
|
||||
# visited?
|
||||
if successor not in lowlinks:
|
||||
# Successor has not yet been visited; recurse on it
|
||||
strongconnect(successor, index_counter)
|
||||
lowlinks[node] = min(lowlinks[node], lowlinks[successor])
|
||||
elif successor in stack:
|
||||
# the successor is in the stack and hence in the
|
||||
# current strongly connected component (SCC)
|
||||
lowlinks[node] = min(lowlinks[node], index[successor])
|
||||
|
||||
# If `node` is a root node, pop the stack and generate an SCC
|
||||
if lowlinks[node] == index[node]:
|
||||
connected_component = []
|
||||
|
||||
while True:
|
||||
successor = stack.pop()
|
||||
connected_component.append(successor)
|
||||
if successor == node:
|
||||
break
|
||||
|
||||
component = tuple(connected_component)
|
||||
|
||||
# storing the result
|
||||
result.append(component)
|
||||
|
||||
for node in graph:
|
||||
if node not in lowlinks:
|
||||
strongconnect(node, index_counter)
|
||||
|
||||
return result
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#!/usr/bin/env python
|
||||
import datetime
|
||||
|
||||
tmpDay = datetime.date.today() # von
|
||||
lastday = datetime.date(2014,2,10) # bis (Vorlesungsende?)
|
||||
tmp_day = datetime.date.today() # von
|
||||
lastday = datetime.date(2014, 2, 10) # bis (Vorlesungsende?)
|
||||
|
||||
while tmpDay < lastday:
|
||||
if tmpDay.weekday() == 0:
|
||||
print tmpDay.strftime('%d.%m.%Y')
|
||||
tmpDay += datetime.timedelta(days=1)
|
||||
while tmp_day < lastday:
|
||||
if tmp_day.weekday() == 0:
|
||||
print tmp_day.strftime('%d.%m.%Y')
|
||||
tmp_day += datetime.timedelta(days=1)
|
||||
|
|
|
@ -1,40 +1,44 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
def printArr(a):
|
||||
|
||||
def print_arr(a):
|
||||
for line in a:
|
||||
print(line)
|
||||
|
||||
|
||||
def initialise(n):
|
||||
array = [[0 for j in xrange(0,n)] for i in xrange(0,n)]
|
||||
array = [[0 for j in xrange(0, n)] for i in xrange(0, n)]
|
||||
return array
|
||||
|
||||
def spiralFill(a):
|
||||
|
||||
def spiral_fill(a):
|
||||
n = len(a)
|
||||
x = y = n/2
|
||||
number = 1
|
||||
# r u l o
|
||||
order = [(1,0), (0,1), (-1,0), (0,-1)]
|
||||
iOrder = 0
|
||||
# r u l o
|
||||
order = [(1, 0), (0, 1), (-1, 0), (0, -1)]
|
||||
i_order = 0
|
||||
length = 1
|
||||
a[y][x] = number
|
||||
while not (x == (n-1) and y == 0):
|
||||
for j in xrange(0, length):
|
||||
xAdd, yAdd = order[iOrder]
|
||||
xAdd, yAdd = order[i_order]
|
||||
x += xAdd
|
||||
y += yAdd
|
||||
number += 1
|
||||
a[y][x] = number
|
||||
if x == (n-1) and y==0:
|
||||
if x == (n-1) and y == 0:
|
||||
break
|
||||
if iOrder == 1 or iOrder == 3:
|
||||
if i_order == 1 or i_order == 3:
|
||||
length += 1
|
||||
iOrder = (iOrder+1) % 4
|
||||
i_order = (i_order+1) % 4
|
||||
return a
|
||||
|
||||
def diagonalSum(a):
|
||||
|
||||
def diagonal_sum(a):
|
||||
n = len(a)
|
||||
sum = -1 # you will have the element in the middle (1) twice
|
||||
sum = -1 # you will have the element in the middle (1) twice
|
||||
for i in xrange(0, n):
|
||||
sum += a[i][i]
|
||||
sum += a[n-i-1][i]
|
||||
|
@ -42,15 +46,15 @@ def diagonalSum(a):
|
|||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(description="ProjectEuler: 28")
|
||||
parser.add_argument("-n", metavar='N', type=int,
|
||||
help="length of the spiral", required=True)
|
||||
parser.add_argument("-d", action="store_true",default=False,
|
||||
parser.add_argument("-d", action="store_true", default=False,
|
||||
help="display the spiral")
|
||||
args = parser.parse_args()
|
||||
array = initialise(args.n)
|
||||
array = spiralFill(array)
|
||||
array = spiral_fill(array)
|
||||
if args.d:
|
||||
printArr(array)
|
||||
print diagonalSum(array)
|
||||
print_arr(array)
|
||||
print diagonal_sum(array)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue