Fish codility excercise

Related searches

Trying to solve this challenge on codility fish challenge I cannot understand why not all the tests are passed by my code.

function solution($A, $B) {
  // write your code in PHP7.0
  $stack =[];

  foreach($A as $key =>$value) {
    if(empty($stack)){
      array_push($stack,$key);
    }
    else if($B[count($stack)-1] == 1 && $B[$key]==0 )
    {
      if($value > $A[count($stack)-1])
      {
        array_pop($stack);
        array_push($stack,$key);
      }
    }
    else array_push($stack,$key);
  }
  return count($stack);
}

There are two problems with your code.

  1. The code does not reference items on the stack correctly. Use $B[$stack[count($stack)-1]] instead of $B[count($stack)-1]. Use $A[$stack[count($stack)-1]] not $A[count($stack)-1].

  2. Fish going upstream must fight every fish coming downstream, not just the first one that they meet.

The following is a successful solution:

function solution($A, $B) {
  // write your code in PHP7.0
  $stack = [];
  $key = 0;
  while($key < count($A)) {
    if(empty($stack)){
      array_push($stack,$key);
      $key++;
    }
    else if($B[$stack[count($stack)-1]] == 1 && $B[$key] == 0){
      if($A[$key] > $A[$stack[count($stack)-1]])
      {
        // fish going upstream eats fish going downstream
        array_pop($stack);
      } else {
        // fish going downstream eats fish going upstream
        $key++;
      }
    }
    else {
      array_push($stack,$key);
      $key++;
    }
  }
  return count($stack);
}

Fish coding task - Learn to Code, N voracious fish are moving along a river. Calculate how many fish are alive. Programming language: C, C++, C#, Go, Java 11, Java 8, JavaScript, Kotlin, Lua � Since, what I understand is that the first fish will never “see” the second one…”If P and Q are two fish and P If A[0] = 1 and B[0] = 0 and A[1] = 2 and B[1] = 0 they will not meet since are flowing the same direction and if B[1] = 1, they will not meet since P < Q (0 A[1] is ahead of A[0] so they will be separating more and more and

Python solution with 100% score

def solution(A, B):
    ds = []
    up = 0

    for i in range(len(A)):
        if B[i] == 1:
            ds.append(A[i])
        if B[i] == 0:
            if len(ds) == 0:
                up += 1
                continue
            while (len(ds) > 0):
                ele = ds.pop()
                if ele < A[i]:
                    continue
                else:
                    ds.append(ele)
                    break
            if len(ds) == 0:
                up += 1
    return len(ds) + up

Fish - Codility, https://github.com/yiqin/Codility-Practice/blob/master/Fish.java public int solution( int[] A, int[] B) { Stack<Integer> stackForOne = new� This is the task's description. My code gets 100% task score. Is it good code yet? (I think I'm getting a lot better, since I started solving this codility's exercises and posting here, but I always get important things to improve -which help me A LOT!- from reviewers) N voracious fish are moving along a river. Calculate how many fish are alive.

Try this one:

  function solution($A, $B) {
  // write your code in PHP7.0
  $stack =[];

  foreach($A as $key =>$value) {
    if(empty($stack)){
      array_push($stack,$key);
    }
    else if($B[count($stack)-1] == 1 && $B[$key]==0 )
    {
        while(true) {
          if($value > $A[count($stack)-1] && !empty($stack) && $B[count($stack)-1] == 1)
          {
            array_pop($stack);
          }
          else break;
        }
      array_push($stack,$key);
    }
    else array_push($stack,$key);
  }
  return count($stack);

}

Codility excercise: Fish, Task:N voracious fish are moving along a river. Calculate how many fish are alive . Here is description of exercise.. I use javascript, My code is� Become a better programmer. Develop your coding skills with our programming lessons. Take part in our programming challenges.

Codility Fish (queue) programming challenge, Today I have attempted a Codility exercise "Fish" (https://app.codility.com/ Arrays A and B represent N voracious fish in a river, ordered� Java solution to Codility Fish problem (Lesson 7 – Stacks and Queues) which scored 100%. The problem is to determine how many fish are alive in a river of fish moving upstream and downstream. The strategy is to keep a running stack of fish as go through array of fish swimming in same direction or upstream …

how to get 100% score on the Fish exercise on Codility – Straight , score javascript solution to the second exercise of the Stacks & Queues lesson named Fish. Now here's the exercise's description on Codility:. Prepare for tech interviews and develop your coding skills with our hands-on programming lessons. Become a strong tech candidate online using Codility!

The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q. Initially, each fish has a unique position. Fish number P is represented by A[P] and B[P]. Array A contains the sizes of the fish. All its elements are unique. Array B contains the directions of the fish.

Comments
  • Why you don't push an element if the stack is empty?
  • I am pushing one element if stack is empty
  • Ok. This condition else if($B[count($stack)-1] == 1 && $B[$key]==0 ). You only remove one element from stack, write?
  • But you need to remove all items which are smaller then $value until it meets the item which goes down.