This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MarkdownParser(object): | |
| def convert_to_paragraph(self, mes): | |
| return '<p>{mes}</p>'.format(mes = mes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| this code is originally from http://greenteapress.com/complexity/Graph.py | |
| modified original code a bit | |
| original author: Allen B.Downey | |
| Copyright 2011 Allen B. Downey. | |
| Distributed under the GNU General Public License at gnu.org/licenses/gpl.html. | |
| """ | |
| class Vertex(object): | |
| """A Vertex is a node in a graph.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from Graph import ( | |
| Graph, | |
| Edge, | |
| Vertex, | |
| ) | |
| import random | |
| class RandomGraph(Graph): | |
| """ | |
| create a random graph with the probability (p) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Counter(object): | |
| """a counter iterator that counts a number | |
| when you invoke the (next) method""" | |
| def __init__(self): | |
| self.count = 0 | |
| def __iter__(self): | |
| return self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bisect | |
| def bisection(t, target): | |
| """ | |
| takes a sorted list (t) and the (target) value. | |
| returns the index of the (target) value in the list (t) if there | |
| is ,otherwise returns None | |
| """ | |
| i = bisect.bisect_left(t, target) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LinearMap(object): | |
| def __init__(self): | |
| self.items = [] | |
| def add(self, k, v): | |
| self.items.append((k, v)) | |
| def get(self, k): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DictFifo(object): | |
| def __init__(self): | |
| self.nextin = 0 | |
| self.nextout = 0 | |
| self.data = {} | |
| def append(self, value): | |
| self.data[self.nextin] = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| RandomGraph(Graph): | |
| def bfs(self, v): | |
| """search graph by a BFS: breadth-first-search""" | |
| worklist = [v] | |
| closed = [] | |
| while worklist: # order of growth is O(n) | |
| visited = worklist.pop(0) # O(n) | |
| closed.append(visited) # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from RandomGraph import RandomGraph,show_graph | |
| from Graph import * | |
| import string | |
| from random import random, choice | |
| class SmallWorldGraph(RandomGraph): | |
| def rewire(self, p): | |
| vs = self.vertices() | |
| for i, v in enumerate(vs): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from RandomGraph import RandomGraph,show_graph | |
| from Graph import * | |
| from random import random, randint, choice | |
| from collections import deque | |
| class SmallWorldGraph(RandomGraph): | |
| def rewire(self, p): | |
| vs = self.vertices() |
OlderNewer