Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I need to make a function to return an exhibit (array) with the quantity of steps it will take to complete another cluster yet with a little condition that I need to make my steps on 0 just and that means in the event that I have an array c = [0,0,0,1,0,0,1,0] it will accept the initial three 0 as one stage however on the off chance that I have only one 0 as you find toward the finish of the array it will be a stage so for this array it will find 4 ways to complete it (0,0,0)(0)(0)(0) as you see it will disregard

var array = [0, 0, 0, 1, 0, 0, 1, 0]

var stepsArray = [];

function jumpingOnClouds(c) {

  for (var i = 0; i < c.length; i++) {

    if (c[i] === 0) {

      if (c[i] === c[i + 1])

        stepsArray.push(c[i + 1])

    } else {

      stepsArray.push(c[i])

    }

  }

  return stepsArray.length

}

var result = jumpingOnClouds(array);

console.log(result);

I also tried the below code, but still, it gives the error

   var array = [0, 0, 0, 1, 0, 0]

   var stepsArray = [];

   function jumpingOnClouds(c) {

    for(var i = 0; i < c.length - 1; i++){

        if (c[i] === 0) {

            console.log(c[i])

            if (c[i] === c[i + 1] && c[i + 1] === c[i + 2]) {

                stepsArray.push(c[i + 2])

            } else if (c[i] === c[i + 1]) {

                stepsArray.push(c[i + 1])

            } else {

                stepsArray.push(c[i])

            }

        }

    }

    return stepsArray.length

}

var result = jumpingOnClouds(array);

console.log(result)

closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer
array = [0, 0, 0, 1, 0, 0, 1, 0]

stepsArray = []

def jumpingOnClouds(c):

    for i in range(len(c)):

        if c[i] == 0:

            if c[i] == c[i + 1]:

                stepsArray.append(c[i + 1])

        else:

            stepsArray.append(c[i])

    return len(stepsArray)

result = jumpingOnClouds(array)

print(result)

Alternatively:

array = [0, 0, 0, 1, 0, 0]

stepsArray = []

def jumpingOnClouds(c):

    for i in range(len(c) - 1):

        if c[i] == 0:

            if c[i] == c[i + 1] and c[i + 1] == c[i + 2]:

                stepsArray.append(c[i + 2])

            elif c[i] == c[i + 1]:

                stepsArray.append(c[i + 1])

            else:

                stepsArray.append(c[i])

    return len(stepsArray)

result = jumpingOnClouds(array)

print(result)

These modified versions of the code aim to determine the number of steps required to complete a new cluster based on specific conditions. The function jumpingOnClouds iterates through the array, considering the conditions for counting steps. The length of the stepsArray is returned as the final result.
0 votes
by (26.4k points)

Just Added 2 limitations on your answer for the index of I and eliminated undesirable conditions, Check the below code:

'use strict';

const fs = require('fs');

process.stdin.resume();

process.stdin.setEncoding('utf-8');

let inputString = '';

let currentLine = 0;

process.stdin.on('data', inputStdin => {

    inputString += inputStdin;

});

process.stdin.on('end', _ => {

    inputString = inputString.replace(/\s*$/, '')

        .split('\n')

        .map(str => str.replace(/\s*$/, ''));

    main();

});

function readLine() {

    return inputString[currentLine++];

}

// Complete the jumpingOnClouds function below.

function jumpingOnClouds(c) {

    var stepsArray = [];

   

    let i=0;

    while(i < c.length - 1){

       

        if ((i+2<c.length) && (c[i+2] === 0)) {

            stepsArray.push(c[i + 2]);

            i+=2;

        } else{

            stepsArray.push(c[i + 1]);

            i+=1;

        } 

        

    }

    return stepsArray.length

}

function main() {

    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);

    const c = readLine().split(' ').map(cTemp => parseInt(cTemp, 10));

    let result = jumpingOnClouds(c);

    ws.write(result + "\n");

    ws.end();

}

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

0 votes
by (25.7k points)
array = [0, 0, 0, 1, 0, 0, 1, 0]

stepsArray = []

def jumpingOnClouds(c):

    for i in range(len(c)):

        if c[i] == 0:

            if c[i] == c[i + 1]:

                stepsArray.append(c[i + 1])

        else:

            stepsArray.append(c[i])

    return len(stepsArray)

result = jumpingOnClouds(array)

print(result)

Alternatively:

array = [0, 0, 0, 1, 0, 0]

stepsArray = []

def jumpingOnClouds(c):

    for i in range(len(c) - 1):

        if c[i] == 0:

            if c[i] == c[i + 1] and c[i + 1] == c[i + 2]:

                stepsArray.append(c[i + 2])

            elif c[i] == c[i + 1]:

                stepsArray.append(c[i + 1])

            else:

                stepsArray.append(c[i])

    return len(stepsArray)

result = jumpingOnClouds(array)

print(result)

Both versions of the code aim to count the number of steps it takes to complete a new cluster based on the given conditions. The function jumpingOnClouds iterates through the array, considering the specific conditions for counting steps. The final result is the length of the stepsArray.
0 votes
by (19k points)
array = [0, 0, 0, 1, 0, 0, 1, 0]

stepsArray = []

def jumpingOnClouds(c):

    for i in range(len(c)):

        if c[i] == 0:

            if c[i] == c[i + 1]:

                stepsArray.append(c[i + 1])

        else:

            stepsArray.append(c[i])

    return len(stepsArray)

result = jumpingOnClouds(array)

print(result)

Alternatively:

array = [0, 0, 0, 1, 0, 0]

stepsArray = []

def jumpingOnClouds(c):

    for i in range(len(c) - 1):

        if c[i] == 0:

            if c[i] == c[i + 1] and c[i + 1] == c[i + 2]:

                stepsArray.append(c[i + 2])

            elif c[i] == c[i + 1]:

                stepsArray.append(c[i + 1])

            else:

                stepsArray.append(c[i])

    return len(stepsArray)

result = jumpingOnClouds(array)

print(result)

These simplified versions of the code aim to count the number of steps needed to complete a new cluster, while considering the given conditions. The jumpingOnClouds function iterates through the array, checking the conditions for counting steps. The length of the stepsArray is returned as the final result.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
4 answers

Browse Categories

...