[Algorithms] – Problem “Jumping on the Clouds” solved with go

Problem:

Screenshot 2019-04-11 at 11.57.48.png

Solution:

Following the code implemented to solve it.

package algoTest

func JumpingOnClouds(c []int32) int {
   jumps :=0
   i := 0;

   // input:= []int32{0, 0, 1, 0, 0, 1, 0}
   for {
      if i + 2 < len(c) && c[i + 2] == 0 {
         i += 2;
      } else if i + 1 < len(c) {
         i++;
      } else {
         break;
      }
      jumps++;
   }
   return jumps;
}

The following code is to test this implementation.

package algoTest

import (
   "github.com/stretchr/testify/assert"
   "testing"
)

func TestJumpingOnClouds(t *testing.T) {
   input:= []int32{0, 0, 1, 0, 0, 1, 0}
   got := JumpingOnClouds( input )
   assert.Equal(t,4, got )

   input2:= []int32{0, 0, 0, 1, 0, 0}
   got2 := JumpingOnClouds( input2 )
   assert.Equal(t,3, got2 )

   input3:= []int32{0, 0}
   got3 := JumpingOnClouds( input3 )
   assert.Equal(t,1, got3 )

   input4:= []int32{1, 0}
   got4 := JumpingOnClouds( input4 )
   assert.Equal(t,1, got4 )
}

This problem is from : https://www.hackerrank.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s