CCDC style algorithm in Python