Skip to content

Instantly share code, notes, and snippets.

@namthatman
Created April 13, 2019 05:38
Show Gist options
  • Select an option

  • Save namthatman/d0703f05a01e1bbcd223dc47a9db2e44 to your computer and use it in GitHub Desktop.

Select an option

Save namthatman/d0703f05a01e1bbcd223dc47a9db2e44 to your computer and use it in GitHub Desktop.
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the jumpingOnClouds function below.
def jumpingOnClouds(c):
if c is None:
return 0
if c[0] == 1:
return 0
step = 0
nextcloud = 1
while nextcloud < len(c):
if c[nextcloud] == 0:
if nextcloud+1 < len(c) and c[nextcloud+1] == 0:
#case current,0,0,...
step += 1
nextcloud += 2
else:
#case here,0,1,...
step += 1
nextcloud += 1
else:
if nextcloud+1 < len(c) and c[nextcloud+1] == 0:
#case current,1,0,...
step += 1
nextcloud += 2
else:
#case current,1,1,...
return step
return step
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()
Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud. It is always possible to win the game.
For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided. For example, c = [0,1,0,0,0,1,0] indexed from 0...6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5. She could follow the following two paths: 0-2-4-6 or 0-2-3-4-6. The first path takes 3 jumps while the second takes 4.
Function Description
Complete the jumpingOnClouds function in the editor below. It should return the minimum number of jumps required, as an integer.
jumpingOnClouds has the following parameter(s):
c: an array of binary integers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment