Skip to content

Commit

Permalink
PermCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianiacobghiula committed Jun 3, 2020
1 parent 12180ad commit 3a4953b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/codility/p04_counting_elements/PermCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package codility.p04_counting_elements;

import java.util.Arrays;
/*
https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/
*/
public class PermCheck {
//https://app.codility.com/demo/results/trainingERTVB8-SXV/
public int solution(int[] a) {
Arrays.sort(a);
int i = 0;
while (i < a.length && a[i] == i + 1) {
i++;
}
return i < a.length ? 0 : 1;
}
}
28 changes: 28 additions & 0 deletions src/test/java/codility/p04_counting_elements/PermCheckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package codility.p04_counting_elements;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class PermCheckTest {
PermCheck permCheck;

@BeforeEach
public void init() {
permCheck = new PermCheck();
}

@Test
public void sample1() {
int solution = permCheck.solution(new int[]{4, 1, 3, 2});
assertThat(solution).isEqualTo(1);
}

@Test
public void sample2() {
int solution = permCheck.solution(new int[]{4, 1, 3});
assertThat(solution).isEqualTo(0);
}

}

0 comments on commit 3a4953b

Please sign in to comment.