From 225a49e7a23f374673fd6759ba0e9de3b38f2a3f Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 10:13:03 +0530 Subject: [PATCH 01/14] modify code to make use of java Optional class --- .../com/thealgorithms/others/BoyerMoore.java | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index d9d5b5d028ef..f6baf40cbef0 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -5,35 +5,54 @@ https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm */ package com.thealgorithms.others; +import java.util.Optional; public final class BoyerMoore { private BoyerMoore() { } - public static int findmajor(final int[] a) { + private static Optional findCandidate(final int[] a) { int count = 0; - int cand = -1; - for (final var k : a) { + int candidate = -1; + for (final int k : a) { if (count == 0) { - cand = k; + candidate = k; count = 1; } else { - if (k == cand) { + if (k == candidate) { count++; } else { count--; } } } - count = 0; - for (final var j : a) { - if (j == cand) { + return Optional.of(candidate); + } + + + public static Optional findmajor(final int[] a) { + Optional candidate = findCandidate(a); + + if (candidate.isPresent() && isMajority(candidate.get(), a)) { + return candidate; + } else { + return Optional.empty(); + } + } + + private static int calculateOccurrences(final int candidate, final int[] a) { + int count = 0; + for (final int j : a) { + if (j == candidate) { count++; } } - if (count > (a.length / 2)) { - return cand; - } - return -1; + return count; } + + private static boolean isMajority(final int candidate, final int[] a) { + int count = calculateOccurrences(candidate, a); + return count > (a.length / 2); + } + } From 353125d42827feabc6051cd730fad23313cf60b5 Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 10:27:03 +0530 Subject: [PATCH 02/14] revert changes --- .../com/thealgorithms/others/BoyerMoore.java | 43 ++++++------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index f6baf40cbef0..d9d5b5d028ef 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -5,54 +5,35 @@ https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm */ package com.thealgorithms.others; -import java.util.Optional; public final class BoyerMoore { private BoyerMoore() { } - private static Optional findCandidate(final int[] a) { + public static int findmajor(final int[] a) { int count = 0; - int candidate = -1; - for (final int k : a) { + int cand = -1; + for (final var k : a) { if (count == 0) { - candidate = k; + cand = k; count = 1; } else { - if (k == candidate) { + if (k == cand) { count++; } else { count--; } } } - return Optional.of(candidate); - } - - - public static Optional findmajor(final int[] a) { - Optional candidate = findCandidate(a); - - if (candidate.isPresent() && isMajority(candidate.get(), a)) { - return candidate; - } else { - return Optional.empty(); - } - } - - private static int calculateOccurrences(final int candidate, final int[] a) { - int count = 0; - for (final int j : a) { - if (j == candidate) { + count = 0; + for (final var j : a) { + if (j == cand) { count++; } } - return count; - } - - private static boolean isMajority(final int candidate, final int[] a) { - int count = calculateOccurrences(candidate, a); - return count > (a.length / 2); + if (count > (a.length / 2)) { + return cand; + } + return -1; } - } From 0527c590b1a6b4c3ba41a3cee9d174a5ab43da79 Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 10:30:34 +0530 Subject: [PATCH 03/14] add java.util.Optional --- src/main/java/com/thealgorithms/others/BoyerMoore.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index d9d5b5d028ef..ce039ebde8ef 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -5,6 +5,7 @@ https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm */ package com.thealgorithms.others; +import java.util.Optional; public final class BoyerMoore { private BoyerMoore() { @@ -34,6 +35,6 @@ public static int findmajor(final int[] a) { if (count > (a.length / 2)) { return cand; } - return -1; + return Optional.empty(); } } From 286c042bb03ac349038a56764e96763acb5b6efd Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 10:54:58 +0530 Subject: [PATCH 04/14] add java.util.Optional --- src/main/java/com/thealgorithms/others/BoyerMoore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index ce039ebde8ef..f49ad598af25 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -11,7 +11,7 @@ public final class BoyerMoore { private BoyerMoore() { } - public static int findmajor(final int[] a) { + public static Optional findmajor(final int[] a) { int count = 0; int cand = -1; for (final var k : a) { From fe687c37c5ddc0421865a3cfe8ccfca8d515127b Mon Sep 17 00:00:00 2001 From: vil02 Date: Tue, 31 Oct 2023 07:20:24 +0100 Subject: [PATCH 05/14] refactors: make `findmajor` return `optional` --- .../com/thealgorithms/others/BoyerMoore.java | 2 +- .../thealgorithms/others/BoyerMooreTest.java | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index f49ad598af25..e7ebc2de7d47 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -33,7 +33,7 @@ public static Optional findmajor(final int[] a) { } } if (count > (a.length / 2)) { - return cand; + return Optional.of(cand); } return Optional.empty(); } diff --git a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java index b614c14070bd..308568cc4342 100644 --- a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java +++ b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java @@ -11,12 +11,22 @@ public class BoyerMooreTest { @ParameterizedTest - @MethodSource("inputStream") - void numberTests(int expected, int[] input) { - Assertions.assertEquals(expected, BoyerMoore.findmajor(input)); + @MethodSource("inputStreamWithExistingMajority") + void checkWhenMajorityExists(int expected, int[] input) { + Assertions.assertEquals(expected, BoyerMoore.findmajor(input).get()); } - private static Stream inputStream() { - return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 2}), Arguments.of(10, new int[] {10, 10, 20}), Arguments.of(10, new int[] {10, 20, 10}), Arguments.of(10, new int[] {20, 10, 10}), Arguments.of(-1, new int[] {10, 10, 20, 20, 30, 30}), Arguments.of(4, new int[] {1, 4, 2, 4, 4, 5, 4})); + private static Stream inputStreamWithExistingMajority() { + return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 2}), Arguments.of(10, new int[] {10, 10, 20}), Arguments.of(10, new int[] {10, 20, 10}), Arguments.of(10, new int[] {20, 10, 10}), Arguments.of(4, new int[] {1, 4, 2, 4, 4, 5, 4})); + } + + @ParameterizedTest + @MethodSource("inputStreamWithoutMajority") + void checkWhenMajorityExists(int[] input) { + Assertions.assertFalse(BoyerMoore.findmajor(input).isPresent()); + } + + private static Stream inputStreamWithoutMajority() { + return Stream.of(Arguments.of(new int[] {10, 10, 20, 20, 30, 30}), Arguments.of(new int[] {10, 20, 30, 40, 50})); } } From 59bbeb1666b7359073dcf52a695f2fe61e4e8902 Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 12:08:44 +0530 Subject: [PATCH 06/14] refactors: make method name findMajor and split it --- .../com/thealgorithms/others/BoyerMoore.java | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index e7ebc2de7d47..9b798b097787 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -11,30 +11,45 @@ public final class BoyerMoore { private BoyerMoore() { } - public static Optional findmajor(final int[] a) { + public static Optional findMajor(final int[] a) { + int candidate = findCandidate(a); + int count = countOccurrences(a, candidate); + + if (count > (a.length / 2)) { + return Optional.of(candidate); + } + + return Optional.empty(); + } + + + + private static int findCandidate(final int[] a) { int count = 0; - int cand = -1; - for (final var k : a) { + int candidate = -1; + for (final int k : a) { if (count == 0) { - cand = k; + candidate = k; count = 1; } else { - if (k == cand) { + if (k == candidate) { count++; } else { count--; } } } - count = 0; - for (final var j : a) { - if (j == cand) { + return candidate; + } + + private static int countOccurrences(final int[] a, int candidate) { + int count = 0; + for (final int j : a) { + if (j == candidate) { count++; } } - if (count > (a.length / 2)) { - return Optional.of(cand); - } - return Optional.empty(); + return count; } + } From 199207dc4fcb6ec4ed6dc5df84c7e3a6a1ed7e09 Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 12:15:20 +0530 Subject: [PATCH 07/14] refactors: change method name in tests --- src/test/java/com/thealgorithms/others/BoyerMooreTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java index 308568cc4342..0f2297ec2d59 100644 --- a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java +++ b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java @@ -13,7 +13,7 @@ public class BoyerMooreTest { @ParameterizedTest @MethodSource("inputStreamWithExistingMajority") void checkWhenMajorityExists(int expected, int[] input) { - Assertions.assertEquals(expected, BoyerMoore.findmajor(input).get()); + Assertions.assertEquals(expected, BoyerMoore.findMajor(input).get()); } private static Stream inputStreamWithExistingMajority() { @@ -23,7 +23,7 @@ private static Stream inputStreamWithExistingMajority() { @ParameterizedTest @MethodSource("inputStreamWithoutMajority") void checkWhenMajorityExists(int[] input) { - Assertions.assertFalse(BoyerMoore.findmajor(input).isPresent()); + Assertions.assertFalse(BoyerMoore.findMajor(input).isPresent()); } private static Stream inputStreamWithoutMajority() { From 1c529e3af99f3792dcfa8f48ebd4d9b9d93dd68e Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 12:51:36 +0530 Subject: [PATCH 08/14] Apply suggestions from code review Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../java/com/thealgorithms/others/BoyerMoore.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index 9b798b097787..85893340dff5 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -12,18 +12,14 @@ private BoyerMoore() { } public static Optional findMajor(final int[] a) { - int candidate = findCandidate(a); - int count = countOccurrences(a, candidate); - + final var candidate = findCandidate(a); + final var count = countOccurrences(a, candidate); if (count > (a.length / 2)) { return Optional.of(candidate); } - return Optional.empty(); } - - private static int findCandidate(final int[] a) { int count = 0; int candidate = -1; @@ -42,9 +38,9 @@ private static int findCandidate(final int[] a) { return candidate; } - private static int countOccurrences(final int[] a, int candidate) { + private static int countOccurrences(final int candidate, final int[] a) { int count = 0; - for (final int j : a) { + for (final var j : a) { if (j == candidate) { count++; } From 8d17984ac57806005ad93a67c8fe8a0f90d26da7 Mon Sep 17 00:00:00 2001 From: Prathamesh Powar Date: Tue, 31 Oct 2023 12:58:44 +0530 Subject: [PATCH 09/14] change back to int --- src/main/java/com/thealgorithms/others/BoyerMoore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index 85893340dff5..2255fa41d9f2 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -40,7 +40,7 @@ private static int findCandidate(final int[] a) { private static int countOccurrences(final int candidate, final int[] a) { int count = 0; - for (final var j : a) { + for (final int j : a) { if (j == candidate) { count++; } From 33dd9ec369492976620f664eb790b238b19d840e Mon Sep 17 00:00:00 2001 From: vil02 Date: Tue, 31 Oct 2023 08:55:16 +0100 Subject: [PATCH 10/14] fix: swap arguments --- src/main/java/com/thealgorithms/others/BoyerMoore.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index 2255fa41d9f2..8412ec70c853 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -38,7 +38,7 @@ private static int findCandidate(final int[] a) { return candidate; } - private static int countOccurrences(final int candidate, final int[] a) { + private static int countOccurrences(final int[] a, final int candidate) { int count = 0; for (final int j : a) { if (j == candidate) { @@ -47,5 +47,4 @@ private static int countOccurrences(final int candidate, final int[] a) { } return count; } - } From 448f5681dc3ad5f5cbafe894b9cd300a722eaa9d Mon Sep 17 00:00:00 2001 From: vil02 Date: Tue, 31 Oct 2023 08:58:28 +0100 Subject: [PATCH 11/14] tests: add some test cases --- src/test/java/com/thealgorithms/others/BoyerMooreTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java index 0f2297ec2d59..b1497f7bc525 100644 --- a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java +++ b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java @@ -17,7 +17,7 @@ void checkWhenMajorityExists(int expected, int[] input) { } private static Stream inputStreamWithExistingMajority() { - return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 2}), Arguments.of(10, new int[] {10, 10, 20}), Arguments.of(10, new int[] {10, 20, 10}), Arguments.of(10, new int[] {20, 10, 10}), Arguments.of(4, new int[] {1, 4, 2, 4, 4, 5, 4})); + return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 2}), Arguments.of(10, new int[] {10, 10, 20}), Arguments.of(10, new int[] {10, 20, 10}), Arguments.of(10, new int[] {20, 10, 10}), Arguments.of(4, new int[] {1, 4, 2, 4, 4, 5, 4}), Arguments.of(-1, new int[] {-1})); } @ParameterizedTest @@ -27,6 +27,6 @@ void checkWhenMajorityExists(int[] input) { } private static Stream inputStreamWithoutMajority() { - return Stream.of(Arguments.of(new int[] {10, 10, 20, 20, 30, 30}), Arguments.of(new int[] {10, 20, 30, 40, 50})); + return Stream.of(Arguments.of(new int[] {10, 10, 20, 20, 30, 30}), Arguments.of(new int[] {10, 20, 30, 40, 50}), Arguments.of(new int[] {1, 2}), Arguments.of(new int[] {})); } } From becbb02e3a80f30c5471562b3a56538e3604191e Mon Sep 17 00:00:00 2001 From: vil02 Date: Tue, 31 Oct 2023 09:01:39 +0100 Subject: [PATCH 12/14] refactor: add `isMajority` and avoid rounding --- src/main/java/com/thealgorithms/others/BoyerMoore.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index 8412ec70c853..30153d2bba52 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -14,7 +14,7 @@ private BoyerMoore() { public static Optional findMajor(final int[] a) { final var candidate = findCandidate(a); final var count = countOccurrences(a, candidate); - if (count > (a.length / 2)) { + if (isMajority(count, a.length)) { return Optional.of(candidate); } return Optional.empty(); @@ -47,4 +47,8 @@ private static int countOccurrences(final int[] a, final int candidate) { } return count; } + + private static boolean isMajority(final int count, final int totalCount) { + return 2 * count > totalCount; + } } From 4864fc5e3cac2fe000102e80ac72e5e3612af380 Mon Sep 17 00:00:00 2001 From: vil02 Date: Tue, 31 Oct 2023 09:03:37 +0100 Subject: [PATCH 13/14] style: use `var` --- src/main/java/com/thealgorithms/others/BoyerMoore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index 30153d2bba52..65f0def0b0e4 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -23,7 +23,7 @@ public static Optional findMajor(final int[] a) { private static int findCandidate(final int[] a) { int count = 0; int candidate = -1; - for (final int k : a) { + for (final var k : a) { if (count == 0) { candidate = k; count = 1; @@ -40,7 +40,7 @@ private static int findCandidate(final int[] a) { private static int countOccurrences(final int[] a, final int candidate) { int count = 0; - for (final int j : a) { + for (final var j : a) { if (j == candidate) { count++; } From 64572897a8ba94667b5f4d5c866fe2f2d4523933 Mon Sep 17 00:00:00 2001 From: vil02 Date: Tue, 31 Oct 2023 09:05:44 +0100 Subject: [PATCH 14/14] style: swap arguments of `countOccurrences` --- src/main/java/com/thealgorithms/others/BoyerMoore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java index 65f0def0b0e4..e67427deda79 100644 --- a/src/main/java/com/thealgorithms/others/BoyerMoore.java +++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java @@ -13,7 +13,7 @@ private BoyerMoore() { public static Optional findMajor(final int[] a) { final var candidate = findCandidate(a); - final var count = countOccurrences(a, candidate); + final var count = countOccurrences(candidate, a); if (isMajority(count, a.length)) { return Optional.of(candidate); } @@ -38,7 +38,7 @@ private static int findCandidate(final int[] a) { return candidate; } - private static int countOccurrences(final int[] a, final int candidate) { + private static int countOccurrences(final int candidate, final int[] a) { int count = 0; for (final var j : a) { if (j == candidate) {