jumpingOnClouds HackerRank Solution:Looking for jumpingOnClouds solution for Hackerrank problem? Get solution with source code and detailed explainer video

There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. The player must avoid the thunderheads. Determine the minimum number of jumps it will take to jump from the starting postion to the last cloud. It is always possible to win the game.
For each game, you will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided.
Go to problem statement

Explanation Video:


Youtube Channel partner: CodingCart
Mentor: Satyendra Jaiswal
Langauge: Python

Source Code:  for jumpingOnClouds function


# Complete the jumpingOnClouds function below.
def jumpingOnClouds(c):
    count=0
    i=0
    while i< len(c)-1:
        #for 2 jumps
        if i+2<len(c) and c[i+2]==0:
            i+=1
        i+=1
        count+=1
    return count            
       
coming soon…
coming soon…

Full Source Code:


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the jumpingOnClouds function below.
def jumpingOnClouds(c):
    count=0
    i=0
    while i< len(c)-1:
        #for 2 jumps
        if i+2<len(c) and c[i+2]==0:
            i+=1
        i+=1
        count+=1
    return count            
 
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    c = list(map(int, input().rstrip().split()))

    result = jumpingOnClouds(c)

    fptr.write(str(result) + '\n')

    fptr.close()


coming soon…
coming soon…

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our Youtube Channel.

This post is in partner with CodingCart.