Implementation of backtracking algorithms in Python