This code will give the wrong answer when I tested it. But I cannot find which part is wrong. I am not very clear about how to use yield, maybe this is the reason. Someone, please help me. Any help will be appreciated.
def canCross(self, stones): def cross(stones, jump): if len(stones) == 2 and (jump - 1) <= stones - stones <= (jump + 1): yield True for i in range(1, len(stones)): if jump + 1 < stones[i] - stones: yield False elif jump - 1 <= stones[i] - stones <= jump + 1: yield cross(stones[1:], stones[i] - stones) elif i == len(stones) - 1 and jump - 1 > stones[i] - stones: yield False if len(stones) <= 2: return True jump = 1 return any(cross(stones, jump))