hackerrank jumping on the clouds

Solutions on MaxInterview for hackerrank jumping on the clouds by the best coders in the world

showing results for - "hackerrank jumping on the clouds"
Mohamed-Amine
08 Aug 2020
1# https://www.hackerrank.com/challenges/jumping-on-the-clouds
2# This is my solution to this fun challenge
3def jumpingOnClouds(c):
4    len_c = len(c)
5    counter = 0
6    result = 0
7    print('c[]:', c)
8    while counter < len_c:
9        
10        # for debugging purpose check these print statements
11        if counter + 2 < len_c:
12            print("current:", c[counter], "Two ahead:", c[counter+2], "counter:", counter,"result: ", result)
13        else:
14            print("current:", c[counter], "counter:", counter,"result: ", result)
15
16        # jump ahead as long as it's safe 2 steps ahead or unsafe 1 spot next    
17        if counter + 2 < len_c and c[counter+2] == 0:
18            counter = counter + 2
19            result = result + 1
20            continue
21        elif counter + 1 < len_c and c[counter+1] == 0:
22            counter = counter + 1
23            result = result + 1
24            continue
25        elif counter + 1 < len_c and c[counter+1] == 1:
26            counter = counter + 2
27            result = result + 1
28            continue
29
30        counter = counter + 1
31
32    print("result: ", result)
33    return result