Rewrite my old cpp code in Python. Core logic not changed; Seperate language dependant away from main algorithm.