Jumping on the Clouds: Revisited

  • + 0 comments
    # jumpingOnClouds function below in python.
    def jumpingOnClouds(c, k):
       
        energy = 100
        
        # got to next cloud by k steps
        i = 0 + k
        
        while energy > 0:
            '''The ternary operators are to check
            if its a thundercloud(c[i] == 1):
                then we decrement energy by 3(source to destination)
            else if its a clumbus (c[i] == 0):
                then we decrement energy by 1
            '''
            # prevent array out of index
            if i >= len(c):
                i -= len(c)
                
            if i == 0:
                # reached cloud zero, game stops
                energy = energy - 1 if c[i] == 0 else energy - 3
                break
            else:
                energy = energy - 1 if c[i] == 0 else energy - 3
                i += k
                
        return energy