Modified Anchor algorithm in Python