view ch3ex3.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 0f98bcb5bd3f
children cfb7f28678c7
line wrap: on
line source
"""Chaper 3.3, exercise 3.

Write a function called bisection that takes a sorted list and a target value
and returns the index of the value in the list if it's there, or None if it's
not.

"""

def bisection(a, item):
    """Returns the index of item in the sorted list a if it exists or None if
    not.

    Finds the "rightmost" item if it exists.

    """
    lo = 0
    hi = len(a)

    while lo < hi:
        mid = (lo + hi) // 2
        if item < a[mid]:
            hi = mid
        else:
            lo = mid + 1

    n = lo - 1
    return a[n] if n >= 0 and a[n] == item else None