From f490a4f2627cc093ad2cfa4ba93809556254e7de Mon Sep 17 00:00:00 2001 From: Diego Leonardo Urban Date: Fri, 6 Nov 2026 16:30:52 +0100 Subject: [PATCH 1/2] Reformat all Java sources with palantir-java-format Whitespace, line wrapping, comment wrapping and import order only, plus one unused import removed (StringUtilsTest). Verified: with comments removed, whitespace collapsed and imports compared as sets, every file is identical to before. Co-Authored-By: Claude Opus 5.5 --- .../algorithms/datastructure/Element.java | 2 +- .../datastructure/array/ArrayUtils.java | 6 +-- .../datastructure/linkedlist/LinkedList.java | 30 +++++------ .../algorithms/datastructure/queue/Queue.java | 1 - .../algorithms/datastructure/set/Set.java | 6 +-- .../algorithms/datastructure/stack/Stack.java | 3 +- .../datastructure/vector/Vector.java | 25 ++++------ .../diegourban/algorithms/io/Reader.java | 3 +- .../algorithms/math/Combinatorics.java | 4 +- .../diegourban/algorithms/math/Geometry.java | 3 +- .../algorithms/math/Measurement.java | 4 +- .../algorithms/math/NumberTheory.java | 7 +-- .../diegourban/algorithms/math/Numbers.java | 46 +++++++---------- .../diegourban/algorithms/math/Sequences.java | 10 ++-- .../algorithms/math/Statistics.java | 7 +-- .../algorithms/math/TriangleType.java | 4 +- .../algorithms/search/BinarySearch.java | 9 ++-- .../algorithms/sorting/InsertionSort.java | 4 +- .../algorithms/sorting/MergeSort.java | 13 +++-- .../algorithms/sorting/QuickSort.java | 7 ++- .../algorithms/sorting/SelectionSort.java | 6 +-- .../algorithms/string/StringUtils.java | 3 +- .../datastructure/array/ArrayUtilsTest.java | 9 ++-- .../linkedlist/LinkedListTest.java | 4 +- .../datastructure/queue/QueueTest.java | 10 ++-- .../algorithms/datastructure/set/SetTest.java | 16 ++++-- .../datastructure/stack/StackTest.java | 4 +- .../datastructure/vector/VectorTest.java | 6 +-- .../diegourban/algorithms/io/ReaderTest.java | 38 ++++++++------ .../algorithms/math/CombinatoricsTest.java | 7 +-- .../algorithms/math/MeasurementTest.java | 4 +- .../math/NumberTheoryPropertyTest.java | 22 ++++---- .../algorithms/math/NumberTheoryTest.java | 29 ++++------- .../algorithms/math/NumbersTest.java | 7 ++- .../math/SequencesPropertyTest.java | 10 ++-- .../algorithms/math/SequencesTest.java | 7 ++- .../math/StatisticsPropertyTest.java | 17 ++++--- .../algorithms/math/StatisticsTest.java | 50 ++++++++++--------- .../algorithms/math/TriangleTypeTest.java | 10 ++-- .../problems/bit/DeltaBitsTest.java | 22 ++++---- .../problems/bit/FindUniqueIdTest.java | 10 ++-- .../algorithms/problems/bit/ParityTest.java | 7 +-- .../problems/bit/ToCarryOrNotToCarryTest.java | 11 ++-- .../array/EqualSidesOfAnArrayTest.java | 25 +++++----- .../array/LargestDifferenceTest.java | 20 ++++---- .../array/SingleRiffleCheckTest.java | 22 ++++---- .../array/TheForceAwakensTest.java | 38 +++++++------- .../graph/GraphColoringTest.java | 7 ++- .../datastructure/graph/MeshMessageTest.java | 43 ++++++++-------- .../linkedlist/DeleteNodeTest.java | 6 +-- .../linkedlist/FindRepeatBeastModeTest.java | 44 ++++++++-------- .../linkedlist/LinkedListCycleTest.java | 4 +- .../MergeSortedLinkedListsTest.java | 5 +- .../linkedlist/ReverseLinkedListTest.java | 4 +- .../datastructure/map/DroughtTest.java | 37 +++++++------- .../map/NumberFrequencyTest.java | 23 +++++---- .../queue/QueueTwoStacksTest.java | 7 ++- .../set/InflightEntertainmentTest.java | 15 +++--- .../set/PermutationPalindromeTest.java | 5 +- .../datastructure/set/TwiceLinearTest.java | 5 +- .../stack/BracketValidatorTest.java | 7 ++- .../stack/DiamondsAndSandTest.java | 9 ++-- .../datastructure/stack/LargestStackTest.java | 5 +- .../stack/ParenthesisBalanceTest.java | 13 +++-- .../tree/BalancedBinaryTreeTest.java | 10 ++-- .../tree/BinarySearchTreeCheckerTest.java | 10 ++-- .../tree/SecondLargestItemInBstTest.java | 6 +-- .../trie/MillionGazillionTest.java | 5 +- .../algorithms/problems/dp/CakeThiefTest.java | 11 ++-- .../greedy/BanknotesAndCoinsTest.java | 30 ++++++----- .../problems/math/CollectableCardsTest.java | 21 ++++---- .../problems/math/CommonDenominatorsTest.java | 19 ++++--- .../problems/math/DivisorsTest.java | 5 +- .../problems/math/EasyLineTest.java | 31 ++++++------ .../problems/math/FactorialSumTest.java | 23 ++++----- .../problems/math/FibonacciArrayTest.java | 20 ++++---- .../problems/math/PascalsTriangleTest.java | 22 ++++---- .../problems/math/PerfectNumberTest.java | 4 +- .../algorithms/problems/math/PrefaceTest.java | 25 +++++----- .../problems/math/PrimeGeneratorTest.java | 12 ++--- .../problems/math/PrimeNumberTest.java | 4 +- .../problems/math/PrimesInNumbersTest.java | 37 +++++++------- .../algorithms/problems/math/QueenTest.java | 25 +++++----- .../problems/math/RainfallTest.java | 17 ++++--- .../problems/math/RectangularLoveTest.java | 10 ++-- .../problems/math/RomanNumeralsTest.java | 28 +++++------ .../problems/math/SumByFactorsTest.java | 16 +++--- .../problems/math/TdaRationalTest.java | 24 +++++---- .../problems/math/TripleDoubleTest.java | 27 +++++----- .../recursion/CollatzConjectureTest.java | 12 ++--- .../RecursiveStringPermutationsTest.java | 5 +- .../recursion/SquareRootOfTwoTest.java | 17 +++---- .../problems/search/FindRepeatTest.java | 40 +++++++-------- .../search/FindRotationPointTest.java | 33 ++++++++---- .../problems/search/TwoBillsTest.java | 43 ++++++++-------- .../problems/simulation/MaestersMapTest.java | 39 +++++++-------- .../problems/simulation/StarTrekTest.java | 40 +++++++-------- .../problems/sorting/FindTheOddIntTest.java | 25 +++++----- .../problems/string/ContractRevisionTest.java | 9 ++-- .../problems/string/EncryptionTest.java | 9 ++-- .../algorithms/problems/string/MorseTest.java | 9 ++-- .../problems/string/OneTwoThreeTest.java | 9 ++-- .../string/ParenthesisMatchingTest.java | 6 +-- .../string/PlayingWithPassphrasesTest.java | 4 +- .../problems/string/ReverseWordsTest.java | 4 +- .../string/SearchingSubsequencesTest.java | 14 +++--- .../search/BinarySearchPropertyTest.java | 7 ++- .../algorithms/search/BinarySearchTest.java | 9 ++-- .../sorting/SorterPropertyTest.java | 7 ++- .../algorithms/sorting/SorterTest.java | 7 ++- .../string/StringUtilsPropertyTest.java | 24 ++++----- .../algorithms/string/StringUtilsTest.java | 7 +-- .../algorithms/testing/SampleBuilder.java | 1 - .../algorithms/testing/SampleBuilderTest.java | 27 +++++++--- .../algorithms/testing/SampleLoaderTest.java | 7 +-- 115 files changed, 827 insertions(+), 876 deletions(-) diff --git a/src/main/java/io/github/diegourban/algorithms/datastructure/Element.java b/src/main/java/io/github/diegourban/algorithms/datastructure/Element.java index c513365..bbd7fae 100644 --- a/src/main/java/io/github/diegourban/algorithms/datastructure/Element.java +++ b/src/main/java/io/github/diegourban/algorithms/datastructure/Element.java @@ -14,7 +14,7 @@ public String getName() { @Override public boolean equals(Object obj) { - if(obj == null) { + if (obj == null) { return false; } Element other = (Element) obj; diff --git a/src/main/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtils.java b/src/main/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtils.java index d5eb583..91f9738 100644 --- a/src/main/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtils.java +++ b/src/main/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtils.java @@ -4,8 +4,7 @@ public class ArrayUtils { - private ArrayUtils() { - } + private ArrayUtils() {} private static Random rand = new Random(); @@ -27,8 +26,7 @@ public static void shuffle(int[] theArray) { } // walk through from beginning to end - for (int indexWeAreChoosingFor = 0; - indexWeAreChoosingFor < theArray.length - 1; indexWeAreChoosingFor++) { + for (int indexWeAreChoosingFor = 0; indexWeAreChoosingFor < theArray.length - 1; indexWeAreChoosingFor++) { // choose a random not-yet-placed item to place there // (could also be the item currently in that spot) diff --git a/src/main/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedList.java b/src/main/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedList.java index e162d23..f7d3cf8 100644 --- a/src/main/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedList.java +++ b/src/main/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedList.java @@ -9,7 +9,7 @@ public class LinkedList { private int totalElements = 0; public void addAtStart(final Element element) { - if(this.totalElements == 0) { + if (this.totalElements == 0) { LinkedListNode newNode = new LinkedListNode(element); this.first = newNode; this.last = newNode; @@ -23,7 +23,7 @@ public void addAtStart(final Element element) { } public void addAtEnd(final Element element) { - if(this.totalElements == 0) { + if (this.totalElements == 0) { addAtStart(element); } else { LinkedListNode newNode = new LinkedListNode(element); @@ -36,9 +36,9 @@ public void addAtEnd(final Element element) { } public void add(final int position, final Element element) { - if(this.totalElements == 0) { + if (this.totalElements == 0) { addAtStart(element); - } else if(position == this.totalElements) { + } else if (position == this.totalElements) { addAtEnd(element); } else { LinkedListNode previous = this.getNode(position - 1); @@ -58,11 +58,11 @@ private boolean positionOccupied(final int position) { } private LinkedListNode getNode(final int position) { - if(!positionOccupied(position)) { + if (!positionOccupied(position)) { throw new IllegalArgumentException("Posição inexistente"); } LinkedListNode current = this.first; - for(int i = 0; i < position; i++) { + for (int i = 0; i < position; i++) { current = current.getNext(); } return current; @@ -77,20 +77,20 @@ public Element get(final int position) { } public void removeFromStart() { - if(this.totalElements == 0) { + if (this.totalElements == 0) { throw new IllegalStateException("Lista vazia"); } this.first = this.first.getNext(); this.totalElements--; - if(this.totalElements == 0) { + if (this.totalElements == 0) { this.last = null; } } public void removeFromEnd() { - if(this.totalElements == 1) { + if (this.totalElements == 1) { this.removeFromStart(); } else { LinkedListNode previous = this.last.getPrevious(); @@ -102,9 +102,9 @@ public void removeFromEnd() { } public void remove(final int position) { - if(position == 0) { + if (position == 0) { this.removeFromStart(); - } else if(position == totalElements - 1) { + } else if (position == totalElements - 1) { this.removeFromEnd(); } else { LinkedListNode previous = this.getNode(position - 1); @@ -123,8 +123,8 @@ public int size() { public boolean contains(Element element) { LinkedListNode current = this.first; - while(current != null) { - if(current.getElement().equals(element)) { + while (current != null) { + if (current.getElement().equals(element)) { return true; } current = current.getNext(); @@ -134,13 +134,13 @@ public boolean contains(Element element) { @Override public String toString() { - if(this.totalElements == 0) { + if (this.totalElements == 0) { return "[]"; } LinkedListNode current = this.first; StringBuilder sb = new StringBuilder("["); - for(int i = 0; i < this.totalElements; i++) { + for (int i = 0; i < this.totalElements; i++) { sb.append(current.getElement().getName()); sb.append(","); current = current.getNext(); diff --git a/src/main/java/io/github/diegourban/algorithms/datastructure/queue/Queue.java b/src/main/java/io/github/diegourban/algorithms/datastructure/queue/Queue.java index 0c257bc..94adc62 100644 --- a/src/main/java/io/github/diegourban/algorithms/datastructure/queue/Queue.java +++ b/src/main/java/io/github/diegourban/algorithms/datastructure/queue/Queue.java @@ -1,7 +1,6 @@ package io.github.diegourban.algorithms.datastructure.queue; import io.github.diegourban.algorithms.datastructure.Element; - import java.util.LinkedList; public class Queue { diff --git a/src/main/java/io/github/diegourban/algorithms/datastructure/set/Set.java b/src/main/java/io/github/diegourban/algorithms/datastructure/set/Set.java index eda3aaf..1aeb6f2 100644 --- a/src/main/java/io/github/diegourban/algorithms/datastructure/set/Set.java +++ b/src/main/java/io/github/diegourban/algorithms/datastructure/set/Set.java @@ -8,13 +8,13 @@ public class Set { private List> table = new LinkedList>(); public Set() { - for(int i = 0; i < 26; i++) { + for (int i = 0; i < 26; i++) { this.table.add(new LinkedList()); } } public void add(String word) { - if(!contains(word)) { + if (!contains(word)) { int index = calculateTableIndex(word); List list = this.table.get(index); list.add(word); @@ -22,7 +22,7 @@ public void add(String word) { } public void remove(String word) { - if(contains(word)) { + if (contains(word)) { int index = calculateTableIndex(word); List list = this.table.get(index); list.remove(word); diff --git a/src/main/java/io/github/diegourban/algorithms/datastructure/stack/Stack.java b/src/main/java/io/github/diegourban/algorithms/datastructure/stack/Stack.java index c738986..a55ee27 100644 --- a/src/main/java/io/github/diegourban/algorithms/datastructure/stack/Stack.java +++ b/src/main/java/io/github/diegourban/algorithms/datastructure/stack/Stack.java @@ -1,7 +1,6 @@ package io.github.diegourban.algorithms.datastructure.stack; import io.github.diegourban.algorithms.datastructure.Element; - import java.util.LinkedList; import java.util.List; @@ -14,7 +13,7 @@ public void push(Element element) { } public Element pop() { - return this.elements.remove(this.elements.size() - 1 ); + return this.elements.remove(this.elements.size() - 1); } public boolean empty() { diff --git a/src/main/java/io/github/diegourban/algorithms/datastructure/vector/Vector.java b/src/main/java/io/github/diegourban/algorithms/datastructure/vector/Vector.java index 7430021..5af4e8b 100644 --- a/src/main/java/io/github/diegourban/algorithms/datastructure/vector/Vector.java +++ b/src/main/java/io/github/diegourban/algorithms/datastructure/vector/Vector.java @@ -1,7 +1,6 @@ package io.github.diegourban.algorithms.datastructure.vector; import io.github.diegourban.algorithms.datastructure.Element; - import java.util.Arrays; public class Vector { @@ -17,13 +16,13 @@ public void add(final Element element) { } public void add(final int position, final Element element) { - if(!isPositionValid(position)) { + if (!isPositionValid(position)) { throw new IllegalArgumentException("Invalid position"); } this.guaranteesSpace(); - for(int i = this.totalElements - 1; i >= position; i--) { - this.elements[i+1] = this.elements[i]; + for (int i = this.totalElements - 1; i >= position; i--) { + this.elements[i + 1] = this.elements[i]; } this.elements[position] = element; this.totalElements++; @@ -34,7 +33,7 @@ private boolean isPositionValid(final int position) { } public Element get(final int position) { - if(!positionOccupied(position)) { + if (!positionOccupied(position)) { throw new IllegalArgumentException("Invalid position"); } return this.elements[position]; @@ -45,16 +44,16 @@ private boolean positionOccupied(final int position) { } public void remove(final int position) { - for(int i = position; i < this.totalElements; i++) { - this.elements[i] = this.elements[i+1]; + for (int i = position; i < this.totalElements; i++) { + this.elements[i] = this.elements[i + 1]; } totalElements--; this.elements[this.totalElements] = null; } public boolean contains(final Element element) { - for(int i = 0; i < this.totalElements; i++) { - if(element.equals(this.elements[i])) { + for (int i = 0; i < this.totalElements; i++) { + if (element.equals(this.elements[i])) { return true; } } @@ -67,9 +66,9 @@ public int size() { } private void guaranteesSpace() { - if(this.totalElements == this.elements.length) { + if (this.totalElements == this.elements.length) { Element[] newElements = new Element[this.elements.length * 2]; - for(int i = 0; i < this.elements.length; i++) { + for (int i = 0; i < this.elements.length; i++) { newElements[i] = this.elements[i]; } this.elements = newElements; @@ -78,8 +77,6 @@ private void guaranteesSpace() { @Override public String toString() { - return "Vector{" + - "elements=" + Arrays.toString(elements) + - '}'; + return "Vector{" + "elements=" + Arrays.toString(elements) + '}'; } } diff --git a/src/main/java/io/github/diegourban/algorithms/io/Reader.java b/src/main/java/io/github/diegourban/algorithms/io/Reader.java index 3a6585e..c9cc53d 100644 --- a/src/main/java/io/github/diegourban/algorithms/io/Reader.java +++ b/src/main/java/io/github/diegourban/algorithms/io/Reader.java @@ -142,7 +142,7 @@ private void fillBuffer() throws IOException { } private byte read() throws IOException { - if(isEOF()) { + if (isEOF()) { throw new EOFException(); } if (bufferPointer == bytesRead) { @@ -166,5 +166,4 @@ public void close() throws IOException { public boolean isClosed() { return din == null; } - } diff --git a/src/main/java/io/github/diegourban/algorithms/math/Combinatorics.java b/src/main/java/io/github/diegourban/algorithms/math/Combinatorics.java index 9b96a25..b037df1 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/Combinatorics.java +++ b/src/main/java/io/github/diegourban/algorithms/math/Combinatorics.java @@ -4,8 +4,7 @@ public class Combinatorics { - private Combinatorics() { - } + private Combinatorics() {} /** * Pascal's Triangle @@ -47,5 +46,4 @@ public static BigInteger binomialCoefficient(int n, int m) { return binomials[m]; } - } diff --git a/src/main/java/io/github/diegourban/algorithms/math/Geometry.java b/src/main/java/io/github/diegourban/algorithms/math/Geometry.java index 8ac86ee..bbe26d8 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/Geometry.java +++ b/src/main/java/io/github/diegourban/algorithms/math/Geometry.java @@ -4,8 +4,7 @@ public class Geometry { - private Geometry() { - } + private Geometry() {} private static final double SIMPLE_PI = 3.14; diff --git a/src/main/java/io/github/diegourban/algorithms/math/Measurement.java b/src/main/java/io/github/diegourban/algorithms/math/Measurement.java index f3d5b0e..d1ded89 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/Measurement.java +++ b/src/main/java/io/github/diegourban/algorithms/math/Measurement.java @@ -2,8 +2,7 @@ public class Measurement { - private Measurement() { - } + private Measurement() {} /** * Converts miles to kilometers @@ -24,5 +23,4 @@ public static double milesToKilometers(double miles) { public static double kilometersToMiles(double kilometers) { return kilometers * 0.621371; } - } diff --git a/src/main/java/io/github/diegourban/algorithms/math/NumberTheory.java b/src/main/java/io/github/diegourban/algorithms/math/NumberTheory.java index 2295228..2463374 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/NumberTheory.java +++ b/src/main/java/io/github/diegourban/algorithms/math/NumberTheory.java @@ -7,8 +7,7 @@ */ public class NumberTheory { - private NumberTheory() { - } + private NumberTheory() {} /** * Least common multiple (lcm) of two numbers. @@ -29,8 +28,7 @@ public static long lcm(long a, long b) { */ public static long lcm(long[] input) { long result = input[0]; - for (int i = 1; i < input.length; i++) - result = lcm(result, input[i]); + for (int i = 1; i < input.length; i++) result = lcm(result, input[i]); return result; } @@ -139,5 +137,4 @@ private static void fillPrimeFactors(int number, Collection factors) { factors.add(n); } } - } diff --git a/src/main/java/io/github/diegourban/algorithms/math/Numbers.java b/src/main/java/io/github/diegourban/algorithms/math/Numbers.java index 191182e..e1f15a8 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/Numbers.java +++ b/src/main/java/io/github/diegourban/algorithms/math/Numbers.java @@ -2,8 +2,7 @@ public class Numbers { - private Numbers() { - } + private Numbers() {} /** * Checks if a number contains a digit @@ -18,7 +17,9 @@ public static boolean containsDigit(long number, long digit) { while (thisNumber != 0) { thisDigit = thisNumber % 10; // Always equal to the last digit of thisNumber - thisNumber = thisNumber / 10; // Always equal to thisNumber with the last digit chopped off, or 0 if thisNumber is less than 10 + thisNumber = thisNumber + / 10; // Always equal to thisNumber with the last digit chopped off, or 0 if thisNumber is less than + // 10 if (thisDigit == digit) { return true; } @@ -82,47 +83,36 @@ public static int[] toArray(final long number) { * @return the length of the number */ public static int length(int number) { - if (number < 100000){ + if (number < 100000) { // 5 or less - if (number < 100){ + if (number < 100) { // 1 or 2 - if (number < 10) - return 1; - else - return 2; - }else{ + if (number < 10) return 1; + else return 2; + } else { // 3 or 4 or 5 - if (number < 1000) - return 3; - else{ + if (number < 1000) return 3; + else { // 4 or 5 - if (number < 10000) - return 4; - else - return 5; + if (number < 10000) return 4; + else return 5; } } } else { // 6 or more if (number < 10000000) { // 6 or 7 - if (number < 1000000) - return 6; - else - return 7; + if (number < 1000000) return 6; + else return 7; } else { // 8 to 10 - if (number < 100000000) - return 8; + if (number < 100000000) return 8; else { // 9 or 10 - if (number < 1000000000) - return 9; - else - return 10; + if (number < 1000000000) return 9; + else return 10; } } } } - } diff --git a/src/main/java/io/github/diegourban/algorithms/math/Sequences.java b/src/main/java/io/github/diegourban/algorithms/math/Sequences.java index 031578f..da67b4e 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/Sequences.java +++ b/src/main/java/io/github/diegourban/algorithms/math/Sequences.java @@ -9,8 +9,7 @@ */ public class Sequences { - private Sequences() { - } + private Sequences() {} /** * Calculates the nth Fibonacci number. @@ -33,9 +32,9 @@ public static int fibonacci(int n) { // we'll be building the fibonacci series from the bottom up // so we'll need to track the previous 2 numbers at each step - int prevPrev = 0; // 0th fibonacci - int prev = 1; // 1st fibonacci - int current = 0; // Declare and initialize current + int prevPrev = 0; // 0th fibonacci + int prev = 1; // 1st fibonacci + int current = 0; // Declare and initialize current for (int i = 1; i < n; i++) { @@ -92,5 +91,4 @@ public static BigInteger fibonacciBinetFormula(int n) { BigDecimal powInvrt = BigDecimal.valueOf(Math.pow(-phi, -n)); return pow.subtract(powInvrt).divide(sqrt5, RoundingMode.HALF_UP).toBigInteger(); } - } diff --git a/src/main/java/io/github/diegourban/algorithms/math/Statistics.java b/src/main/java/io/github/diegourban/algorithms/math/Statistics.java index f156fa8..69a6019 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/Statistics.java +++ b/src/main/java/io/github/diegourban/algorithms/math/Statistics.java @@ -4,8 +4,7 @@ public class Statistics { - private Statistics() { - } + private Statistics() {} /** * An average is the sum of a list of numbers divided by the number of @@ -58,8 +57,7 @@ public static double mode(double[] data) { for (int i = 0; i < data.length; ++i) { int count = 0; for (int j = 0; j < data.length; ++j) { - if (data[j] == data[i]) - ++count; + if (data[j] == data[i]) ++count; } if (count > maxCount) { maxCount = count; @@ -87,5 +85,4 @@ public static double meanAbsoluteDeviation(double[] data) { return sum / data.length; } - } diff --git a/src/main/java/io/github/diegourban/algorithms/math/TriangleType.java b/src/main/java/io/github/diegourban/algorithms/math/TriangleType.java index 2917c35..3094bdb 100644 --- a/src/main/java/io/github/diegourban/algorithms/math/TriangleType.java +++ b/src/main/java/io/github/diegourban/algorithms/math/TriangleType.java @@ -1,7 +1,9 @@ package io.github.diegourban.algorithms.math; public enum TriangleType { - EQUILATERAL("Equilatero"), ESCALENE("Escaleno"), ISOSCELES("Isoceles"); + EQUILATERAL("Equilatero"), + ESCALENE("Escaleno"), + ISOSCELES("Isoceles"); private String name; diff --git a/src/main/java/io/github/diegourban/algorithms/search/BinarySearch.java b/src/main/java/io/github/diegourban/algorithms/search/BinarySearch.java index 751cb17..6e608a0 100644 --- a/src/main/java/io/github/diegourban/algorithms/search/BinarySearch.java +++ b/src/main/java/io/github/diegourban/algorithms/search/BinarySearch.java @@ -5,8 +5,7 @@ */ public class BinarySearch { - private BinarySearch() { - } + private BinarySearch() {} /** * Finds an element in an ordered list @@ -31,17 +30,17 @@ public static int indexOf(final int[] sorted, final int key) { * @return the index of the element, -1 otherwise */ private static int find(final int[] items, final int from, final int to, final int element) { - if(from > to) { + if (from > to) { return -1; } final int middle = (from + to) / 2; final int middleItem = items[middle]; - if(element == middleItem) { + if (element == middleItem) { return middle; } - if(element < middleItem) { + if (element < middleItem) { return find(items, from, middle - 1, element); } return find(items, middle + 1, to, element); diff --git a/src/main/java/io/github/diegourban/algorithms/sorting/InsertionSort.java b/src/main/java/io/github/diegourban/algorithms/sorting/InsertionSort.java index 122cbe6..8260efd 100644 --- a/src/main/java/io/github/diegourban/algorithms/sorting/InsertionSort.java +++ b/src/main/java/io/github/diegourban/algorithms/sorting/InsertionSort.java @@ -17,10 +17,10 @@ public void sort(final int[] items) { * @param totalElements total elements */ private void sort(final int[] items, final int totalElements) { - for(int current = 1; current < totalElements; current++) { + for (int current = 1; current < totalElements; current++) { int analysis = current; - while(analysis > 0 && items[analysis] < items[analysis - 1]) { + while (analysis > 0 && items[analysis] < items[analysis - 1]) { Integer analysisItem = items[analysis]; Integer previousItem = items[analysis - 1]; items[analysis] = previousItem; diff --git a/src/main/java/io/github/diegourban/algorithms/sorting/MergeSort.java b/src/main/java/io/github/diegourban/algorithms/sorting/MergeSort.java index 964d8b6..2a2779d 100644 --- a/src/main/java/io/github/diegourban/algorithms/sorting/MergeSort.java +++ b/src/main/java/io/github/diegourban/algorithms/sorting/MergeSort.java @@ -19,7 +19,7 @@ public void sort(final int[] items) { */ private void sort(final int[] items, final int begin, final int end) { final int total = end - begin; - if(total > 1) { + if (total > 1) { final int middle = (begin + end) / 2; sort(items, begin, middle); sort(items, middle, end); @@ -34,11 +34,11 @@ private void merge(final int[] items, final int begin, final int middle, final i int currentLeft = begin; int currentRight = middle; - while(currentLeft < middle && currentRight < end) { + while (currentLeft < middle && currentRight < end) { final int firstValue = items[currentLeft]; final int secondValue = items[currentRight]; - if(firstValue < secondValue) { + if (firstValue < secondValue) { result[current] = firstValue; currentLeft++; } else { @@ -50,22 +50,21 @@ private void merge(final int[] items, final int begin, final int middle, final i } // remaining values on the left side - while(currentLeft < middle) { + while (currentLeft < middle) { result[current] = items[currentLeft]; currentLeft++; current++; } // remaining values on the right side - while(currentRight < end) { + while (currentRight < end) { result[current] = items[currentRight]; currentRight++; current++; } - for(int count = 0; count < current; count++) { + for (int count = 0; count < current; count++) { items[begin + count] = result[count]; } } - } diff --git a/src/main/java/io/github/diegourban/algorithms/sorting/QuickSort.java b/src/main/java/io/github/diegourban/algorithms/sorting/QuickSort.java index ed234d0..b34a40a 100644 --- a/src/main/java/io/github/diegourban/algorithms/sorting/QuickSort.java +++ b/src/main/java/io/github/diegourban/algorithms/sorting/QuickSort.java @@ -19,7 +19,7 @@ public void sort(final int[] items) { */ private void sort(final int[] items, final int start, final int end) { final int totalElements = end - start; - if(totalElements > 1) { + if (totalElements > 1) { final int pivotPosition = partition(items, start, end); sort(items, start, pivotPosition); sort(items, pivotPosition + 1, end); @@ -30,9 +30,9 @@ private int partition(final int[] items, final int start, final int end) { int minorsFound = start; final int pivot = items[end - 1]; - for(int current = start; current < end - 1; current++) { + for (int current = start; current < end - 1; current++) { final int currentItem = items[current]; - if(currentItem <= pivot) { + if (currentItem <= pivot) { swap(items, current, minorsFound); minorsFound++; } @@ -46,5 +46,4 @@ private void swap(final int[] items, final int from, final int to) { items[from] = items[to]; items[to] = temp; } - } diff --git a/src/main/java/io/github/diegourban/algorithms/sorting/SelectionSort.java b/src/main/java/io/github/diegourban/algorithms/sorting/SelectionSort.java index 98ca7b3..03d924b 100644 --- a/src/main/java/io/github/diegourban/algorithms/sorting/SelectionSort.java +++ b/src/main/java/io/github/diegourban/algorithms/sorting/SelectionSort.java @@ -17,7 +17,7 @@ public void sort(final int[] items) { * @param totalElements total elements */ private void sort(final int[] items, final int totalElements) { - for(int current = 0; current < totalElements - 1; current++) { + for (int current = 0; current < totalElements - 1; current++) { final int smallerIndex = findSmallerIndex(items, current, totalElements); Integer currentItem = items[current]; Integer smallerItem = items[smallerIndex]; @@ -28,8 +28,8 @@ private void sort(final int[] items, final int totalElements) { private int findSmallerIndex(final int[] items, final int begin, final int end) { int smallerIndex = begin; - for(int i = begin; i < end; i++) { - if(items[i] < items[smallerIndex]) { + for (int i = begin; i < end; i++) { + if (items[i] < items[smallerIndex]) { smallerIndex = i; } } diff --git a/src/main/java/io/github/diegourban/algorithms/string/StringUtils.java b/src/main/java/io/github/diegourban/algorithms/string/StringUtils.java index 73b8355..b563570 100644 --- a/src/main/java/io/github/diegourban/algorithms/string/StringUtils.java +++ b/src/main/java/io/github/diegourban/algorithms/string/StringUtils.java @@ -4,8 +4,7 @@ public class StringUtils { - private StringUtils() { - } + private StringUtils() {} public static boolean isPalindrome(String theString) { if (theString == null || theString.isEmpty()) { diff --git a/src/test/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtilsTest.java b/src/test/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtilsTest.java index 901da00..91a3e0a 100644 --- a/src/test/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtilsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/datastructure/array/ArrayUtilsTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.datastructure.array; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; class ArrayUtilsTest { @Test void shouldNotShuffleSmallArray() { - int[] array = new int[]{1}; + int[] array = new int[] {1}; ArrayUtils.shuffle(array); assertThat(array).hasSize(1); @@ -20,7 +19,7 @@ void shouldNotShuffleSmallArray() { @Test void shouldShuffle() { - int[] array = new int[]{1, 2, 3, 4, 5}; + int[] array = new int[] {1, 2, 3, 4, 5}; ArrayUtils.shuffle(array); assertThat(array).hasSize(5); diff --git a/src/test/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedListTest.java b/src/test/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedListTest.java index 926e270..59f9178 100644 --- a/src/test/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedListTest.java +++ b/src/test/java/io/github/diegourban/algorithms/datastructure/linkedlist/LinkedListTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.datastructure.linkedlist; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.datastructure.Element; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class LinkedListTest { @Test diff --git a/src/test/java/io/github/diegourban/algorithms/datastructure/queue/QueueTest.java b/src/test/java/io/github/diegourban/algorithms/datastructure/queue/QueueTest.java index 7f4405a..3901b3b 100644 --- a/src/test/java/io/github/diegourban/algorithms/datastructure/queue/QueueTest.java +++ b/src/test/java/io/github/diegourban/algorithms/datastructure/queue/QueueTest.java @@ -1,14 +1,14 @@ package io.github.diegourban.algorithms.datastructure.queue; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.datastructure.Element; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class QueueTest { @Test - void shouldAdd(){ + void shouldAdd() { final Queue queue = new Queue(); queue.add(new Element("diego")); queue.add(new Element("leonardo")); @@ -18,7 +18,7 @@ void shouldAdd(){ } @Test - void shouldPoll(){ + void shouldPoll() { final Queue queue = new Queue(); queue.add(new Element("diego")); queue.add(new Element("leonardo")); @@ -28,6 +28,4 @@ void shouldPoll(){ assertThat(removed.getName()).isEqualTo("diego"); assertThat(queue).hasToString("[leonardo, urban]"); } - - } diff --git a/src/test/java/io/github/diegourban/algorithms/datastructure/set/SetTest.java b/src/test/java/io/github/diegourban/algorithms/datastructure/set/SetTest.java index 3f6b865..f63d63a 100644 --- a/src/test/java/io/github/diegourban/algorithms/datastructure/set/SetTest.java +++ b/src/test/java/io/github/diegourban/algorithms/datastructure/set/SetTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.datastructure.set; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + class SetTest { @Test @@ -12,7 +12,9 @@ void shouldAdd() { set.add("Diego"); set.add("André"); set.add("Zuleika"); - assertThat(set).hasToString("[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]"); + assertThat(set) + .hasToString( + "[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]"); } @Test @@ -21,8 +23,12 @@ void shouldRemove() { set.add("Diego"); set.add("André"); set.add("Zuleika"); - assertThat(set).hasToString("[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]"); + assertThat(set) + .hasToString( + "[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [André], [], [], [Diego], [], [], []]"); set.remove("André"); - assertThat(set).hasToString("[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [], [], [], [Diego], [], [], []]"); + assertThat(set) + .hasToString( + "[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [Zuleika], [], [], [], [Diego], [], [], []]"); } } diff --git a/src/test/java/io/github/diegourban/algorithms/datastructure/stack/StackTest.java b/src/test/java/io/github/diegourban/algorithms/datastructure/stack/StackTest.java index 9d1a907..50f739f 100644 --- a/src/test/java/io/github/diegourban/algorithms/datastructure/stack/StackTest.java +++ b/src/test/java/io/github/diegourban/algorithms/datastructure/stack/StackTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.datastructure.stack; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.datastructure.Element; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class StackTest { @Test diff --git a/src/test/java/io/github/diegourban/algorithms/datastructure/vector/VectorTest.java b/src/test/java/io/github/diegourban/algorithms/datastructure/vector/VectorTest.java index 28efa45..d7c6d1d 100644 --- a/src/test/java/io/github/diegourban/algorithms/datastructure/vector/VectorTest.java +++ b/src/test/java/io/github/diegourban/algorithms/datastructure/vector/VectorTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.datastructure.vector; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.datastructure.Element; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class VectorTest { @Test @@ -26,7 +26,7 @@ void test() { vector.remove(1); - for(int i = 0; i < 300; i++) { + for (int i = 0; i < 300; i++) { Element e = new Element("Element " + i); vector.add(e); } diff --git a/src/test/java/io/github/diegourban/algorithms/io/ReaderTest.java b/src/test/java/io/github/diegourban/algorithms/io/ReaderTest.java index b70ea58..5ae48b4 100644 --- a/src/test/java/io/github/diegourban/algorithms/io/ReaderTest.java +++ b/src/test/java/io/github/diegourban/algorithms/io/ReaderTest.java @@ -1,15 +1,14 @@ package io.github.diegourban.algorithms.io; -import io.github.diegourban.algorithms.testing.SampleBuilder; -import org.junit.jupiter.api.Test; - -import java.io.EOFException; -import java.io.InputStream; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.within; +import io.github.diegourban.algorithms.testing.SampleBuilder; +import java.io.EOFException; +import java.io.InputStream; +import org.junit.jupiter.api.Test; + class ReaderTest { @Test @@ -40,17 +39,21 @@ void shouldHandleEOF() throws Exception { @Test void shouldReadUntilEOF() throws Exception { String[] inputs = {"first line", "second line", "last line"}; - InputStream input = new SampleBuilder().appendln(inputs[0]).appendln(inputs[1]).append(inputs[2]).buildAsInputStream(); + InputStream input = new SampleBuilder() + .appendln(inputs[0]) + .appendln(inputs[1]) + .append(inputs[2]) + .buildAsInputStream(); Reader reader = new Reader(input); int i = 0; - while(true) { + while (true) { try { String line = reader.readLine(); assertThat(line).isEqualTo(inputs[i]); i++; - } catch(EOFException ex) { - break; + } catch (EOFException ex) { + break; } } reader.close(); @@ -81,7 +84,9 @@ void shouldReadOneLine() throws Exception { @Test void shouldReadTwoLines() throws Exception { - InputStream input = new SampleBuilder().appendln("this is a line").appendln("this is another line") + InputStream input = new SampleBuilder() + .appendln("this is a line") + .appendln("this is another line") .buildAsInputStream(); Reader reader = new Reader(input); @@ -95,7 +100,8 @@ void shouldReadTwoLines() throws Exception { @Test void shouldJumpEmptyLines() throws Exception { - InputStream input = new SampleBuilder().appendln("").appendln("this is a line").buildAsInputStream(); + InputStream input = + new SampleBuilder().appendln("").appendln("this is a line").buildAsInputStream(); Reader reader = new Reader(input); String line = reader.readLine(); @@ -314,7 +320,8 @@ void shouldDoubleWithNineDecimalPlaces() throws Exception { @Test void shouldReadChainOfPositiveAndNegativeDoubles() throws Exception { - InputStream input = new SampleBuilder().appendln("-1.23 98.7 -8.8 7.4 -9 7").buildAsInputStream(); + InputStream input = + new SampleBuilder().appendln("-1.23 98.7 -8.8 7.4 -9 7").buildAsInputStream(); Reader reader = new Reader(input); double first = reader.nextDouble(); @@ -380,7 +387,9 @@ void shouldReadChainOfPositiveAndNegativeLongs() throws Exception { @Test void shouldReadLongsWithSpacesAndLineBreaks() throws Exception { - InputStream input = new SampleBuilder().appendln("6546546565 -4326757896").appendln("786676767") + InputStream input = new SampleBuilder() + .appendln("6546546565 -4326757896") + .appendln("786676767") .buildAsInputStream(); Reader reader = new Reader(input); @@ -488,5 +497,4 @@ void shouldSkipNChars() throws Exception { assertThat(day).isEqualTo(5); } - } diff --git a/src/test/java/io/github/diegourban/algorithms/math/CombinatoricsTest.java b/src/test/java/io/github/diegourban/algorithms/math/CombinatoricsTest.java index 4290e42..a1b9330 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/CombinatoricsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/CombinatoricsTest.java @@ -1,11 +1,9 @@ package io.github.diegourban.algorithms.math; - -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.math.BigInteger; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; class CombinatoricsTest { @@ -33,5 +31,4 @@ void shouldCalculateBinomialCoefficient() { assertThat(Combinatorics.binomialCoefficient(7, 4)).isEqualTo(new BigInteger("35")); assertThat(Combinatorics.binomialCoefficient(7, 5)).isEqualTo(new BigInteger("21")); } - } diff --git a/src/test/java/io/github/diegourban/algorithms/math/MeasurementTest.java b/src/test/java/io/github/diegourban/algorithms/math/MeasurementTest.java index cc63a5f..e0d92e3 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/MeasurementTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/MeasurementTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.math; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.within; +import org.junit.jupiter.api.Test; + class MeasurementTest { @Test diff --git a/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryPropertyTest.java b/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryPropertyTest.java index 1ddddc9..c2bdc4b 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryPropertyTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryPropertyTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.math; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashSet; +import java.util.List; import net.jqwik.api.ForAll; import net.jqwik.api.Property; import net.jqwik.api.constraints.IntRange; import net.jqwik.api.constraints.LongRange; import net.jqwik.api.constraints.Size; -import java.util.HashSet; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - class NumberTheoryPropertyTest { @Property @@ -27,13 +26,14 @@ void gcdDividesBothNumbers(@ForAll @LongRange(min = 1) long a, @ForAll @LongRang } @Property - void lcmTimesGcdIsTheProduct(@ForAll @LongRange(min = 1, max = 1_000_000) long a, - @ForAll @LongRange(min = 1, max = 1_000_000) long b) { + void lcmTimesGcdIsTheProduct( + @ForAll @LongRange(min = 1, max = 1_000_000) long a, @ForAll @LongRange(min = 1, max = 1_000_000) long b) { assertThat(NumberTheory.lcm(a, b) * NumberTheory.gcdLoop(a, b)).isEqualTo(a * b); } @Property - void lcmOfAListIsAMultipleOfEveryElement(@ForAll @Size(min = 1, max = 6) List<@LongRange(min = 1, max = 1_000) Long> numbers) { + void lcmOfAListIsAMultipleOfEveryElement( + @ForAll @Size(min = 1, max = 6) List<@LongRange(min = 1, max = 1_000) Long> numbers) { var lcm = NumberTheory.lcm(numbers.stream().mapToLong(Long::longValue).toArray()); assertThat(numbers).allSatisfy(n -> assertThat(lcm % n).isZero()); @@ -48,8 +48,10 @@ void isPrimeAgreesWithTrialDivision(@ForAll @IntRange(min = -10, max = 200_000) void primeFactorsMultiplyBackToTheNumber(@ForAll @IntRange(min = 2, max = 10_000_000) int n) { var factors = NumberTheory.primeFactorsList(n); - assertThat(factors.stream().mapToLong(Integer::longValue).reduce(1, (x, y) -> x * y)).isEqualTo(n); - assertThat(factors).allSatisfy(f -> assertThat(isPrimeByTrialDivision(f)).isTrue()); + assertThat(factors.stream().mapToLong(Integer::longValue).reduce(1, (x, y) -> x * y)) + .isEqualTo(n); + assertThat(factors) + .allSatisfy(f -> assertThat(isPrimeByTrialDivision(f)).isTrue()); assertThat(factors).isSorted(); } diff --git a/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryTest.java b/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryTest.java index e87eb68..028a536 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/NumberTheoryTest.java @@ -1,12 +1,11 @@ package io.github.diegourban.algorithms.math; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import static org.assertj.core.api.Assertions.assertThat; import java.util.List; import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; class NumberTheoryTest { @@ -24,9 +23,9 @@ void shouldFindLeastCommonMultiplesOfTwoNumbers() { @Test void shouldFindLeastCommonMultiplesOfList() { - assertThat(NumberTheory.lcm(new long[]{1, 2, 3})).isEqualTo(6); - assertThat(NumberTheory.lcm(new long[]{2, 4, 6})).isEqualTo(12); - assertThat(NumberTheory.lcm(new long[]{7, 11, 13})).isEqualTo(1001); + assertThat(NumberTheory.lcm(new long[] {1, 2, 3})).isEqualTo(6); + assertThat(NumberTheory.lcm(new long[] {2, 4, 6})).isEqualTo(12); + assertThat(NumberTheory.lcm(new long[] {7, 11, 13})).isEqualTo(1001); } @Test @@ -149,26 +148,18 @@ void shouldListPrimeFactorsOfPrimeNumber() { @Test void shouldCreateSetOfPrimeFactorsOfEvenNumber() { Set primeFactors = NumberTheory.primeFactorsSet(12); - assertThat(primeFactors) - .hasSize(2) - .contains(2) - .contains(3); + assertThat(primeFactors).hasSize(2).contains(2).contains(3); } @Test void shouldCreateSetOfPrimeFactorsOfOddNumber() { Set primeFactors = NumberTheory.primeFactorsSet(49); - assertThat(primeFactors) - .hasSize(1) - .contains(7); + assertThat(primeFactors).hasSize(1).contains(7); } @Test void shouldCreateSetOfPrimeFactorsOfPrimeNumber() { Set primeFactors = NumberTheory.primeFactorsSet(7); - assertThat(primeFactors) - .hasSize(1) - .contains(7); + assertThat(primeFactors).hasSize(1).contains(7); } - -} \ No newline at end of file +} diff --git a/src/test/java/io/github/diegourban/algorithms/math/NumbersTest.java b/src/test/java/io/github/diegourban/algorithms/math/NumbersTest.java index 96f8df2..fc8aba5 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/NumbersTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/NumbersTest.java @@ -1,10 +1,9 @@ package io.github.diegourban.algorithms.math; +import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class NumbersTest { @Test @@ -22,8 +21,8 @@ void shouldHandleContainsRepatingDigit() { @Test void shouldConvertNumberToArray() { - assertThat(Numbers.toArray(123)).containsExactly(new int[]{3, 2, 1}); - assertThat(Numbers.toArray(321)).containsExactly(new int[]{1, 2, 3}); + assertThat(Numbers.toArray(123)).containsExactly(new int[] {3, 2, 1}); + assertThat(Numbers.toArray(321)).containsExactly(new int[] {1, 2, 3}); } @Test diff --git a/src/test/java/io/github/diegourban/algorithms/math/SequencesPropertyTest.java b/src/test/java/io/github/diegourban/algorithms/math/SequencesPropertyTest.java index a843a43..d7b2ff8 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/SequencesPropertyTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/SequencesPropertyTest.java @@ -1,14 +1,13 @@ package io.github.diegourban.algorithms.math; +import static org.assertj.core.api.Assertions.assertThat; + +import java.math.BigInteger; import net.jqwik.api.ForAll; import net.jqwik.api.GenerationMode; import net.jqwik.api.Property; import net.jqwik.api.constraints.IntRange; -import java.math.BigInteger; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Checks each Fibonacci variant against an exact reference, over the range its Javadoc says it supports. */ @@ -21,7 +20,8 @@ void fibonacciIsExactUpTo46(@ForAll @IntRange(min = 0, max = 46) int n) { @Property(generation = GenerationMode.EXHAUSTIVE) void simpleBinetFormulaRoundsToTheExactValueUpTo67(@ForAll @IntRange(min = 0, max = 67) int n) { - assertThat(BigInteger.valueOf(Math.round(Sequences.fibonacciSimpleBinetFormula(n)))).isEqualTo(exactFibonacci(n)); + assertThat(BigInteger.valueOf(Math.round(Sequences.fibonacciSimpleBinetFormula(n)))) + .isEqualTo(exactFibonacci(n)); } @Property(generation = GenerationMode.EXHAUSTIVE) diff --git a/src/test/java/io/github/diegourban/algorithms/math/SequencesTest.java b/src/test/java/io/github/diegourban/algorithms/math/SequencesTest.java index 6f5728d..b1d893a 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/SequencesTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/SequencesTest.java @@ -1,13 +1,12 @@ package io.github.diegourban.algorithms.math; -import org.junit.jupiter.api.Test; - -import java.math.BigInteger; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.within; +import java.math.BigInteger; +import org.junit.jupiter.api.Test; + class SequencesTest { @Test diff --git a/src/test/java/io/github/diegourban/algorithms/math/StatisticsPropertyTest.java b/src/test/java/io/github/diegourban/algorithms/math/StatisticsPropertyTest.java index c3f7596..de59fb2 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/StatisticsPropertyTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/StatisticsPropertyTest.java @@ -1,20 +1,20 @@ package io.github.diegourban.algorithms.math; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; + +import java.util.Arrays; +import java.util.List; import net.jqwik.api.ForAll; import net.jqwik.api.Property; import net.jqwik.api.constraints.DoubleRange; import net.jqwik.api.constraints.Size; -import java.util.Arrays; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.within; - class StatisticsPropertyTest { @Property - void meanLiesBetweenMinAndMax(@ForAll @Size(min = 1, max = 50) List<@DoubleRange(min = -1e6, max = 1e6) Double> values) { + void meanLiesBetweenMinAndMax( + @ForAll @Size(min = 1, max = 50) List<@DoubleRange(min = -1e6, max = 1e6) Double> values) { var data = values.stream().mapToDouble(Double::doubleValue).toArray(); var min = Arrays.stream(data).min().orElseThrow(); var max = Arrays.stream(data).max().orElseThrow(); @@ -23,7 +23,8 @@ void meanLiesBetweenMinAndMax(@ForAll @Size(min = 1, max = 50) List<@DoubleRange } @Property - void medianIsTheMiddleOfTheSortedData(@ForAll @Size(min = 1, max = 50) List<@DoubleRange(min = -1e6, max = 1e6) Double> values) { + void medianIsTheMiddleOfTheSortedData( + @ForAll @Size(min = 1, max = 50) List<@DoubleRange(min = -1e6, max = 1e6) Double> values) { var data = values.stream().mapToDouble(Double::doubleValue).toArray(); var sorted = data.clone(); Arrays.sort(sorted); diff --git a/src/test/java/io/github/diegourban/algorithms/math/StatisticsTest.java b/src/test/java/io/github/diegourban/algorithms/math/StatisticsTest.java index cdfeab2..69556e3 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/StatisticsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/StatisticsTest.java @@ -1,46 +1,50 @@ package io.github.diegourban.algorithms.math; - -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.within; +import org.junit.jupiter.api.Test; + class StatisticsTest { @Test void shouldCalculateMean() { - assertThat(Statistics.mean(new double[]{})).isCloseTo(0.0, within(0.1)); - assertThat(Statistics.mean(new double[]{1})).isCloseTo(1.0, within(0.1)); - assertThat(Statistics.mean(new double[]{1, 0})).isCloseTo(0.5, within(0.1)); - assertThat(Statistics.mean(new double[]{3, 2, 1})).isCloseTo(2.0, within(0.1)); - assertThat(Statistics.mean(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})).isCloseTo(5.5, within(0.1)); - assertThat(Statistics.mean(new double[]{2, 2, 3, 4, 14})).isCloseTo(5.0, within(0.1)); + assertThat(Statistics.mean(new double[] {})).isCloseTo(0.0, within(0.1)); + assertThat(Statistics.mean(new double[] {1})).isCloseTo(1.0, within(0.1)); + assertThat(Statistics.mean(new double[] {1, 0})).isCloseTo(0.5, within(0.1)); + assertThat(Statistics.mean(new double[] {3, 2, 1})).isCloseTo(2.0, within(0.1)); + assertThat(Statistics.mean(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) + .isCloseTo(5.5, within(0.1)); + assertThat(Statistics.mean(new double[] {2, 2, 3, 4, 14})).isCloseTo(5.0, within(0.1)); } @Test void shouldCalculateMedian() { - assertThat(Statistics.median(new double[]{1})).isCloseTo(1.0, within(0.1)); - assertThat(Statistics.median(new double[]{1, 0})).isCloseTo(0.5, within(0.1)); - assertThat(Statistics.median(new double[]{3, 2, 1})).isCloseTo(2.0, within(0.1)); - assertThat(Statistics.median(new double[]{1, 2, 3})).isCloseTo(2.0, within(0.1)); - assertThat(Statistics.median(new double[]{2, 1, 3})).isCloseTo(2.0, within(0.1)); - assertThat(Statistics.median(new double[]{3, 1, 2})).isCloseTo(2.0, within(0.1)); - assertThat(Statistics.median(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})).isCloseTo(5.5, within(0.1)); - assertThat(Statistics.median(new double[]{2, 2, 3, 4, 14})).isCloseTo(3.0, within(0.1)); + assertThat(Statistics.median(new double[] {1})).isCloseTo(1.0, within(0.1)); + assertThat(Statistics.median(new double[] {1, 0})).isCloseTo(0.5, within(0.1)); + assertThat(Statistics.median(new double[] {3, 2, 1})).isCloseTo(2.0, within(0.1)); + assertThat(Statistics.median(new double[] {1, 2, 3})).isCloseTo(2.0, within(0.1)); + assertThat(Statistics.median(new double[] {2, 1, 3})).isCloseTo(2.0, within(0.1)); + assertThat(Statistics.median(new double[] {3, 1, 2})).isCloseTo(2.0, within(0.1)); + assertThat(Statistics.median(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) + .isCloseTo(5.5, within(0.1)); + assertThat(Statistics.median(new double[] {2, 2, 3, 4, 14})).isCloseTo(3.0, within(0.1)); } @Test void shouldCalculateMode() { - assertThat(Statistics.mode(new double[]{1})).isEqualTo(1.0); - assertThat(Statistics.mode(new double[]{1, 0, 1})).isEqualTo(1.0); - assertThat(Statistics.mode(new double[]{1, 2, 2, 3, 3, 3, 4, 4, 5, 4, 4})).isEqualTo(4.0); - assertThat(Statistics.mode(new double[]{1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 4, 4, 5})).isEqualTo(5.0); - assertThat(Statistics.mode(new double[]{2, 2, 3, 4, 14})).isEqualTo(2.0); + assertThat(Statistics.mode(new double[] {1})).isEqualTo(1.0); + assertThat(Statistics.mode(new double[] {1, 0, 1})).isEqualTo(1.0); + assertThat(Statistics.mode(new double[] {1, 2, 2, 3, 3, 3, 4, 4, 5, 4, 4})) + .isEqualTo(4.0); + assertThat(Statistics.mode(new double[] {1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 4, 4, 5})) + .isEqualTo(5.0); + assertThat(Statistics.mode(new double[] {2, 2, 3, 4, 14})).isEqualTo(2.0); } @Test void shouldCalculateAverageAbsoluteDeviation() { - assertThat(Statistics.meanAbsoluteDeviation(new double[]{2, 2, 3, 4, 14})).isCloseTo(3.6, within(0.1)); + assertThat(Statistics.meanAbsoluteDeviation(new double[] {2, 2, 3, 4, 14})) + .isCloseTo(3.6, within(0.1)); } } diff --git a/src/test/java/io/github/diegourban/algorithms/math/TriangleTypeTest.java b/src/test/java/io/github/diegourban/algorithms/math/TriangleTypeTest.java index 2c375d6..b1f8a00 100644 --- a/src/test/java/io/github/diegourban/algorithms/math/TriangleTypeTest.java +++ b/src/test/java/io/github/diegourban/algorithms/math/TriangleTypeTest.java @@ -1,18 +1,14 @@ package io.github.diegourban.algorithms.math; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import static org.assertj.core.api.Assertions.assertThat; - class TriangleTypeTest { @ParameterizedTest - @CsvSource({ - "EQUILATERAL, Equilatero", - "ISOSCELES, Isoceles", - "ESCALENE, Escaleno" - }) + @CsvSource({"EQUILATERAL, Equilatero", "ISOSCELES, Isoceles", "ESCALENE, Escaleno"}) void exposesDisplayName(TriangleType type, String expectedName) { assertThat(type.getName()).isEqualTo(expectedName); } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/bit/DeltaBitsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/bit/DeltaBitsTest.java index f669349..8fa214f 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/bit/DeltaBitsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/bit/DeltaBitsTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.problems.bit; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import static org.assertj.core.api.Assertions.assertThat; - /** * Delta Bits — Codewars (5 kyu). *

@@ -18,15 +18,15 @@ static int solve(int a, int b) { @ParameterizedTest @CsvSource({ - "0, 0, 0", - "0, 1, 1", - "0, 3, 2", - "0, 7, 3", - "0, 10, 2", - "31, 14, 2", - "31, 31, 0", - "7, 17, 3", - "330274332, 199260599, 19", + "0, 0, 0", + "0, 1, 1", + "0, 3, 2", + "0, 7, 3", + "0, 10, 2", + "31, 14, 2", + "31, 31, 0", + "7, 17, 3", + "330274332, 199260599, 19", }) void countsDifferingBits(int a, int b, int expected) { assertThat(solve(a, b)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/bit/FindUniqueIdTest.java b/src/test/java/io/github/diegourban/algorithms/problems/bit/FindUniqueIdTest.java index 81ce5ec..5835edc 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/bit/FindUniqueIdTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/bit/FindUniqueIdTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.bit; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * The Stolen Breakfast Drone — Interview Cake. *

@@ -23,16 +23,16 @@ static int solve(int[] ids) { @Test void findsUniqueIdAtTheStart() { - assertThat(solve(new int[]{1, 2, 2})).isOne(); + assertThat(solve(new int[] {1, 2, 2})).isOne(); } @Test void findsUniqueIdInTheMiddle() { - assertThat(solve(new int[]{1, 2, 1})).isEqualTo(2); + assertThat(solve(new int[] {1, 2, 1})).isEqualTo(2); } @Test void findsUniqueIdAmongManyPairs() { - assertThat(solve(new int[]{1, 5, 8, 2, 4, 9, 9, 8, 4, 1, 2})).isEqualTo(5); + assertThat(solve(new int[] {1, 5, 8, 2, 4, 9, 9, 8, 4, 1, 2})).isEqualTo(5); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/bit/ParityTest.java b/src/test/java/io/github/diegourban/algorithms/problems/bit/ParityTest.java index a082ce7..f090e11 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/bit/ParityTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/bit/ParityTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.bit; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Parity — beecrowd (formerly URI Online Judge) 2176. *

@@ -41,7 +41,8 @@ void oddCountOfOnesAppendsOne() { @Test void handlesHundredBits() { - var bits = "0101011001101010011001010101001010101010101010101011010010101010101011010110101010000101010101010101"; + var bits = + "0101011001101010011001010101001010101010101010101011010010101010101011010110101010000101010101010101"; assertThat(solve(bits)).isEqualTo(bits + "1"); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/bit/ToCarryOrNotToCarryTest.java b/src/test/java/io/github/diegourban/algorithms/problems/bit/ToCarryOrNotToCarryTest.java index ec32db0..7720389 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/bit/ToCarryOrNotToCarryTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/bit/ToCarryOrNotToCarryTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.bit; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.EOFException; import java.io.IOException; import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * To Carry or not to Carry — beecrowd (formerly URI Online Judge) 1026. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/EqualSidesOfAnArrayTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/EqualSidesOfAnArrayTest.java index 020a2c3..d47f364 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/EqualSidesOfAnArrayTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/EqualSidesOfAnArrayTest.java @@ -1,10 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.array; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Equal Sides Of An Array — Codewars (6 kyu). @@ -28,43 +27,43 @@ static int solve(int[] arr) { @Test void findsMiddleOfUniformArray() { - assertThat(solve(new int[]{1, 1, 1})).isEqualTo(1); + assertThat(solve(new int[] {1, 1, 1})).isEqualTo(1); } @Test void returnsMinusOneWhenNoIndexBalances() { - assertThat(solve(new int[]{1, 1, 2})).isEqualTo(-1); - assertThat(solve(new int[]{1, 1})).isEqualTo(-1); + assertThat(solve(new int[] {1, 1, 2})).isEqualTo(-1); + assertThat(solve(new int[] {1, 1})).isEqualTo(-1); } @Test void returnsMinusOneForEmptyArray() { - assertThat(solve(new int[]{})).isEqualTo(-1); + assertThat(solve(new int[] {})).isEqualTo(-1); } @Test void balancesSymmetricSides() { - assertThat(solve(new int[]{2, 50, 3, 50, 2})).isEqualTo(2); + assertThat(solve(new int[] {2, 50, 3, 50, 2})).isEqualTo(2); } @Test void balancesAsymmetricSidesWithEqualSums() { - assertThat(solve(new int[]{20, 30, 3, 48, 2})).isEqualTo(2); + assertThat(solve(new int[] {20, 30, 3, 48, 2})).isEqualTo(2); } @Test void firstIndexBalancesWhenTheRestSumsToZero() { - assertThat(solve(new int[]{20, 10, -80, 10, 10, 15, 35})).isEqualTo(0); + assertThat(solve(new int[] {20, 10, -80, 10, 10, 15, 35})).isEqualTo(0); } @Test void lastIndexBalancesWhenTheRestSumsToZero() { - assertThat(solve(new int[]{1, -1, 5})).isEqualTo(2); + assertThat(solve(new int[] {1, -1, 5})).isEqualTo(2); } @Test void handlesNegativeValues() { - assertThat(solve(new int[]{20, -30, 3, -30, 20})).isEqualTo(2); - assertThat(solve(new int[]{1, 2, 3, -30, 3, -30, 6})).isEqualTo(4); + assertThat(solve(new int[] {20, -30, 3, -30, 20})).isEqualTo(2); + assertThat(solve(new int[] {1, 2, 3, -30, 3, -30, 6})).isEqualTo(4); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/LargestDifferenceTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/LargestDifferenceTest.java index 64a176c..41b44c5 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/LargestDifferenceTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/LargestDifferenceTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.array; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Largest Difference in Increasing Indexes — Codewars (5 kyu). *

@@ -29,29 +29,29 @@ static int solve(int[] data) { @Test void singleElementHasNoDifference() { - assertThat(solve(new int[]{0})).isZero(); + assertThat(solve(new int[] {0})).isZero(); } @Test void strictlyDecreasingHasNoDifference() { - assertThat(solve(new int[]{2, 1})).isZero(); - assertThat(solve(new int[]{3, 2, 1})).isZero(); + assertThat(solve(new int[] {2, 1})).isZero(); + assertThat(solve(new int[] {3, 2, 1})).isZero(); } @Test void equalValuesCount() { - assertThat(solve(new int[]{1, 1})).isEqualTo(1); - assertThat(solve(new int[]{1, 1, 1})).isEqualTo(2); + assertThat(solve(new int[] {1, 1})).isEqualTo(1); + assertThat(solve(new int[] {1, 1, 1})).isEqualTo(2); } @Test void increasingPairSpansTheArray() { - assertThat(solve(new int[]{1, 2})).isEqualTo(1); - assertThat(solve(new int[]{1, 1, 3})).isEqualTo(2); + assertThat(solve(new int[] {1, 2})).isEqualTo(1); + assertThat(solve(new int[] {1, 1, 3})).isEqualTo(2); } @Test void findsWidestPairInMixedData() { - assertThat(solve(new int[]{9, 4, 1, 10, 3, 4, 0, -1, -2})).isEqualTo(4); + assertThat(solve(new int[] {9, 4, 1, 10, 3, 4, 0, -1, -2})).isEqualTo(4); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/SingleRiffleCheckTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/SingleRiffleCheckTest.java index e32f4f2..6eea47e 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/SingleRiffleCheckTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/SingleRiffleCheckTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.array; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Single Riffle Check — Interview Cake. *

@@ -31,36 +31,40 @@ static boolean solve(int[] half1, int[] half2, int[] deck) { @Test void interleavedHalvesAreARiffle() { - assertThat(solve(new int[]{1, 3, 5}, new int[]{2, 4, 6}, new int[]{1, 2, 3, 4, 5, 6})).isTrue(); + assertThat(solve(new int[] {1, 3, 5}, new int[] {2, 4, 6}, new int[] {1, 2, 3, 4, 5, 6})) + .isTrue(); } @Test void oneHalfThenTheOtherIsARiffle() { - assertThat(solve(new int[]{1, 2}, new int[]{3, 4}, new int[]{1, 2, 3, 4})).isTrue(); + assertThat(solve(new int[] {1, 2}, new int[] {3, 4}, new int[] {1, 2, 3, 4})) + .isTrue(); } @Test void unevenInterleavingIsARiffle() { - assertThat(solve(new int[]{1, 3, 5}, new int[]{2, 4, 6}, new int[]{1, 3, 2, 5, 4, 6})).isTrue(); + assertThat(solve(new int[] {1, 3, 5}, new int[] {2, 4, 6}, new int[] {1, 3, 2, 5, 4, 6})) + .isTrue(); } @Test void cardOutOfOrderWithinItsHalfIsNotARiffle() { - assertThat(solve(new int[]{1, 3, 5}, new int[]{2, 4, 6}, new int[]{3, 1, 2, 4, 5, 6})).isFalse(); + assertThat(solve(new int[] {1, 3, 5}, new int[] {2, 4, 6}, new int[] {3, 1, 2, 4, 5, 6})) + .isFalse(); } @Test void unknownCardIsNotARiffle() { - assertThat(solve(new int[]{1}, new int[]{2}, new int[]{1, 7})).isFalse(); + assertThat(solve(new int[] {1}, new int[] {2}, new int[] {1, 7})).isFalse(); } @Test void missingCardsAreNotARiffle() { - assertThat(solve(new int[]{1, 3}, new int[]{2}, new int[]{1, 2})).isFalse(); + assertThat(solve(new int[] {1, 3}, new int[] {2}, new int[] {1, 2})).isFalse(); } @Test void emptyDeckOfEmptyHalvesIsARiffle() { - assertThat(solve(new int[]{}, new int[]{}, new int[]{})).isTrue(); + assertThat(solve(new int[] {}, new int[] {}, new int[] {})).isTrue(); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/TheForceAwakensTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/TheForceAwakensTest.java index 27c66fb..81287cd 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/TheForceAwakensTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/array/TheForceAwakensTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.datastructure.array; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.IOException; import java.io.InputStream; import java.util.Optional; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * The Force Awakens — beecrowd (formerly URI Online Judge) 2163. @@ -26,8 +25,7 @@ class TheForceAwakensTest { private static final int GUARD = 7; /** A 1-based terrain position. */ - record Position(int row, int column) { - } + record Position(int row, int column) {} static Optional solve(int[][] terrain) { for (var i = 1; i < terrain.length - 1; i++) { @@ -68,9 +66,9 @@ static String run(InputStream in) throws IOException { @Test void findsTheGuardedLightsaber() { int[][] terrain = { - {7, 7, 7, 1}, - {7, 42, 7, 1}, - {7, 7, 7, 1}, + {7, 7, 7, 1}, + {7, 42, 7, 1}, + {7, 7, 7, 1}, }; assertThat(solve(terrain)).contains(new Position(2, 2)); } @@ -78,9 +76,9 @@ void findsTheGuardedLightsaber() { @Test void ignoresLightsaberMissingAGuard() { int[][] terrain = { - {7, 7, 7}, - {7, 42, 7}, - {7, 7, 0}, + {7, 7, 7}, + {7, 42, 7}, + {7, 7, 0}, }; assertThat(solve(terrain)).isEmpty(); } @@ -88,9 +86,9 @@ void ignoresLightsaberMissingAGuard() { @Test void ignoresLightsaberOnTheBorder() { int[][] terrain = { - {42, 7, 7}, - {7, 7, 7}, - {7, 7, 7}, + {42, 7, 7}, + {7, 7, 7}, + {7, 7, 7}, }; assertThat(solve(terrain)).isEmpty(); } @@ -98,9 +96,9 @@ void ignoresLightsaberOnTheBorder() { @Test void skipsUnguardedCandidateAndFindsTheNextOne() { int[][] terrain = { - {7, 7, 7, 7, 7}, - {0, 42, 7, 42, 7}, - {7, 7, 7, 7, 7}, + {7, 7, 7, 7, 7}, + {0, 42, 7, 42, 7}, + {7, 7, 7, 7, 7}, }; assertThat(solve(terrain)).contains(new Position(2, 4)); } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/GraphColoringTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/GraphColoringTest.java index 75ceeb7..4f265dc 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/GraphColoringTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/GraphColoringTest.java @@ -1,13 +1,12 @@ package io.github.diegourban.algorithms.problems.datastructure.graph; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import java.util.HashSet; import java.util.List; import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import org.junit.jupiter.api.Test; /** * Graph Coloring — Interview Cake. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/MeshMessageTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/MeshMessageTest.java index ba46eb0..e692e1a 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/MeshMessageTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/graph/MeshMessageTest.java @@ -1,15 +1,14 @@ package io.github.diegourban.algorithms.problems.datastructure.graph; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import org.junit.jupiter.api.Test; /** * MeshMessage — Interview Cake. @@ -59,14 +58,14 @@ private static String[] path(Map reachedFrom, String end) { @Test void unknownSenderIsRejected() { - var graph = Map.of("Foo", new String[]{"A", "B"}); + var graph = Map.of("Foo", new String[] {"A", "B"}); assertThatIllegalArgumentException().isThrownBy(() -> solve(graph, "Bar", "Baz")); } @Test void unknownRecipientIsRejected() { - var graph = Map.of("Foo", new String[]{"A", "B"}); + var graph = Map.of("Foo", new String[] {"A", "B"}); assertThatIllegalArgumentException().isThrownBy(() -> solve(graph, "Foo", "Baz")); } @@ -74,10 +73,10 @@ void unknownRecipientIsRejected() { @Test void disconnectedRecipientHasNoPath() { var graph = Map.of( - "Foo", new String[]{"Bar"}, - "Bar", new String[]{"Foo"}, - "Baz", new String[]{"Asdf"}, - "Asdf", new String[]{"Baz"}); + "Foo", new String[] {"Bar"}, + "Bar", new String[] {"Foo"}, + "Baz", new String[] {"Asdf"}, + "Asdf", new String[] {"Baz"}); assertThat(solve(graph, "Foo", "Asdf")).isNull(); } @@ -85,10 +84,10 @@ void disconnectedRecipientHasNoPath() { @Test void loopsDoNotPreventTermination() { var graph = Map.of( - "Foo", new String[]{"Asdf", "Bar"}, - "Asdf", new String[]{"Foo", "Bar"}, - "Bar", new String[]{"Foo", "Asdf", "Baz"}, - "Baz", new String[]{"Bar"}); + "Foo", new String[] {"Asdf", "Bar"}, + "Asdf", new String[] {"Foo", "Bar"}, + "Bar", new String[] {"Foo", "Asdf", "Baz"}, + "Baz", new String[] {"Bar"}); assertThat(solve(graph, "Foo", "Baz")).containsExactly("Foo", "Bar", "Baz"); } @@ -96,20 +95,20 @@ void loopsDoNotPreventTermination() { @Test void findsShortestRoute() { var graph = Map.of( - "Min", new String[]{"William", "Jayden"}, - "William", new String[]{"Min"}, - "Jayden", new String[]{"Min", "Amelia", "Ren"}, - "Ren", new String[]{"Jayden"}, - "Amelia", new String[]{"Jayden", "Adam", "Miguel"}, - "Adam", new String[]{"Amelia", "Miguel"}, - "Miguel", new String[]{"Amelia", "Adam"}); + "Min", new String[] {"William", "Jayden"}, + "William", new String[] {"Min"}, + "Jayden", new String[] {"Min", "Amelia", "Ren"}, + "Ren", new String[] {"Jayden"}, + "Amelia", new String[] {"Jayden", "Adam", "Miguel"}, + "Adam", new String[] {"Amelia", "Miguel"}, + "Miguel", new String[] {"Amelia", "Adam"}); assertThat(solve(graph, "Min", "Adam")).containsExactly("Min", "Jayden", "Amelia", "Adam"); } @Test void senderIsRecipient() { - var graph = Map.of("Foo", new String[]{}); + var graph = Map.of("Foo", new String[] {}); assertThat(solve(graph, "Foo", "Foo")).containsExactly("Foo"); } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/DeleteNodeTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/DeleteNodeTest.java index 2d5b484..3689828 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/DeleteNodeTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/DeleteNodeTest.java @@ -1,12 +1,12 @@ package io.github.diegourban.algorithms.problems.datastructure.linkedlist; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + import io.github.diegourban.algorithms.datastructure.Element; import io.github.diegourban.algorithms.datastructure.linkedlist.LinkedListNode; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; - /** * Delete Node — Interview Cake. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/FindRepeatBeastModeTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/FindRepeatBeastModeTest.java index 8658769..f10ceec 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/FindRepeatBeastModeTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/FindRepeatBeastModeTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.linkedlist; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Find Repeat, Space Edition BEAST MODE — Interview Cake. *

@@ -24,7 +24,9 @@ static int solve(int[] numbers) { } var cycleLength = 1; - for (var position = next(numbers, positionInCycle); position != positionInCycle; position = next(numbers, position)) { + for (var position = next(numbers, positionInCycle); + position != positionInCycle; + position = next(numbers, position)) { cycleLength++; } @@ -46,31 +48,31 @@ private static int next(int[] numbers, int position) { @Test void smallestInputsRepeatOne() { - assertThat(solve(new int[]{1, 1})).isOne(); - assertThat(solve(new int[]{1, 1, 1})).isOne(); + assertThat(solve(new int[] {1, 1})).isOne(); + assertThat(solve(new int[] {1, 1, 1})).isOne(); } @Test void findsAdjacentRepeatAnywhereInTheRange() { - assertThat(solve(new int[]{1, 1, 2, 3, 4, 5, 6, 7, 8, 9})).isOne(); - assertThat(solve(new int[]{1, 2, 2, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(2); - assertThat(solve(new int[]{1, 2, 3, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(3); - assertThat(solve(new int[]{1, 2, 3, 4, 4, 5, 6, 7, 8, 9})).isEqualTo(4); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 5, 6, 7, 8, 9})).isEqualTo(5); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 6, 7, 8, 9})).isEqualTo(6); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 7, 8, 9})).isEqualTo(7); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 8, 9})).isEqualTo(8); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 9})).isEqualTo(9); + assertThat(solve(new int[] {1, 1, 2, 3, 4, 5, 6, 7, 8, 9})).isOne(); + assertThat(solve(new int[] {1, 2, 2, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(2); + assertThat(solve(new int[] {1, 2, 3, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(3); + assertThat(solve(new int[] {1, 2, 3, 4, 4, 5, 6, 7, 8, 9})).isEqualTo(4); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 5, 6, 7, 8, 9})).isEqualTo(5); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 6, 7, 8, 9})).isEqualTo(6); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 7, 8, 9})).isEqualTo(7); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 8, 9})).isEqualTo(8); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 9})).isEqualTo(9); } @Test void findsRepeatRegardlessOfPosition() { - assertThat(solve(new int[]{1, 2, 1, 3, 4, 5, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 1, 4, 5, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 1, 5, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 1, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 1, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 1, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 1})).isOne(); + assertThat(solve(new int[] {1, 2, 1, 3, 4, 5, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 1, 4, 5, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 1, 5, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 1, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 1, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 1, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 1})).isOne(); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/LinkedListCycleTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/LinkedListCycleTest.java index 0c26ea5..ec88c5c 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/LinkedListCycleTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/LinkedListCycleTest.java @@ -1,11 +1,11 @@ package io.github.diegourban.algorithms.problems.datastructure.linkedlist; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.datastructure.Element; import io.github.diegourban.algorithms.datastructure.linkedlist.LinkedListNode; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - /** * Does This Linked List Have A Cycle? — Interview Cake. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/MergeSortedLinkedListsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/MergeSortedLinkedListsTest.java index 5b4dac7..354d948 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/MergeSortedLinkedListsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/MergeSortedLinkedListsTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.datastructure.linkedlist; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Merge two sorted linked lists — HackerRank. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/ReverseLinkedListTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/ReverseLinkedListTest.java index 481d141..9823b50 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/ReverseLinkedListTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/linkedlist/ReverseLinkedListTest.java @@ -1,12 +1,12 @@ package io.github.diegourban.algorithms.problems.datastructure.linkedlist; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.datastructure.Element; import io.github.diegourban.algorithms.datastructure.linkedlist.LinkedList; import io.github.diegourban.algorithms.datastructure.linkedlist.LinkedListNode; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - /** * Reverse A Linked List — Interview Cake. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/DroughtTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/DroughtTest.java index 97ecfbf..c881edf 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/DroughtTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/DroughtTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.datastructure.map; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; @@ -16,9 +15,9 @@ import java.util.SortedMap; import java.util.TreeMap; import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Drought — beecrowd (formerly URI Online Judge) 1023. @@ -30,15 +29,13 @@ */ class DroughtTest { - record Property(int residents, int consumption) { - } + record Property(int residents, int consumption) {} /** * @param residentsByConsumption residents per per-capita consumption, ascending by consumption * @param averageConsumption total consumption over total residents, truncated to 2 decimals */ - record City(SortedMap residentsByConsumption, BigDecimal averageConsumption) { - } + record City(SortedMap residentsByConsumption, BigDecimal averageConsumption) {} static City solve(List properties) { var residentsByConsumption = new TreeMap(); @@ -47,10 +44,11 @@ static City solve(List properties) { for (var property : properties) { totalResidents += property.residents(); totalConsumption += property.consumption(); - residentsByConsumption.merge(property.consumption() / property.residents(), property.residents(), Integer::sum); + residentsByConsumption.merge( + property.consumption() / property.residents(), property.residents(), Integer::sum); } - var average = BigDecimal.valueOf(totalConsumption) - .divide(BigDecimal.valueOf(totalResidents), 2, RoundingMode.DOWN); + var average = + BigDecimal.valueOf(totalConsumption).divide(BigDecimal.valueOf(totalResidents), 2, RoundingMode.DOWN); return new City(residentsByConsumption, average); } @@ -70,7 +68,8 @@ static String run(InputStream in) throws IOException { Cidade# %d: %s Consumo medio: %s m3. - """.formatted(cities.size() + 1, groups, city.averageConsumption().toPlainString())); + """.formatted( + cities.size() + 1, groups, city.averageConsumption().toPlainString())); } return String.join("\n", cities); } @@ -92,8 +91,12 @@ void mergesPropertiesWithSamePerCapitaConsumption() { @Test void truncatesAverageInsteadOfRounding() { - var city = solve(List.of(new Property(1, 25), new Property(2, 20), new Property(3, 31), - new Property(2, 40), new Property(6, 70))); + var city = solve(List.of( + new Property(1, 25), + new Property(2, 20), + new Property(3, 31), + new Property(2, 40), + new Property(6, 70))); // 186 / 14 = 13.2857... assertThat(city.averageConsumption()).isEqualByComparingTo("13.28"); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/NumberFrequencyTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/NumberFrequencyTest.java index 8f66df6..8ef5617 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/NumberFrequencyTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/map/NumberFrequencyTest.java @@ -1,18 +1,17 @@ package io.github.diegourban.algorithms.problems.datastructure.map; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.IOException; import java.io.InputStream; import java.util.SortedMap; import java.util.TreeMap; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Number Frequence — beecrowd (formerly URI Online Judge) 1171. @@ -38,24 +37,26 @@ static String run(InputStream in) throws IOException { numbers[i] = reader.nextInt(); } var out = new StringBuilder(); - solve(numbers).forEach((x, count) -> out.append(x).append(" aparece ").append(count).append(" vez(es)\n")); + solve(numbers) + .forEach((x, count) -> + out.append(x).append(" aparece ").append(count).append(" vez(es)\n")); return out.toString(); } @Test void countsOccurrencesInAscendingOrder() { - assertThat(solve(new int[]{8, 10, 8, 260, 4, 10, 10})) + assertThat(solve(new int[] {8, 10, 8, 260, 4, 10, 10})) .containsExactly(entry(4, 1), entry(8, 2), entry(10, 3), entry(260, 1)); } @Test void singleValue() { - assertThat(solve(new int[]{5})).containsExactly(entry(5, 1)); + assertThat(solve(new int[] {5})).containsExactly(entry(5, 1)); } @Test void emptyInputHasNoFrequencies() { - assertThat(solve(new int[]{})).isEmpty(); + assertThat(solve(new int[] {})).isEmpty(); } @ParameterizedTest diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/queue/QueueTwoStacksTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/queue/QueueTwoStacksTest.java index db91c2d..407fa14 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/queue/QueueTwoStacksTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/queue/QueueTwoStacksTest.java @@ -1,13 +1,12 @@ package io.github.diegourban.algorithms.problems.datastructure.queue; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import java.util.ArrayDeque; import java.util.Deque; import java.util.NoSuchElementException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import org.junit.jupiter.api.Test; /** * Queue Two Stacks — Interview Cake. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/InflightEntertainmentTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/InflightEntertainmentTest.java index 27ff65d..a15ae82 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/InflightEntertainmentTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/InflightEntertainmentTest.java @@ -1,10 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.set; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.HashSet; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Inflight Entertainment — Interview Cake. @@ -29,22 +28,22 @@ static boolean solve(int[] movieLengths, int flightLength) { @Test void twoMoviesOfTheSameLengthFillTheFlight() { - assertThat(solve(new int[]{1, 1}, 2)).isTrue(); + assertThat(solve(new int[] {1, 1}, 2)).isTrue(); } @Test void twoDifferentMoviesFillTheFlight() { - assertThat(solve(new int[]{1, 1, 2}, 3)).isTrue(); + assertThat(solve(new int[] {1, 1, 2}, 3)).isTrue(); } @Test void noPairAddsUpToTheFlight() { - assertThat(solve(new int[]{1, 1}, 3)).isFalse(); - assertThat(solve(new int[]{2, 3}, 2)).isFalse(); + assertThat(solve(new int[] {1, 1}, 3)).isFalse(); + assertThat(solve(new int[] {2, 3}, 2)).isFalse(); } @Test void sameMovieCannotBeWatchedTwice() { - assertThat(solve(new int[]{2, 3}, 4)).isFalse(); + assertThat(solve(new int[] {2, 3}, 4)).isFalse(); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/PermutationPalindromeTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/PermutationPalindromeTest.java index 1bc8d9c..807522e 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/PermutationPalindromeTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/PermutationPalindromeTest.java @@ -1,10 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.set; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.HashSet; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Permutation Palindrome — Interview Cake. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/TwiceLinearTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/TwiceLinearTest.java index f5bb4bb..5c3b4c7 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/TwiceLinearTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/set/TwiceLinearTest.java @@ -1,10 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.set; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.TreeSet; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Twice Linear — Codewars (5 kyu). diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/BracketValidatorTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/BracketValidatorTest.java index 5fc9dc0..8e3ec8c 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/BracketValidatorTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/BracketValidatorTest.java @@ -1,10 +1,9 @@ package io.github.diegourban.algorithms.problems.datastructure.stack; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayDeque; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Bracket Validator — Interview Cake. @@ -26,7 +25,7 @@ static boolean solve(String code) { return false; } } - default -> { } + default -> {} } } return openers.isEmpty(); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/DiamondsAndSandTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/DiamondsAndSandTest.java index 294cef1..f96021d 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/DiamondsAndSandTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/DiamondsAndSandTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.datastructure.stack; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; +import java.io.IOException; +import java.io.InputStream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Diamonds and Sand — beecrowd (formerly URI Online Judge) 1069. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/LargestStackTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/LargestStackTest.java index 9f180f0..8865c4d 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/LargestStackTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/LargestStackTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.datastructure.stack; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayDeque; import java.util.Deque; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Largest Stack — Interview Cake. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/ParenthesisBalanceTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/ParenthesisBalanceTest.java index 9e8d315..7a335a8 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/ParenthesisBalanceTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/stack/ParenthesisBalanceTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.datastructure.stack; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.EOFException; import java.io.IOException; import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Parenthesis Balance I — beecrowd (formerly URI Online Judge) 1068. @@ -32,7 +31,7 @@ static boolean solve(String expression) { return false; } } - default -> { } + default -> {} } } return depth == 0; diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BalancedBinaryTreeTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BalancedBinaryTreeTest.java index 0bf010c..b8be54c 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BalancedBinaryTreeTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BalancedBinaryTreeTest.java @@ -1,12 +1,11 @@ package io.github.diegourban.algorithms.problems.datastructure.tree; -import io.github.diegourban.algorithms.datastructure.binarytree.BinaryTreeNode; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import io.github.diegourban.algorithms.datastructure.binarytree.BinaryTreeNode; import java.util.ArrayDeque; import java.util.ArrayList; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Balanced Binary Tree — Interview Cake. @@ -18,8 +17,7 @@ */ class BalancedBinaryTreeTest { - private record NodeDepth(BinaryTreeNode node, int depth) { - } + private record NodeDepth(BinaryTreeNode node, int depth) {} static boolean solve(BinaryTreeNode root) { if (root == null) { diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BinarySearchTreeCheckerTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BinarySearchTreeCheckerTest.java index c5f7759..7f3cdbe 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BinarySearchTreeCheckerTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/BinarySearchTreeCheckerTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.datastructure.tree; -import io.github.diegourban.algorithms.datastructure.binarytree.BinaryTreeNode; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import io.github.diegourban.algorithms.datastructure.binarytree.BinaryTreeNode; import java.util.ArrayDeque; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Binary Search Tree Checker — Interview Cake. @@ -17,8 +16,7 @@ */ class BinarySearchTreeCheckerTest { - private record NodeBounds(BinaryTreeNode node, long lower, long upper) { - } + private record NodeBounds(BinaryTreeNode node, long lower, long upper) {} static boolean solve(BinaryTreeNode root) { if (root == null) { diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/SecondLargestItemInBstTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/SecondLargestItemInBstTest.java index a11ddf9..f202912 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/SecondLargestItemInBstTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/tree/SecondLargestItemInBstTest.java @@ -1,11 +1,11 @@ package io.github.diegourban.algorithms.problems.datastructure.tree; -import io.github.diegourban.algorithms.datastructure.binarytree.BinaryTreeNode; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import io.github.diegourban.algorithms.datastructure.binarytree.BinaryTreeNode; +import org.junit.jupiter.api.Test; + /** * Find The Second Largest Item In A Binary Search Tree — Interview Cake. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/trie/MillionGazillionTest.java b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/trie/MillionGazillionTest.java index 5072914..f631e2b 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/datastructure/trie/MillionGazillionTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/datastructure/trie/MillionGazillionTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.datastructure.trie; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.HashMap; import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * MillionGazillion — Interview Cake. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/dp/CakeThiefTest.java b/src/test/java/io/github/diegourban/algorithms/problems/dp/CakeThiefTest.java index 8e76f74..c76c087 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/dp/CakeThiefTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/dp/CakeThiefTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.problems.dp; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import org.junit.jupiter.api.Test; + /** * The Cake Thief — Interview Cake. *

@@ -16,8 +16,7 @@ */ class CakeThiefTest { - record Box(int weight, int value) { - } + record Box(int weight, int value) {} static class InfinityException extends RuntimeException { InfinityException() { @@ -45,14 +44,14 @@ static long solve(Box[] boxes, int capacity) { @Test void takesAsManyOfTheBestBoxesAsFit() { - var boxes = new Box[]{new Box(1, 3), new Box(2, 2), new Box(4, 5)}; + var boxes = new Box[] {new Box(1, 3), new Box(2, 2), new Box(4, 5)}; assertThat(solve(boxes, 15)).isEqualTo(45L); } @Test void weightlessValuableBoxIsInfinite() { - var boxes = new Box[]{new Box(0, 1), new Box(2, 2)}; + var boxes = new Box[] {new Box(0, 1), new Box(2, 2)}; assertThatExceptionOfType(InfinityException.class).isThrownBy(() -> solve(boxes, 5)); } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/greedy/BanknotesAndCoinsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/greedy/BanknotesAndCoinsTest.java index 2a53d68..c5a5236 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/greedy/BanknotesAndCoinsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/greedy/BanknotesAndCoinsTest.java @@ -1,13 +1,12 @@ package io.github.diegourban.algorithms.problems.greedy; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; +import static org.assertj.core.api.Assertions.assertThat; import java.math.BigDecimal; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; /** * Banknotes and Coins — beecrowd (formerly URI Online Judge) 1021. @@ -23,16 +22,16 @@ class BanknotesAndCoinsTest { static final List COIN_CENTS = List.of(100, 50, 25, 10, 5, 1); /** Counts per denomination, aligned with {@link #BANKNOTE_CENTS} and {@link #COIN_CENTS}. */ - record Breakdown(List banknotes, List coins) { - } + record Breakdown(List banknotes, List coins) {} static int toCents(String amount) { return new BigDecimal(amount).movePointRight(2).intValueExact(); } static Breakdown solve(int cents) { - var remaining = new int[]{cents}; - var banknotes = BANKNOTE_CENTS.stream().map(value -> take(remaining, value)).toList(); + var remaining = new int[] {cents}; + var banknotes = + BANKNOTE_CENTS.stream().map(value -> take(remaining, value)).toList(); var coins = COIN_CENTS.stream().map(value -> take(remaining, value)).toList(); return new Breakdown(banknotes, coins); } @@ -45,11 +44,11 @@ private static int take(int[] remaining, int value) { @ParameterizedTest @CsvSource({ - "576.73, 57673", - "4.00, 400", - "91.01, 9101", - "0.01, 1", - "1000000.00, 100000000", + "576.73, 57673", + "4.00, 400", + "91.01, 9101", + "0.01, 1", + "1000000.00, 100000000", }) void parsesAmountToCentsExactly(String amount, int cents) { assertThat(toCents(amount)).isEqualTo(cents); @@ -87,7 +86,6 @@ void oneOfEachCoin() { @Test void zeroNeedsNothing() { - assertThat(solve(0)) - .isEqualTo(new Breakdown(List.of(0, 0, 0, 0, 0, 0), List.of(0, 0, 0, 0, 0, 0))); + assertThat(solve(0)).isEqualTo(new Breakdown(List.of(0, 0, 0, 0, 0, 0), List.of(0, 0, 0, 0, 0, 0))); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/CollectableCardsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/CollectableCardsTest.java index 46456ac..1ec7fc2 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/CollectableCardsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/CollectableCardsTest.java @@ -1,17 +1,16 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.math.NumberTheory; import io.github.diegourban.algorithms.testing.SampleLoader; +import java.io.IOException; +import java.io.InputStream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Collectable Cards — beecrowd (formerly URI Online Judge) 1028. *

@@ -40,12 +39,12 @@ static String run(InputStream in) throws IOException { @ParameterizedTest @CsvSource({ - "8, 12, 4", - "9, 27, 9", - "259, 111, 37", - "1, 1, 1", - "7, 13, 1", - "1000, 1000, 1000", + "8, 12, 4", + "9, 27, 9", + "259, 111, 37", + "1, 1, 1", + "7, 13, 1", + "1000, 1000, 1000", }) void largestPileSizeIsTheGcd(int f1, int f2, long expected) { assertThat(solve(f1, f2)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/CommonDenominatorsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/CommonDenominatorsTest.java index 8fa797e..0935916 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/CommonDenominatorsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/CommonDenominatorsTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.math; -import io.github.diegourban.algorithms.math.NumberTheory; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import io.github.diegourban.algorithms.math.NumberTheory; import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Common Denominators — Codewars (5 kyu). @@ -24,23 +23,27 @@ static String solve(long[][] fractions) { var lcm = NumberTheory.lcm(Arrays.stream(fractions).mapToLong(f -> f[1]).toArray()); var result = new StringBuilder(); for (var f : fractions) { - result.append('(').append(f[0] * (lcm / f[1])).append(',').append(lcm).append(')'); + result.append('(') + .append(f[0] * (lcm / f[1])) + .append(',') + .append(lcm) + .append(')'); } return result.toString(); } @Test void emptyListGivesEmptyString() { - assertThat(solve(new long[][]{})).isEmpty(); + assertThat(solve(new long[][] {})).isEmpty(); } @Test void unitFractions() { - assertThat(solve(new long[][]{{1, 2}, {1, 3}, {1, 4}})).isEqualTo("(6,12)(4,12)(3,12)"); + assertThat(solve(new long[][] {{1, 2}, {1, 3}, {1, 4}})).isEqualTo("(6,12)(4,12)(3,12)"); } @Test void coprimeDenominators() { - assertThat(solve(new long[][]{{1, 130}, {1, 131}, {1, 4}})).isEqualTo("(262,34060)(260,34060)(8515,34060)"); + assertThat(solve(new long[][] {{1, 130}, {1, 131}, {1, 4}})).isEqualTo("(262,34060)(260,34060)(8515,34060)"); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/DivisorsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/DivisorsTest.java index 2b141f5..5bf783c 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/DivisorsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/DivisorsTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.math; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Divisors I — beecrowd (formerly URI Online Judge) 1157. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/EasyLineTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/EasyLineTest.java index b09b2e7..0d4fb8f 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/EasyLineTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/EasyLineTest.java @@ -1,13 +1,12 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.math.Combinatorics; +import java.math.BigInteger; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import java.math.BigInteger; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Easy Line — Codewars (7 kyu). *

@@ -22,18 +21,18 @@ static BigInteger solve(int n) { @ParameterizedTest @CsvSource({ - "0, 1", - "1, 2", - "2, 6", - "3, 20", - "4, 70", - "5, 252", - "6, 924", - "7, 3432", - "13, 10400600", - "17, 2333606220", - "19, 35345263800", - "50, 100891344545564193334812497256", + "0, 1", + "1, 2", + "2, 6", + "3, 20", + "4, 70", + "5, 252", + "6, 924", + "7, 3432", + "13, 10400600", + "17, 2333606220", + "19, 35345263800", + "50, 100891344545564193334812497256", }) void sumsSquaresOfPascalRow(int n, BigInteger expected) { assertThat(solve(n)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/FactorialSumTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/FactorialSumTest.java index f2c6bbc..3c328ed 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/FactorialSumTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/FactorialSumTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.EOFException; import java.io.IOException; import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; /** * Factorial Sum — beecrowd (formerly URI Online Judge) 1161. @@ -50,12 +49,12 @@ static String run(InputStream in) throws IOException { @ParameterizedTest @CsvSource({ - "4, 4, 48", - "0, 0, 2", - "0, 2, 3", - "1, 2, 3", - "5, 0, 121", - "20, 20, 4865804016353280000", + "4, 4, 48", + "0, 0, 2", + "0, 2, 3", + "1, 2, 3", + "5, 0, 121", + "20, 20, 4865804016353280000", }) void sumsBothFactorials(int m, int n, long expected) { assertThat(solve(m, n)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/FibonacciArrayTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/FibonacciArrayTest.java index 2bcd9ca..7fd14da 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/FibonacciArrayTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/FibonacciArrayTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import static org.assertj.core.api.Assertions.assertThat; - /** * Fibonacci Array — beecrowd (formerly URI Online Judge) 1176. *

@@ -28,14 +28,14 @@ static long solve(int n) { @ParameterizedTest @CsvSource({ - "0, 0", - "1, 1", - "2, 1", - "4, 3", - "10, 55", - "46, 1836311903", - "47, 2971215073", - "60, 1548008755920", + "0, 0", + "1, 1", + "2, 1", + "4, 3", + "10, 55", + "46, 1836311903", + "47, 2971215073", + "60, 1548008755920", }) void nthFibonacci(int n, long expected) { assertThat(solve(n)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/PascalsTriangleTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/PascalsTriangleTest.java index 6faca70..18cf34c 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/PascalsTriangleTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/PascalsTriangleTest.java @@ -1,15 +1,14 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.math.Combinatorics; +import java.math.BigInteger; +import java.util.stream.IntStream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import java.math.BigInteger; -import java.util.stream.IntStream; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Pascal's Triangle — beecrowd (formerly URI Online Judge) 2232. *

@@ -25,11 +24,11 @@ static long solve(int n) { @ParameterizedTest @CsvSource({ - "1, 1", - "2, 3", - "5, 31", - "31, 2147483647", - "50, 1125899906842623", + "1, 1", + "2, 3", + "5, 31", + "31, 2147483647", + "50, 1125899906842623", }) void sumOfFirstRows(int n, long expected) { assertThat(solve(n)).isEqualTo(expected); @@ -40,7 +39,8 @@ void closedFormMatchesSummingTheBinomialCoefficients() { for (var n = 1; n <= 20; n++) { var sum = IntStream.range(0, n) .boxed() - .flatMap(row -> IntStream.rangeClosed(0, row).mapToObj(k -> Combinatorics.binomialCoefficient(row, k))) + .flatMap(row -> + IntStream.rangeClosed(0, row).mapToObj(k -> Combinatorics.binomialCoefficient(row, k))) .reduce(BigInteger.ZERO, BigInteger::add); assertThat(solve(n)).as("n = %d", n).isEqualTo(sum.longValueExact()); } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/PerfectNumberTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/PerfectNumberTest.java index 7ec21bd..a592201 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/PerfectNumberTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/PerfectNumberTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.assertj.core.api.Assertions.assertThat; - /** * Perfect Number — beecrowd (formerly URI Online Judge) 1164. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/PrefaceTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/PrefaceTest.java index b5d65fe..67cdda0 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/PrefaceTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/PrefaceTest.java @@ -1,12 +1,12 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; - /** * Preface — beecrowd (formerly URI Online Judge) 1837. *

@@ -17,8 +17,7 @@ */ class PrefaceTest { - record Division(int quotient, int remainder) { - } + record Division(int quotient, int remainder) {} static Division solve(int a, int b) { if (b == 0) { @@ -30,14 +29,14 @@ static Division solve(int a, int b) { @ParameterizedTest @CsvSource({ - "7, 3, 2, 1", - "-7, -3, 3, 2", - "-736, 539, -2, 342", - "62, -59, -1, 3", - "999, 999, 1, 0", - "-1000, -1000, 1, 0", - "-6, 3, -2, 0", - "0, -5, 0, 0", + "7, 3, 2, 1", + "-7, -3, 3, 2", + "-736, 539, -2, 342", + "62, -59, -1, 3", + "999, 999, 1, 0", + "-1000, -1000, 1, 0", + "-6, 3, -2, 0", + "0, -5, 0, 0", }) void remainderIsNeverNegative(int a, int b, int quotient, int remainder) { assertThat(solve(a, b)).isEqualTo(new Division(quotient, remainder)); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeGeneratorTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeGeneratorTest.java index cc2329d..198d9d6 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeGeneratorTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeGeneratorTest.java @@ -1,17 +1,16 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Prime Generator — SPOJ PRIME1. @@ -75,8 +74,9 @@ private static InputStream input(String text) { @Test void primesUpToOneHundred() { - assertThat(solve(1, 100)).containsExactly( - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); + assertThat(solve(1, 100)) + .containsExactly( + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); } @Test diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeNumberTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeNumberTest.java index 3a2a062..f56cdce 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeNumberTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/PrimeNumberTest.java @@ -1,11 +1,11 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.math.NumberTheory; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.assertj.core.api.Assertions.assertThat; - /** * Prime Number — beecrowd (formerly URI Online Judge) 1165. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/PrimesInNumbersTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/PrimesInNumbersTest.java index 30ee306..af543b0 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/PrimesInNumbersTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/PrimesInNumbersTest.java @@ -1,14 +1,13 @@ package io.github.diegourban.algorithms.problems.math; -import io.github.diegourban.algorithms.math.NumberTheory; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; +import static org.assertj.core.api.Assertions.assertThat; +import io.github.diegourban.algorithms.math.NumberTheory; import java.util.TreeMap; import java.util.function.Function; import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; /** * Primes in numbers — Codewars (5 kyu). @@ -34,20 +33,20 @@ static String solve(int n) { @ParameterizedTest @CsvSource({ - "2, (2)", - "3, (3)", - "4, (2**2)", - "5, (5)", - "6, (2)(3)", - "7, (7)", - "8, (2**3)", - "9, (3**2)", - "10, (2)(5)", - "11, (11)", - "12, (2**2)(3)", - "13, (13)", - "86240, (2**5)(5)(7**2)(11)", - "7775460, (2**2)(3**3)(5)(7)(11**2)(17)", + "2, (2)", + "3, (3)", + "4, (2**2)", + "5, (5)", + "6, (2)(3)", + "7, (7)", + "8, (2**3)", + "9, (3**2)", + "10, (2)(5)", + "11, (11)", + "12, (2**2)(3)", + "13, (13)", + "86240, (2**5)(5)(7**2)(11)", + "7775460, (2**2)(3**3)(5)(7)(11**2)(17)", }) void decomposesIntoPrimePowers(int n, String expected) { assertThat(solve(n)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/QueenTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/QueenTest.java index e68b188..3b1d553 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/QueenTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/QueenTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; +import java.io.IOException; +import java.io.InputStream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Queen — beecrowd (formerly URI Online Judge) 1087. *

@@ -50,14 +49,14 @@ static String run(InputStream in) throws IOException { @ParameterizedTest @CsvSource({ - "4, 4, 6, 2, 1", - "3, 5, 3, 5, 0", - "5, 5, 4, 3, 2", - "1, 1, 1, 8, 1", - "1, 1, 8, 1, 1", - "1, 1, 8, 8, 1", - "1, 8, 8, 1, 1", - "1, 1, 2, 3, 2", + "4, 4, 6, 2, 1", + "3, 5, 3, 5, 0", + "5, 5, 4, 3, 2", + "1, 1, 1, 8, 1", + "1, 1, 8, 1, 1", + "1, 1, 8, 8, 1", + "1, 8, 8, 1, 1", + "1, 1, 2, 3, 2", }) void minimumMoves(int x1, int y1, int x2, int y2, int expected) { assertThat(solve(x1, y1, x2, y2)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/RainfallTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/RainfallTest.java index f8d42df..5564bd5 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/RainfallTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/RainfallTest.java @@ -1,13 +1,12 @@ package io.github.diegourban.algorithms.problems.math; -import io.github.diegourban.algorithms.math.Statistics; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.within; +import io.github.diegourban.algorithms.math.Statistics; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + /** * Rainfall — Codewars (6 kyu). *

@@ -16,8 +15,7 @@ */ class RainfallTest { - record Rainfall(double mean, double variance) { - } + record Rainfall(double mean, double variance) {} static final Rainfall UNKNOWN = new Rainfall(-1, -1); @@ -30,7 +28,10 @@ static Rainfall solve(String town, String data) { .mapToDouble(month -> Double.parseDouble(month.split(" ")[1])) .toArray(); var mean = Statistics.mean(values); - var variance = Arrays.stream(values).map(v -> (v - mean) * (v - mean)).average().orElseThrow(); + var variance = Arrays.stream(values) + .map(v -> (v - mean) * (v - mean)) + .average() + .orElseThrow(); return new Rainfall(mean, variance); }) .orElse(UNKNOWN); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/RectangularLoveTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/RectangularLoveTest.java index 13db347..d32445a 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/RectangularLoveTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/RectangularLoveTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.math; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Rectangular Love — Interview Cake. *

@@ -15,11 +15,9 @@ class RectangularLoveTest { /** {@code (leftX, bottomY)} is the bottom-left corner. */ - record Rectangle(int leftX, int bottomY, int width, int height) { - } + record Rectangle(int leftX, int bottomY, int width, int height) {} - private record RangeOverlap(int startPoint, int length) { - } + private record RangeOverlap(int startPoint, int length) {} private static final Rectangle NO_OVERLAP = new Rectangle(0, 0, 0, 0); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/RomanNumeralsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/RomanNumeralsTest.java index 19968f7..1ede0f1 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/RomanNumeralsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/RomanNumeralsTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.problems.math; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import static org.assertj.core.api.Assertions.assertThat; - /** * Roman Numerals for Page Numbers — beecrowd (formerly URI Online Judge) 1960. *

@@ -32,18 +32,18 @@ static String solve(int n) { @ParameterizedTest @CsvSource({ - "666, DCLXVI", - "83, LXXXIII", - "1, I", - "5, V", - "10, X", - "50, L", - "100, C", - "500, D", - "4, IV", - "49, XLIX", - "444, CDXLIV", - "999, CMXCIX", + "666, DCLXVI", + "83, LXXXIII", + "1, I", + "5, V", + "10, X", + "50, L", + "100, C", + "500, D", + "4, IV", + "49, XLIX", + "444, CDXLIV", + "999, CMXCIX", }) void convertsToRoman(int n, String expected) { assertThat(solve(n)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/SumByFactorsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/SumByFactorsTest.java index 7dd6309..55906b2 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/SumByFactorsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/SumByFactorsTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.math; -import io.github.diegourban.algorithms.math.NumberTheory; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import io.github.diegourban.algorithms.math.NumberTheory; import java.util.TreeMap; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Sum by Factors — Codewars (4 kyu). @@ -25,22 +24,23 @@ static String solve(int[] numbers) { } } var result = new StringBuilder(); - sums.forEach((prime, sum) -> result.append('(').append(prime).append(' ').append(sum).append(')')); + sums.forEach((prime, sum) -> + result.append('(').append(prime).append(' ').append(sum).append(')')); return result.toString(); } @Test void sumsEachPrimeFactorInAscendingOrder() { - assertThat(solve(new int[]{12, 15})).isEqualTo("(2 12)(3 27)(5 15)"); + assertThat(solve(new int[] {12, 15})).isEqualTo("(2 12)(3 27)(5 15)"); } @Test void negativeNumbersContributeTheirSignedValue() { - assertThat(solve(new int[]{15, 30, -45})).isEqualTo("(2 30)(3 0)(5 0)"); + assertThat(solve(new int[] {15, 30, -45})).isEqualTo("(2 30)(3 0)(5 0)"); } @Test void emptyListGivesEmptyString() { - assertThat(solve(new int[]{})).isEmpty(); + assertThat(solve(new int[] {})).isEmpty(); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/TdaRationalTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/TdaRationalTest.java index a4d40b6..659f2ab 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/TdaRationalTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/TdaRationalTest.java @@ -1,11 +1,11 @@ package io.github.diegourban.algorithms.problems.math; -import io.github.diegourban.algorithms.math.NumberTheory; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import io.github.diegourban.algorithms.math.NumberTheory; +import org.junit.jupiter.api.Test; + /** * TDA Rational — beecrowd (formerly URI Online Judge) 1022. *

@@ -16,18 +16,20 @@ */ class TdaRationalTest { - record Fraction(int numerator, int denominator) { - } + record Fraction(int numerator, int denominator) {} - record Result(Fraction raw, Fraction simplified) { - } + record Result(Fraction raw, Fraction simplified) {} static Result solve(Fraction a, char operator, Fraction b) { var raw = switch (operator) { - case '+' -> new Fraction(a.numerator() * b.denominator() + b.numerator() * a.denominator(), - a.denominator() * b.denominator()); - case '-' -> new Fraction(a.numerator() * b.denominator() - b.numerator() * a.denominator(), - a.denominator() * b.denominator()); + case '+' -> + new Fraction( + a.numerator() * b.denominator() + b.numerator() * a.denominator(), + a.denominator() * b.denominator()); + case '-' -> + new Fraction( + a.numerator() * b.denominator() - b.numerator() * a.denominator(), + a.denominator() * b.denominator()); case '*' -> new Fraction(a.numerator() * b.numerator(), a.denominator() * b.denominator()); case '/' -> new Fraction(a.numerator() * b.denominator(), b.numerator() * a.denominator()); default -> throw new IllegalArgumentException("Invalid operator: " + operator); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/math/TripleDoubleTest.java b/src/test/java/io/github/diegourban/algorithms/problems/math/TripleDoubleTest.java index 6f582d4..dbe8e47 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/math/TripleDoubleTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/math/TripleDoubleTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.math; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; +import static org.assertj.core.api.Assertions.assertThat; import java.util.stream.IntStream; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; /** * Triple trouble — Codewars (6 kyu). @@ -22,19 +21,21 @@ static int solve(long num1, long num2) { var a = Long.toString(num1); var b = Long.toString(num2); return IntStream.rangeClosed(0, 9) - .mapToObj(Integer::toString) - .anyMatch(d -> a.contains(d.repeat(3)) && b.contains(d.repeat(2))) ? 1 : 0; + .mapToObj(Integer::toString) + .anyMatch(d -> a.contains(d.repeat(3)) && b.contains(d.repeat(2))) + ? 1 + : 0; } @ParameterizedTest @CsvSource({ - "451999277, 41177722899, 1", - "1222345, 12345, 0", - "12345, 12345, 0", - "666789, 12345667, 1", - "10560002, 100, 1", - "111, 22, 0", - "1112, 122, 0", + "451999277, 41177722899, 1", + "1222345, 12345, 0", + "12345, 12345, 0", + "666789, 12345667, 1", + "10560002, 100, 1", + "111, 22, 0", + "1112, 122, 0", }) void requiresTripleAndDoubleOfTheSameDigit(long num1, long num2, int expected) { assertThat(solve(num1, num2)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/recursion/CollatzConjectureTest.java b/src/test/java/io/github/diegourban/algorithms/problems/recursion/CollatzConjectureTest.java index 579cb1a..42df6b3 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/recursion/CollatzConjectureTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/recursion/CollatzConjectureTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.problems.recursion; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import static org.assertj.core.api.Assertions.assertThat; - /** * Collatz Conjecture — Codewars (7 kyu). *

@@ -22,13 +22,7 @@ static long solve(long x) { @ParameterizedTest @CsvSource({ - "1, 1", - "2, 2", - "3, 8", - "30, 19", - "27, 112", - "100, 26", - "103, 88", + "1, 1", "2, 2", "3, 8", "30, 19", "27, 112", "100, 26", "103, 88", }) void countsSequenceLength(long x, long expected) { assertThat(solve(x)).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/recursion/RecursiveStringPermutationsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/recursion/RecursiveStringPermutationsTest.java index 7b42c4d..887303e 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/recursion/RecursiveStringPermutationsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/recursion/RecursiveStringPermutationsTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.recursion; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.HashSet; import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Recursive String Permutations — Interview Cake. diff --git a/src/test/java/io/github/diegourban/algorithms/problems/recursion/SquareRootOfTwoTest.java b/src/test/java/io/github/diegourban/algorithms/problems/recursion/SquareRootOfTwoTest.java index 7656e7a..156c50d 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/recursion/SquareRootOfTwoTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/recursion/SquareRootOfTwoTest.java @@ -1,14 +1,13 @@ package io.github.diegourban.algorithms.problems.recursion; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.offset; + +import java.util.Locale; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; -import java.util.Locale; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.offset; - /** * Square Root of 2 — beecrowd (formerly URI Online Judge) 2166. *

@@ -29,10 +28,10 @@ private static double tail(int n) { @ParameterizedTest @CsvSource({ - "0, 1.0000000000", - "1, 1.5000000000", - "5, 1.4142857143", - "100, 1.4142135624", + "0, 1.0000000000", + "1, 1.5000000000", + "5, 1.4142857143", + "100, 1.4142135624", }) void approximatesSquareRootOfTwoToTenDecimals(int n, String expected) { assertThat(String.format(Locale.ROOT, "%.10f", solve(n))).isEqualTo(expected); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/search/FindRepeatTest.java b/src/test/java/io/github/diegourban/algorithms/problems/search/FindRepeatTest.java index 80fc247..08eb636 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/search/FindRepeatTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/search/FindRepeatTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.search; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Find Repeat, Space Edition — Interview Cake. *

@@ -37,31 +37,31 @@ static int solve(int[] numbers) { @Test void smallestInputsRepeatOne() { - assertThat(solve(new int[]{1, 1})).isOne(); - assertThat(solve(new int[]{1, 1, 1})).isOne(); + assertThat(solve(new int[] {1, 1})).isOne(); + assertThat(solve(new int[] {1, 1, 1})).isOne(); } @Test void findsAdjacentRepeatAnywhereInTheRange() { - assertThat(solve(new int[]{1, 1, 2, 3, 4, 5, 6, 7, 8, 9})).isOne(); - assertThat(solve(new int[]{1, 2, 2, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(2); - assertThat(solve(new int[]{1, 2, 3, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(3); - assertThat(solve(new int[]{1, 2, 3, 4, 4, 5, 6, 7, 8, 9})).isEqualTo(4); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 5, 6, 7, 8, 9})).isEqualTo(5); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 6, 7, 8, 9})).isEqualTo(6); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 7, 8, 9})).isEqualTo(7); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 8, 9})).isEqualTo(8); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 9})).isEqualTo(9); + assertThat(solve(new int[] {1, 1, 2, 3, 4, 5, 6, 7, 8, 9})).isOne(); + assertThat(solve(new int[] {1, 2, 2, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(2); + assertThat(solve(new int[] {1, 2, 3, 3, 4, 5, 6, 7, 8, 9})).isEqualTo(3); + assertThat(solve(new int[] {1, 2, 3, 4, 4, 5, 6, 7, 8, 9})).isEqualTo(4); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 5, 6, 7, 8, 9})).isEqualTo(5); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 6, 7, 8, 9})).isEqualTo(6); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 7, 8, 9})).isEqualTo(7); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 8, 9})).isEqualTo(8); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 9})).isEqualTo(9); } @Test void findsRepeatRegardlessOfPosition() { - assertThat(solve(new int[]{1, 2, 1, 3, 4, 5, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 1, 4, 5, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 1, 5, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 1, 6, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 1, 7, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 1, 8})).isOne(); - assertThat(solve(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 1})).isOne(); + assertThat(solve(new int[] {1, 2, 1, 3, 4, 5, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 1, 4, 5, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 1, 5, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 1, 6, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 1, 7, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 1, 8})).isOne(); + assertThat(solve(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 1})).isOne(); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/search/FindRotationPointTest.java b/src/test/java/io/github/diegourban/algorithms/problems/search/FindRotationPointTest.java index 7d27fef..803c12f 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/search/FindRotationPointTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/search/FindRotationPointTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.search; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Find Rotation Point — Interview Cake. *

@@ -34,34 +34,45 @@ static int solve(String[] words) { @Test void findsRotationPointInSingleLetterWords() { - assertThat(solve(new String[]{"k", "v", "a", "b", "c", "d", "e", "g", "i"})).isEqualTo(2); + assertThat(solve(new String[] {"k", "v", "a", "b", "c", "d", "e", "g", "i"})) + .isEqualTo(2); } @Test void findsRotationPointInDictionaryWords() { - assertThat(solve(new String[]{ - "ptolemaic", "retrograde", "supplant", "undulate", "xenoepist", - "asymptote", "babka", "banoffee", "engender", "karpatka", "othellolagkage" - })).isEqualTo(5); + assertThat(solve(new String[] { + "ptolemaic", + "retrograde", + "supplant", + "undulate", + "xenoepist", + "asymptote", + "babka", + "banoffee", + "engender", + "karpatka", + "othellolagkage" + })) + .isEqualTo(5); } @Test void findsRotationPointRightAfterTheFirstWord() { - assertThat(solve(new String[]{"z", "a", "b", "c", "d"})).isOne(); + assertThat(solve(new String[] {"z", "a", "b", "c", "d"})).isOne(); } @Test void unrotatedListStartsAtZero() { - assertThat(solve(new String[]{"a", "b", "c", "d"})).isZero(); + assertThat(solve(new String[] {"a", "b", "c", "d"})).isZero(); } @Test void singleWordStartsAtZero() { - assertThat(solve(new String[]{"a"})).isZero(); + assertThat(solve(new String[] {"a"})).isZero(); } @Test void findsRotationPointAtTheEnd() { - assertThat(solve(new String[]{"b", "c", "d", "a"})).isEqualTo(3); + assertThat(solve(new String[] {"b", "c", "d", "a"})).isEqualTo(3); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/search/TwoBillsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/search/TwoBillsTest.java index 852676a..a600847 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/search/TwoBillsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/search/TwoBillsTest.java @@ -1,18 +1,17 @@ package io.github.diegourban.algorithms.problems.search; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; /** * Two Bills — beecrowd (formerly URI Online Judge) 2140. @@ -43,8 +42,9 @@ static boolean solve(int price, int paid) { static String run(InputStream in) throws IOException { var reader = new Reader(in); var out = new StringBuilder(); - for (int price = reader.nextInt(), paid = reader.nextInt(); price != 0 || paid != 0; - price = reader.nextInt(), paid = reader.nextInt()) { + for (int price = reader.nextInt(), paid = reader.nextInt(); + price != 0 || paid != 0; + price = reader.nextInt(), paid = reader.nextInt()) { out.append(solve(price, paid) ? "possible" : "impossible").append('\n'); } return out.toString(); @@ -52,11 +52,11 @@ static String run(InputStream in) throws IOException { @ParameterizedTest @CsvSource({ - "11, 23", - "500, 650", - "9948, 9963", - "1, 5", - "0, 200", + "11, 23", + "500, 650", + "9948, 9963", + "1, 5", + "0, 200", }) void changeMadeOfTwoBillsIsPossible(int price, int paid) { assertThat(solve(price, paid)).isTrue(); @@ -64,12 +64,12 @@ void changeMadeOfTwoBillsIsPossible(int price, int paid) { @ParameterizedTest @CsvSource({ - "100, 600", - "1, 2", - "1, 3", - "1, 7", - "5, 5", - "0, 201", + "100, 600", + "1, 2", + "1, 3", + "1, 7", + "5, 5", + "0, 201", }) void otherChangeIsImpossible(int price, int paid) { assertThat(solve(price, paid)).isFalse(); @@ -77,7 +77,8 @@ void otherChangeIsImpossible(int price, int paid) { @Test void zeroZeroEndsTheInput() throws IOException { - assertThat(run(new ByteArrayInputStream("0 0\n".getBytes(StandardCharsets.UTF_8)))).isEmpty(); + assertThat(run(new ByteArrayInputStream("0 0\n".getBytes(StandardCharsets.UTF_8)))) + .isEmpty(); } @ParameterizedTest diff --git a/src/test/java/io/github/diegourban/algorithms/problems/simulation/MaestersMapTest.java b/src/test/java/io/github/diegourban/algorithms/problems/simulation/MaestersMapTest.java index 714a410..d510a9a 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/simulation/MaestersMapTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/simulation/MaestersMapTest.java @@ -1,17 +1,16 @@ package io.github.diegourban.algorithms.problems.simulation; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Maester's Map — beecrowd (formerly URI Online Judge) 1855. @@ -77,10 +76,7 @@ static String run(InputStream in) throws IOException { @Test void followsArrowsToTheChest() { - assertThat(solve(List.of( - ">..v", - "....", - "*..<"))).isTrue(); + assertThat(solve(List.of(">..v", "....", "*..<"))).isTrue(); } @Test @@ -90,10 +86,7 @@ void walksStraightIntoTheChest() { @Test void reusingAnArrowIsALoop() { - assertThat(solve(List.of( - ">.....v", - ".......", - "^.....<"))).isFalse(); + assertThat(solve(List.of(">.....v", ".......", "^.....<"))).isFalse(); } @Test @@ -103,19 +96,21 @@ void walkingOffTheMapIsAFailure() { } @ParameterizedTest - @ValueSource(strings = { - "sampleFindChest1", "sampleFindChest2", "sampleFindChest3", - "sampleFindChest4", "sampleFindChest5", "sampleFindChest6", - }) + @ValueSource( + strings = { + "sampleFindChest1", "sampleFindChest2", "sampleFindChest3", + "sampleFindChest4", "sampleFindChest5", "sampleFindChest6", + }) void samplesThatReachTheChest(String name) throws IOException { assertThat(run(samples.loadAsInputStream(name + ".in"))).isEqualTo("*\n"); } @ParameterizedTest - @ValueSource(strings = { - "sampleLoop1", "sampleLoop2", "sampleLoop3", "sampleLoop4", - "sampleOutOfBounds1", "sampleOutOfBounds2", "sampleOutOfBounds3", "sampleOutOfBounds4", - }) + @ValueSource( + strings = { + "sampleLoop1", "sampleLoop2", "sampleLoop3", "sampleLoop4", + "sampleOutOfBounds1", "sampleOutOfBounds2", "sampleOutOfBounds3", "sampleOutOfBounds4", + }) void samplesThatLoopOrLeaveTheMap(String name) throws IOException { assertThat(run(samples.loadAsInputStream(name + ".in"))).isEqualTo("!\n"); } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/simulation/StarTrekTest.java b/src/test/java/io/github/diegourban/algorithms/problems/simulation/StarTrekTest.java index 4dabc1c..c85ab5c 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/simulation/StarTrekTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/simulation/StarTrekTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.simulation; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - import java.io.IOException; import java.io.InputStream; import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; /** * Star Trek — beecrowd (formerly URI Online Judge) 1973. @@ -25,8 +24,7 @@ class StarTrekTest { private final SampleLoader samples = new SampleLoader("problems/simulation/star-trek"); - record Raid(int starsAttacked, long sheepLeft) { - } + record Raid(int starsAttacked, long sheepLeft) {} static Raid solve(int[] sheep) { var left = sheep.clone(); @@ -61,43 +59,43 @@ static String run(InputStream in) throws IOException { @Test void singleStarWithOneSheep() { - assertThat(solve(new int[]{1})).isEqualTo(new Raid(1, 0)); + assertThat(solve(new int[] {1})).isEqualTo(new Raid(1, 0)); } @Test void allOddStarsAreAttackedOnceOnTheWayOut() { - assertThat(solve(new int[]{1, 1, 1, 1, 1})).isEqualTo(new Raid(5, 0)); - assertThat(solve(new int[]{3, 3, 3, 3, 3})).isEqualTo(new Raid(5, 10)); + assertThat(solve(new int[] {1, 1, 1, 1, 1})).isEqualTo(new Raid(5, 0)); + assertThat(solve(new int[] {3, 3, 3, 3, 3})).isEqualTo(new Raid(5, 10)); } @Test void evenStarTurnsKirkBackAndEveryStarBehindIsRobbedAgain() { - assertThat(solve(new int[]{1, 1, 2, 1, 1})).isEqualTo(new Raid(3, 3)); - assertThat(solve(new int[]{3, 3, 2, 3, 3})).isEqualTo(new Raid(3, 9)); + assertThat(solve(new int[] {1, 1, 2, 1, 1})).isEqualTo(new Raid(3, 3)); + assertThat(solve(new int[] {3, 3, 2, 3, 3})).isEqualTo(new Raid(3, 9)); } @Test void evenLastStarTurnsKirkBack() { - assertThat(solve(new int[]{1, 1, 1, 1, 2})).isEqualTo(new Raid(5, 1)); - assertThat(solve(new int[]{3, 3, 3, 3, 2})).isEqualTo(new Raid(5, 5)); + assertThat(solve(new int[] {1, 1, 1, 1, 2})).isEqualTo(new Raid(5, 1)); + assertThat(solve(new int[] {3, 3, 3, 3, 2})).isEqualTo(new Raid(5, 5)); } @Test void emptyStarIsEvenAndHasNothingToSteal() { - assertThat(solve(new int[]{3, 0, 5})).isEqualTo(new Raid(2, 6)); + assertThat(solve(new int[] {3, 0, 5})).isEqualTo(new Raid(2, 6)); } @Test void judgeSamples() { - assertThat(solve(new int[]{1, 3, 5, 7, 11, 13, 17, 19})).isEqualTo(new Raid(8, 68)); - assertThat(solve(new int[]{1, 3, 5, 7, 11, 13, 16, 19})).isEqualTo(new Raid(7, 63)); + assertThat(solve(new int[] {1, 3, 5, 7, 11, 13, 17, 19})).isEqualTo(new Raid(8, 68)); + assertThat(solve(new int[] {1, 3, 5, 7, 11, 13, 16, 19})).isEqualTo(new Raid(7, 63)); } @ParameterizedTest @CsvSource({ - "max, 10000 99990000", - "max2, 16000 15999968000", - "max3, 8000 15999968002", + "max, 10000 99990000", + "max2, 16000 15999968000", + "max3, 8000 15999968002", }) void maxSamples(String name, String expected) throws IOException { assertThat(run(samples.loadAsInputStream(name + ".in"))).isEqualTo(expected + "\n"); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/sorting/FindTheOddIntTest.java b/src/test/java/io/github/diegourban/algorithms/problems/sorting/FindTheOddIntTest.java index 62bb7ca..0158c7d 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/sorting/FindTheOddIntTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/sorting/FindTheOddIntTest.java @@ -1,11 +1,11 @@ package io.github.diegourban.algorithms.problems.sorting; -import io.github.diegourban.algorithms.sorting.MergeSort; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import io.github.diegourban.algorithms.sorting.MergeSort; +import org.junit.jupiter.api.Test; + /** * Find the odd int — Codewars (6 kyu). *

@@ -34,40 +34,41 @@ static int solve(int[] numbers) { @Test void findsValueAppearingOddTimesAmongPairs() { - assertThat(solve(new int[]{20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5})).isEqualTo(5); - assertThat(solve(new int[]{20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5})).isEqualTo(5); + assertThat(solve(new int[] {20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5})) + .isEqualTo(5); + assertThat(solve(new int[] {20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5})).isEqualTo(5); } @Test void handlesNegativeValues() { - assertThat(solve(new int[]{1, 1, 2, -2, 5, 2, 4, 4, -1, -2, 5})).isEqualTo(-1); + assertThat(solve(new int[] {1, 1, 2, -2, 5, 2, 4, 4, -1, -2, 5})).isEqualTo(-1); } @Test void singleElementIsTheAnswer() { - assertThat(solve(new int[]{10})).isEqualTo(10); + assertThat(solve(new int[] {10})).isEqualTo(10); } @Test void valueSurroundedByAnEvenRunOfAnother() { - assertThat(solve(new int[]{1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1})).isEqualTo(10); + assertThat(solve(new int[] {1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1})).isEqualTo(10); } @Test void answerIsTheSmallestValue() { - assertThat(solve(new int[]{5, 4, 3, 2, 1, 5, 4, 3, 2, 10, 10})).isEqualTo(1); + assertThat(solve(new int[] {5, 4, 3, 2, 1, 5, 4, 3, 2, 10, 10})).isEqualTo(1); } @Test void doesNotMutateInput() { - var numbers = new int[]{3, 1, 3}; + var numbers = new int[] {3, 1, 3}; solve(numbers); assertThat(numbers).containsExactly(3, 1, 3); } @Test void rejectsInputWithoutAnOddCount() { - assertThatIllegalArgumentException().isThrownBy(() -> solve(new int[]{})); - assertThatIllegalArgumentException().isThrownBy(() -> solve(new int[]{7, 7})); + assertThatIllegalArgumentException().isThrownBy(() -> solve(new int[] {})); + assertThatIllegalArgumentException().isThrownBy(() -> solve(new int[] {7, 7})); } } diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/ContractRevisionTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/ContractRevisionTest.java index 78490aa..65735d0 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/ContractRevisionTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/ContractRevisionTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.string; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; +import java.io.IOException; +import java.io.InputStream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Contract Revision — beecrowd (formerly URI Online Judge) 1120. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/EncryptionTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/EncryptionTest.java index 43240ba..e3b2c1a 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/EncryptionTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/EncryptionTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.string; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; +import java.io.IOException; +import java.io.InputStream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Encryption — beecrowd (formerly URI Online Judge) 1024. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/MorseTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/MorseTest.java index bffcacf..2d859fd 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/MorseTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/MorseTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.problems.string; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; /** * Morse — beecrowd (formerly URI Online Judge) 2338. @@ -18,8 +17,8 @@ class MorseTest { private static final String[] MORSE = { - ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", - "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." + ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", + "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." }; static String solve(String code) { diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/OneTwoThreeTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/OneTwoThreeTest.java index 5098db7..3baf9df 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/OneTwoThreeTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/OneTwoThreeTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.string; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; +import java.io.IOException; +import java.io.InputStream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; - /** * One-Two-Three — beecrowd (formerly URI Online Judge) 1332. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/ParenthesisMatchingTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/ParenthesisMatchingTest.java index 2e3d03e..c04f74f 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/ParenthesisMatchingTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/ParenthesisMatchingTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.problems.string; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import org.junit.jupiter.api.Test; + /** * Parenthesis Matching — Interview Cake. *

@@ -26,7 +26,7 @@ static int solve(String sentence, int openingParenthesisIndex) { } openNested--; } - default -> { } + default -> {} } } throw new IllegalArgumentException("No closing parenthesis :("); diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/PlayingWithPassphrasesTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/PlayingWithPassphrasesTest.java index 40b95aa..fbc2caf 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/PlayingWithPassphrasesTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/PlayingWithPassphrasesTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.string; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Playing with passphrases — Codewars (6 kyu). *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/ReverseWordsTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/ReverseWordsTest.java index 1c2c7e5..909562d 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/ReverseWordsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/ReverseWordsTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.problems.string; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + /** * Reverse Words — Interview Cake. *

diff --git a/src/test/java/io/github/diegourban/algorithms/problems/string/SearchingSubsequencesTest.java b/src/test/java/io/github/diegourban/algorithms/problems/string/SearchingSubsequencesTest.java index 38bb947..8fe8e83 100644 --- a/src/test/java/io/github/diegourban/algorithms/problems/string/SearchingSubsequencesTest.java +++ b/src/test/java/io/github/diegourban/algorithms/problems/string/SearchingSubsequencesTest.java @@ -1,16 +1,15 @@ package io.github.diegourban.algorithms.problems.string; +import static org.assertj.core.api.Assertions.assertThat; + import io.github.diegourban.algorithms.io.Reader; import io.github.diegourban.algorithms.testing.SampleLoader; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - import java.io.EOFException; import java.io.IOException; import java.io.InputStream; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Searching Subsequences — beecrowd (formerly URI Online Judge) 2126. @@ -25,8 +24,7 @@ class SearchingSubsequencesTest { private static final SampleLoader SAMPLES = new SampleLoader("problems/string/searching-subsequences"); /** {@code count} occurrences, the last one starting at 1-based {@code lastPosition} (0 when none). */ - record Occurrences(int count, int lastPosition) { - } + record Occurrences(int count, int lastPosition) {} static Occurrences solve(String pattern, String text) { var count = 0; diff --git a/src/test/java/io/github/diegourban/algorithms/search/BinarySearchPropertyTest.java b/src/test/java/io/github/diegourban/algorithms/search/BinarySearchPropertyTest.java index 546d7ab..f27ce51 100644 --- a/src/test/java/io/github/diegourban/algorithms/search/BinarySearchPropertyTest.java +++ b/src/test/java/io/github/diegourban/algorithms/search/BinarySearchPropertyTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.search; -import net.jqwik.api.ForAll; -import net.jqwik.api.Property; +import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; /** * {@link BinarySearch#indexOf(int[], int)} must agree with {@link Arrays#binarySearch(int[], int)} on whether the key diff --git a/src/test/java/io/github/diegourban/algorithms/search/BinarySearchTest.java b/src/test/java/io/github/diegourban/algorithms/search/BinarySearchTest.java index 99e9811..a9d673e 100644 --- a/src/test/java/io/github/diegourban/algorithms/search/BinarySearchTest.java +++ b/src/test/java/io/github/diegourban/algorithms/search/BinarySearchTest.java @@ -1,9 +1,9 @@ package io.github.diegourban.algorithms.search; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + class BinarySearchTest { private static final int[] ONE_TO_TEN = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; @@ -26,7 +26,8 @@ void findsBothEnds() { @Test void returnsMinusOneForAMissingKey() { - assertThat(BinarySearch.indexOf(new int[]{1, 2, 3, 5, 6, 7, 8, 9, 10}, 4)).isEqualTo(-1); + assertThat(BinarySearch.indexOf(new int[] {1, 2, 3, 5, 6, 7, 8, 9, 10}, 4)) + .isEqualTo(-1); } @Test @@ -37,6 +38,6 @@ void returnsMinusOneOutsideTheRange() { @Test void returnsMinusOneForAnEmptyArray() { - assertThat(BinarySearch.indexOf(new int[]{}, 1)).isEqualTo(-1); + assertThat(BinarySearch.indexOf(new int[] {}, 1)).isEqualTo(-1); } } diff --git a/src/test/java/io/github/diegourban/algorithms/sorting/SorterPropertyTest.java b/src/test/java/io/github/diegourban/algorithms/sorting/SorterPropertyTest.java index dcdd5d5..e70a9b1 100644 --- a/src/test/java/io/github/diegourban/algorithms/sorting/SorterPropertyTest.java +++ b/src/test/java/io/github/diegourban/algorithms/sorting/SorterPropertyTest.java @@ -1,15 +1,14 @@ package io.github.diegourban.algorithms.sorting; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; import net.jqwik.api.Arbitraries; import net.jqwik.api.Arbitrary; import net.jqwik.api.ForAll; import net.jqwik.api.Property; import net.jqwik.api.Provide; -import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; - /** * Every {@link Sorter} must agree with {@link Arrays#sort(int[])} on any input. */ diff --git a/src/test/java/io/github/diegourban/algorithms/sorting/SorterTest.java b/src/test/java/io/github/diegourban/algorithms/sorting/SorterTest.java index abc10a5..e5ec128 100644 --- a/src/test/java/io/github/diegourban/algorithms/sorting/SorterTest.java +++ b/src/test/java/io/github/diegourban/algorithms/sorting/SorterTest.java @@ -1,11 +1,10 @@ package io.github.diegourban.algorithms.sorting; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import static org.assertj.core.api.Assertions.assertThat; import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Example-based checks run against every {@link Sorter}; {@link SorterPropertyTest} covers random inputs. diff --git a/src/test/java/io/github/diegourban/algorithms/string/StringUtilsPropertyTest.java b/src/test/java/io/github/diegourban/algorithms/string/StringUtilsPropertyTest.java index 9f878ad..09e7b28 100644 --- a/src/test/java/io/github/diegourban/algorithms/string/StringUtilsPropertyTest.java +++ b/src/test/java/io/github/diegourban/algorithms/string/StringUtilsPropertyTest.java @@ -1,17 +1,16 @@ package io.github.diegourban.algorithms.string; -import net.jqwik.api.ForAll; -import net.jqwik.api.Property; -import net.jqwik.api.constraints.AlphaChars; -import net.jqwik.api.constraints.NotEmpty; -import net.jqwik.api.constraints.StringLength; +import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; +import net.jqwik.api.constraints.AlphaChars; +import net.jqwik.api.constraints.NotEmpty; +import net.jqwik.api.constraints.StringLength; /** * Inputs are ASCII letters: both methods compare {@code char} by {@code char}, so the properties check their logic @@ -27,8 +26,8 @@ void isPalindromeAgreesWithReversing(@ForAll @AlphaChars @NotEmpty @StringLength } @Property - void mirroredStringsArePalindromes(@ForAll @AlphaChars @NotEmpty @StringLength(max = 20) String s, - @ForAll @AlphaChars char middle) { + void mirroredStringsArePalindromes( + @ForAll @AlphaChars @NotEmpty @StringLength(max = 20) String s, @ForAll @AlphaChars char middle) { var reversed = new StringBuilder(s).reverse().toString(); assertThat(StringUtils.isPalindrome(s + reversed)).isTrue(); @@ -42,12 +41,13 @@ void aShuffleIsAnAnagram(@ForAll @AlphaChars String s, @ForAll long seed) { var shuffled = chars.stream().map(String::valueOf).collect(Collectors.joining()); assertThat(StringUtils.isAnagram(s, shuffled)).isTrue(); - assertThat(StringUtils.isAnagram(s.toUpperCase(), shuffled.toLowerCase())).isTrue(); + assertThat(StringUtils.isAnagram(s.toUpperCase(), shuffled.toLowerCase())) + .isTrue(); } @Property - void isAnagramAgreesWithLetterCounts(@ForAll @AlphaChars @StringLength(max = 6) String a, - @ForAll @AlphaChars @StringLength(max = 6) String b) { + void isAnagramAgreesWithLetterCounts( + @ForAll @AlphaChars @StringLength(max = 6) String a, @ForAll @AlphaChars @StringLength(max = 6) String b) { assertThat(StringUtils.isAnagram(a, b)).isEqualTo(letterCounts(a).equals(letterCounts(b))); } diff --git a/src/test/java/io/github/diegourban/algorithms/string/StringUtilsTest.java b/src/test/java/io/github/diegourban/algorithms/string/StringUtilsTest.java index be626a8..2f4ff4f 100644 --- a/src/test/java/io/github/diegourban/algorithms/string/StringUtilsTest.java +++ b/src/test/java/io/github/diegourban/algorithms/string/StringUtilsTest.java @@ -1,10 +1,8 @@ package io.github.diegourban.algorithms.string; -import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import org.junit.jupiter.api.Test; class StringUtilsTest { @@ -26,5 +24,4 @@ void shouldNotConsiderPalindrome() { assertThat(StringUtils.isPalindrome("ab")).isFalse(); assertThat(StringUtils.isPalindrome("abb")).isFalse(); } - } diff --git a/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilder.java b/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilder.java index 83aa952..d86d5d6 100644 --- a/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilder.java +++ b/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilder.java @@ -76,5 +76,4 @@ private boolean endsWithSpaceSeparator() { private void trim() { this.stringBuilder.deleteCharAt(this.stringBuilder.length() - 1); } - } diff --git a/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilderTest.java b/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilderTest.java index 544aea0..d4c5f78 100644 --- a/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilderTest.java +++ b/src/test/java/io/github/diegourban/algorithms/testing/SampleBuilderTest.java @@ -1,11 +1,11 @@ package io.github.diegourban.algorithms.testing; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class SampleBuilderTest { private SampleBuilder sampleBuilder; @@ -57,7 +57,12 @@ void shouldAppendString() { @Test void shouldAppendAllTypes() { - String sample = this.sampleBuilder.append(123).append(9.9).append(9876543210L).append("bla bla").build(); + String sample = this.sampleBuilder + .append(123) + .append(9.9) + .append(9876543210L) + .append("bla bla") + .build(); assertThat(sample).isEqualTo("123 9.9 9876543210 bla bla"); } @@ -79,7 +84,8 @@ void shouldAppendTwoDoubleLines() { @Test void shouldAppendTwoLongLines() { - String sample = this.sampleBuilder.appendln(123987123987L).append(1122334455L).build(); + String sample = + this.sampleBuilder.appendln(123987123987L).append(1122334455L).build(); String expected = "123987123987" + System.lineSeparator() + "1122334455"; assertThat(sample).isEqualTo(expected); @@ -111,10 +117,15 @@ void shouldAppendTwoLinesWithNewLineAtTheEnd() { @Test void shouldAppendFourLinesOneTypeEach() { - String sample = this.sampleBuilder.appendln(123).appendln(987654321L).appendln(99.99).appendln("end").build(); - - String expected = "123" + System.lineSeparator() + "987654321" + System.lineSeparator() + "99.99" + System.lineSeparator() + "end" + System.lineSeparator(); + String sample = this.sampleBuilder + .appendln(123) + .appendln(987654321L) + .appendln(99.99) + .appendln("end") + .build(); + + String expected = "123" + System.lineSeparator() + "987654321" + System.lineSeparator() + "99.99" + + System.lineSeparator() + "end" + System.lineSeparator(); assertThat(sample).isEqualTo(expected); } - } diff --git a/src/test/java/io/github/diegourban/algorithms/testing/SampleLoaderTest.java b/src/test/java/io/github/diegourban/algorithms/testing/SampleLoaderTest.java index 6e3e373..b58977d 100644 --- a/src/test/java/io/github/diegourban/algorithms/testing/SampleLoaderTest.java +++ b/src/test/java/io/github/diegourban/algorithms/testing/SampleLoaderTest.java @@ -1,10 +1,10 @@ package io.github.diegourban.algorithms.testing; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import org.junit.jupiter.api.Test; + class SampleLoaderTest { private final SampleLoader loader = new SampleLoader("testing/sample-loader"); @@ -38,7 +38,8 @@ void shouldLoadAsInputStream() throws Exception { @Test void shouldAcceptTrailingSlash() { - assertThat(new SampleLoader("testing/sample-loader/").loadAsString("sample.out")).startsWith("1"); + assertThat(new SampleLoader("testing/sample-loader/").loadAsString("sample.out")) + .startsWith("1"); } @Test From 293439571c206450fb887a4e0e33815795e6b7ac Mon Sep 17 00:00:00 2001 From: Diego Leonardo Urban Date: Fri, 6 Nov 2026 16:30:52 +0100 Subject: [PATCH 2/2] Check formatting in the build with Spotless (palantir-java-format) ./mvnw verify now fails on unformatted sources; ./mvnw spotless:apply fixes them. The bulk reformat commit is listed in .git-blame-ignore-revs. Co-Authored-By: Claude Opus 5.5 --- .git-blame-ignore-revs | 5 +++++ README.md | 3 ++- pom.xml | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..3726c41 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,5 @@ +# Bulk formatting commits; hide them from `git blame` (GitHub honours this file). +# Locally: git config blame.ignoreRevsFile .git-blame-ignore-revs + +# Reformat all Java sources with palantir-java-format +f490a4f2627cc093ad2cfa4ba93809556254e7de diff --git a/README.md b/README.md index aadeffe..7b71007 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ A small Java library of algorithms and data structures, plus worked problems org Requires Java 25. Maven comes with the wrapper. ```sh -./mvnw verify # compile, run all tests, write coverage to target/site/jacoco +./mvnw verify # format check, compile, run all tests, write coverage to target/site/jacoco +./mvnw spotless:apply # format the sources (palantir-java-format) ./mvnw install # make io.github.diegourban:algorithms available to other local projects ``` diff --git a/pom.xml b/pom.xml index afb2010..fcbee54 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,8 @@ 3.5.0 3.6.1 0.8.16 + 3.11.0 + 2.103.0 @@ -93,6 +95,26 @@ + + com.diffplug.spotless + spotless-maven-plugin + ${spotless-maven-plugin.version} + + + + ${palantir-java-format.version} + + + + + + + + check + + + + maven-enforcer-plugin ${maven-enforcer-plugin.version}