Mercurial > public > think_complexity
view ch4ex1.py @ 24:5c2c4ce095ef
A stab at the L(p)/L(0) plot.
I still don't quite get how the graphs in the Watts and Strogatz paper were
generated. My results have basically the same shape, but don't converge to 0.
I'm not sure how this is possible if the rewire function does not remove edges.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 03 Jan 2013 18:41:13 -0600 |
parents | 84e183b40c63 |
children |
line wrap: on
line source
"""Section 4.2, exercise 1 of Allen Downey's Think Complexity book. "Write an implementation of a FIFO using either a doubly-linked list or a circular buffer." """ class Node(object): def __init__(self, data): self.prev = self.next = None self.data = data class Fifo(object): def __init__(self): self.first = None self.last = None def append(self, val): """Append a value onto the back of the queue.""" node = Node(val) if self.last: self.last.next = node node.prev = self.last self.last = node if not self.first: self.first = node def pop(self): """Remove and return the value at the front of the queue. Raises an IndexError if the queue is empty. """ if not self.first: raise IndexError node = self.first self.first = node.next node.prev = node.next = None if self.last is node: self.last = None return node.data def empty(self): """Returns True if the queue is empty and False otherwise.""" return self.first is None and self.last is None if __name__ == '__main__': import random values = range(100) random.shuffle(values) fifo = Fifo() assert fifo.empty() for v in values: fifo.append(v) got = [] while not fifo.empty(): got.append(fifo.pop()) assert got == values fifo.append(42) assert fifo.pop() == 42 assert fifo.empty() try: fifo.pop() except IndexError: pass else: assert False