From 9b5ced457c6e1e78c5d958a7b0d7ccc678f773c5 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 16:37:49 +0500 Subject: [PATCH 01/28] Sorted Linked List added with Javadoc and tests --- .../lists/SortedLinkedList.java | 161 ++++++++++++++++++ .../lists/SortedlinkedListTest.java | 88 ++++++++++ 2 files changed, 249 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java create mode 100644 src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java new file mode 100644 index 000000000000..2090fa505061 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -0,0 +1,161 @@ +package com.thealgorithms.datastructures.lists; + +import java.util.ArrayList; + +/** + * A SortedLinkedList is a data structure that maintains a sorted list of elements. + * Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation. + * + * @author Muhammad Junaid Khalid + * @param int the type of elements in this list + */ + +public class SortedLinkedList { + private Node head; + private Node tail; + + public SortedLinkedList(){ + this.head=null; + this.tail=null; + } + + /** + * Inserts a new element into the sorted linked list. + * + * @param value the value to be inserted + */ + public void insert(int value){ + Node newNode = new Node(value); + if (head == null) { + this.head = newNode; + this.tail = newNode; + } + else if (value < head.value) { + newNode.next = this.head; + this.head = newNode; + } + else if (value > tail.value) { + this.tail.next = newNode; + } + else{ + Node temp=head; + while (temp.next != null && temp.next.value < value) { + temp = temp.next; + } + newNode.next = temp.next; + temp.next = newNode; + } + } + + /** + * Displays the elements of the sorted linked list. + */ + public void display(){ + System.out.println(this.toString()); + } + + /** + * Deletes the first occurrence of the specified element in the sorted linked list. + * + * @param value the value to be deleted + * @return true if the element is found and deleted, false otherwise + */ + public boolean delete(int value){ + if (this.head == null) { + return false; + } + else if (this.head.value == value) { + this.head = this.head.next; + return true; + } + else{ + Node temp = this.head; + while (temp.next != null) { + if (temp.next.value == value) { + temp.next = temp.next.next; + return true; + } + temp = temp.next; + } + return false; + + } + } + + /** + * Searches for the specified element in the sorted linked list. + * + * @param value the value to be searched + * @return true if the element is found, false otherwise + */ + public boolean search(int value){ + Node temp = this.head; + while (temp != null) { + if (temp.value == value) { + return true; + } + temp = temp.next; + } + return false; + } + + /** + * Checks if the sorted linked list is empty. + * + * @return true if the list is empty, false otherwise + */ + public boolean isEmpty() { + return head == null; + } + + /** + * Returns the minimum value in the sorted linked list. + * + * @return the minimum value + */ + public int minValue(){ + return this.head.value; + } + + /** + * Returns the maximum value in the sorted linked list. + * + * @return the maximum value + */ + public int maxValue(){ + return this.tail.value; + } + + /** + * Returns a string representation of the sorted linked list. + * + * @return a string representation of the sorted linked list + */ + @Override + public String toString() { + ArrayList elements=new ArrayList<>(); + Node temp = this.head; + while (temp != null) { + elements.add(String.valueOf(temp.value)); + temp = temp.next; + } + return String.join(", ", elements); + } + + + public class Node { + public int value; + public Node next; + + public Node(){ + this.value = 0; + this.next= null; + } + + public Node(int value){ + this.value = value; + this.next = null; + } + + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java new file mode 100644 index 000000000000..f12294767466 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java @@ -0,0 +1,88 @@ +package com.thealgorithms.datastructures.lists; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class SortedLinkedListTest { + + @Test + public void testInsert() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertEquals("3, 5, 7", list.toString()); + } + + @Test + public void testDelete() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertTrue(list.delete(5)); + assertEquals("3, 7", list.toString()); + assertFalse(list.delete(10)); + } + + @Test + public void testSearch() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertTrue(list.search(5)); + assertFalse(list.search(10)); + } + + @Test + public void testMinValue() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertEquals(3, list.minValue()); + } + + @Test + public void testMaxValue() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertEquals(7, list.maxValue()); + } + + @Test + public void testEmptyList() { + SortedLinkedList list = new SortedLinkedList(); + assertEquals("", list.toString()); + assertFalse(list.delete(5)); + assertFalse(list.search(5)); + assertEquals(0, list.minValue()); + assertEquals(0, list.maxValue()); + } + @Test + public void testIsEmpty_onEmptyList() { + SortedLinkedList list = new SortedLinkedList(); + assertTrue(list.isEmpty()); + } + + @Test + public void testIsEmpty_onNonEmptyList() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(10); + assertFalse(list.isEmpty()); + } + + @Test + public void testIsEmpty_afterDeletion() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(10); + list.delete(10); + assertTrue(list.isEmpty()); + } +} From e1382c1ac040f03e1497957a1799b542ba9b0827 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 16:49:14 +0500 Subject: [PATCH 02/28] "Added comments to SortedLinkedList.java to describe the implementation and provide a reference link." --- .../thealgorithms/datastructures/lists/SortedLinkedList.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 2090fa505061..bde6a97de84a 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -5,7 +5,10 @@ /** * A SortedLinkedList is a data structure that maintains a sorted list of elements. * Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation. - * + * This implementation uses a singly linked list to store the elements. + * Further details can be found on this link + * https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html + * * @author Muhammad Junaid Khalid * @param int the type of elements in this list */ From 3ec762783b4e3a96ba686e6c1771de17e834b2f7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 16:56:07 +0500 Subject: [PATCH 03/28] Upgraded test from junit 4 to junit 5 --- .../datastructures/lists/SortedlinkedListTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java index f12294767466..5c19cd078b5d 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java @@ -1,10 +1,10 @@ package com.thealgorithms.datastructures.lists; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class SortedLinkedListTest { From 6ff74f0ea33e0df179e3c99404a8ad0ebc158710 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:02:53 +0500 Subject: [PATCH 04/28] Added space before braces of functions --- .../datastructures/lists/SortedLinkedList.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index bde6a97de84a..018329253518 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -17,7 +17,7 @@ public class SortedLinkedList { private Node head; private Node tail; - public SortedLinkedList(){ + public SortedLinkedList() { this.head=null; this.tail=null; } @@ -27,7 +27,7 @@ public SortedLinkedList(){ * * @param value the value to be inserted */ - public void insert(int value){ + public void insert(int value) { Node newNode = new Node(value); if (head == null) { this.head = newNode; @@ -53,7 +53,7 @@ else if (value > tail.value) { /** * Displays the elements of the sorted linked list. */ - public void display(){ + public void display() { System.out.println(this.toString()); } @@ -63,7 +63,7 @@ public void display(){ * @param value the value to be deleted * @return true if the element is found and deleted, false otherwise */ - public boolean delete(int value){ + public boolean delete(int value) { if (this.head == null) { return false; } @@ -91,7 +91,7 @@ else if (this.head.value == value) { * @param value the value to be searched * @return true if the element is found, false otherwise */ - public boolean search(int value){ + public boolean search(int value) { Node temp = this.head; while (temp != null) { if (temp.value == value) { @@ -116,7 +116,7 @@ public boolean isEmpty() { * * @return the minimum value */ - public int minValue(){ + public int minValue() { return this.head.value; } @@ -125,7 +125,7 @@ public int minValue(){ * * @return the maximum value */ - public int maxValue(){ + public int maxValue() { return this.tail.value; } @@ -150,12 +150,12 @@ public class Node { public int value; public Node next; - public Node(){ + public Node() { this.value = 0; this.next= null; } - public Node(int value){ + public Node(int value) { this.value = value; this.next = null; } From dd91a15949be35a10cd8916af9d76a100ebf3369 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Wed, 2 Oct 2024 17:07:24 +0500 Subject: [PATCH 05/28] Rename SortedlinkedListTest.java to SortedLinkedListTest.java --- .../{SortedlinkedListTest.java => SortedLinkedListTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/test/java/com/thealgorithms/datastructures/lists/{SortedlinkedListTest.java => SortedLinkedListTest.java} (100%) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java similarity index 100% rename from src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java rename to src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java From 2c33f89f132560af95602a3c2c506f812f1fbb8b Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:12:08 +0500 Subject: [PATCH 06/28] made to string null safe --- .../lists/SortedLinkedList.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 018329253518..59c34fdc021f 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -18,8 +18,8 @@ public class SortedLinkedList { private Node tail; public SortedLinkedList() { - this.head=null; - this.tail=null; + this.head = null; + this.tail = null; } /** @@ -41,7 +41,7 @@ else if (value > tail.value) { this.tail.next = newNode; } else{ - Node temp=head; + Node temp = head; while (temp.next != null && temp.next.value < value) { temp = temp.next; } @@ -136,13 +136,18 @@ public int maxValue() { */ @Override public String toString() { - ArrayList elements=new ArrayList<>(); - Node temp = this.head; - while (temp != null) { - elements.add(String.valueOf(temp.value)); - temp = temp.next; + if (this.head != null) { + ArrayList elements=new ArrayList<>(); + Node temp = this.head; + while (temp != null) { + elements.add(String.valueOf(temp.value)); + temp = temp.next; + } + return String.join(", ", elements); + } + else { + return ""; } - return String.join(", ", elements); } @@ -152,7 +157,7 @@ public class Node { public Node() { this.value = 0; - this.next= null; + this.next = null; } public Node(int value) { From 58390c1d9c47fa7b2a2f6160e160664050d66bb7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:32:15 +0500 Subject: [PATCH 07/28] Updated tail --- .../datastructures/lists/SortedLinkedList.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 59c34fdc021f..948e533f085b 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -47,6 +47,9 @@ else if (value > tail.value) { } newNode.next = temp.next; temp.next = newNode; + if (newNode.next==null){ + this.tail=newNode; + } } } @@ -63,25 +66,33 @@ public void display() { * @param value the value to be deleted * @return true if the element is found and deleted, false otherwise */ - public boolean delete(int value) { + public boolean delete(int value){ if (this.head == null) { return false; } else if (this.head.value == value) { - this.head = this.head.next; + if (this.head.next == null) { + this.head = null; + this.tail = null; + } else { + this.head = this.head.next; + } return true; } else{ Node temp = this.head; while (temp.next != null) { if (temp.next.value == value) { + if (temp.next == this.tail) { + this.tail = temp; + } temp.next = temp.next.next; return true; } temp = temp.next; } return false; - + } } From 028727f19366d58b5bb4db83072bd7a1e06a3e1c Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:38:06 +0500 Subject: [PATCH 08/28] "Added assignment of `this.tail` to `newNode` in `SortedLinkedList` class." --- .../com/thealgorithms/datastructures/lists/SortedLinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 948e533f085b..837697cd23f7 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -39,6 +39,7 @@ else if (value < head.value) { } else if (value > tail.value) { this.tail.next = newNode; + this.tail = newNode; } else{ Node temp = head; From 3dce7f0ff797dad43181b8b10f64dafbf6252914 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:48:52 +0500 Subject: [PATCH 09/28] Remove assertions for minValue and maxValue in empty list test --- .../datastructures/lists/SortedLinkedListTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 5c19cd078b5d..9c4a185c5155 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -62,8 +62,6 @@ public void testEmptyList() { assertEquals("", list.toString()); assertFalse(list.delete(5)); assertFalse(list.search(5)); - assertEquals(0, list.minValue()); - assertEquals(0, list.maxValue()); } @Test public void testIsEmpty_onEmptyList() { From 9e0cdf3135967e59e5cc77f7ebb4bc19b1c0543d Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:56:57 +0500 Subject: [PATCH 10/28] tried to get link updated --- .../lists/SortedLinkedList.java | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 837697cd23f7..12a40e18d696 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -8,7 +8,6 @@ * This implementation uses a singly linked list to store the elements. * Further details can be found on this link * https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html - * * @author Muhammad Junaid Khalid * @param int the type of elements in this list */ @@ -24,7 +23,6 @@ public SortedLinkedList() { /** * Inserts a new element into the sorted linked list. - * * @param value the value to be inserted */ public void insert(int value) { @@ -32,16 +30,13 @@ public void insert(int value) { if (head == null) { this.head = newNode; this.tail = newNode; - } - else if (value < head.value) { + } else if (value < head.value) { newNode.next = this.head; this.head = newNode; - } - else if (value > tail.value) { + } else if (value > tail.value) { this.tail.next = newNode; this.tail = newNode; - } - else{ + } else { Node temp = head; while (temp.next != null && temp.next.value < value) { temp = temp.next; @@ -63,15 +58,13 @@ public void display() { /** * Deletes the first occurrence of the specified element in the sorted linked list. - * * @param value the value to be deleted * @return true if the element is found and deleted, false otherwise */ - public boolean delete(int value){ + public boolean delete(int value) { if (this.head == null) { return false; - } - else if (this.head.value == value) { + } else if (this.head.value == value) { if (this.head.next == null) { this.head = null; this.tail = null; @@ -79,8 +72,7 @@ else if (this.head.value == value) { this.head = this.head.next; } return true; - } - else{ + } else{ Node temp = this.head; while (temp.next != null) { if (temp.next.value == value) { @@ -93,13 +85,11 @@ else if (this.head.value == value) { temp = temp.next; } return false; - } } /** * Searches for the specified element in the sorted linked list. - * * @param value the value to be searched * @return true if the element is found, false otherwise */ @@ -116,7 +106,6 @@ public boolean search(int value) { /** * Checks if the sorted linked list is empty. - * * @return true if the list is empty, false otherwise */ public boolean isEmpty() { @@ -125,7 +114,6 @@ public boolean isEmpty() { /** * Returns the minimum value in the sorted linked list. - * * @return the minimum value */ public int minValue() { @@ -134,7 +122,6 @@ public int minValue() { /** * Returns the maximum value in the sorted linked list. - * * @return the maximum value */ public int maxValue() { @@ -143,21 +130,19 @@ public int maxValue() { /** * Returns a string representation of the sorted linked list. - * * @return a string representation of the sorted linked list */ @Override public String toString() { if (this.head != null) { - ArrayList elements=new ArrayList<>(); + ArrayList elements = new ArrayList<>(); Node temp = this.head; while (temp != null) { elements.add(String.valueOf(temp.value)); temp = temp.next; } return String.join(", ", elements); - } - else { + } else { return ""; } } From 1c2bb10dfbd27639627741a2f4fe97b5a3c2fa1b Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 18:05:02 +0500 Subject: [PATCH 11/28] "Fixed whitespace and formatting issues in SortedLinkedList.java and SortedLinkedListTest.java" --- .../datastructures/lists/SortedLinkedList.java | 7 +++---- .../datastructures/lists/SortedLinkedListTest.java | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 12a40e18d696..0f084ca0e87a 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -43,8 +43,8 @@ public void insert(int value) { } newNode.next = temp.next; temp.next = newNode; - if (newNode.next==null){ - this.tail=newNode; + if (newNode.next == null) { + this.tail = newNode; } } } @@ -72,7 +72,7 @@ public boolean delete(int value) { this.head = this.head.next; } return true; - } else{ + } else { Node temp = this.head; while (temp.next != null) { if (temp.next.value == value) { @@ -146,7 +146,6 @@ public String toString() { return ""; } } - public class Node { public int value; diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 9c4a185c5155..53f1faf5ff69 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -64,20 +64,20 @@ public void testEmptyList() { assertFalse(list.search(5)); } @Test - public void testIsEmpty_onEmptyList() { + public void testIsEmptyOnEmptyList() { SortedLinkedList list = new SortedLinkedList(); assertTrue(list.isEmpty()); } @Test - public void testIsEmpty_onNonEmptyList() { + public void testIsEmptyOnNonEmptyList() { SortedLinkedList list = new SortedLinkedList(); list.insert(10); assertFalse(list.isEmpty()); } @Test - public void testIsEmpty_afterDeletion() { + public void testIsEmptyAfterDeletion() { SortedLinkedList list = new SortedLinkedList(); list.insert(10); list.delete(10); From 25be88e4a9b958cd8715da86019131731fb04b3a Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 18:09:29 +0500 Subject: [PATCH 12/28] formatting of test file corrected --- .../datastructures/lists/SortedLinkedListTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 53f1faf5ff69..8d130a9731c0 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test; public class SortedLinkedListTest { - + @Test public void testInsert() { SortedLinkedList list = new SortedLinkedList(); From 0a5495d07c2c07974bb4663c3cde606697dee0c7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 18:22:08 +0500 Subject: [PATCH 13/28] Removed few whitespaces --- .../thealgorithms/datastructures/lists/SortedLinkedList.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 0f084ca0e87a..57bf9d1ff3ff 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -84,7 +84,7 @@ public boolean delete(int value) { } temp = temp.next; } - return false; + return false; } } @@ -160,6 +160,5 @@ public Node(int value) { this.value = value; this.next = null; } - } } From 16cdb6012253588f1d67bb395c8ac0079e77bd04 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 05:45:44 +0500 Subject: [PATCH 14/28] Addressed comments by alxkm --- .../lists/SortedLinkedList.java | 27 +++---------------- .../lists/SortedLinkedListTest.java | 19 ------------- 2 files changed, 4 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 57bf9d1ff3ff..81f12d83bb10 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -1,6 +1,7 @@ package com.thealgorithms.datastructures.lists; import java.util.ArrayList; +import java.util.List; /** * A SortedLinkedList is a data structure that maintains a sorted list of elements. @@ -8,10 +9,7 @@ * This implementation uses a singly linked list to store the elements. * Further details can be found on this link * https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html - * @author Muhammad Junaid Khalid - * @param int the type of elements in this list */ - public class SortedLinkedList { private Node head; private Node tail; @@ -111,23 +109,6 @@ public boolean search(int value) { public boolean isEmpty() { return head == null; } - - /** - * Returns the minimum value in the sorted linked list. - * @return the minimum value - */ - public int minValue() { - return this.head.value; - } - - /** - * Returns the maximum value in the sorted linked list. - * @return the maximum value - */ - public int maxValue() { - return this.tail.value; - } - /** * Returns a string representation of the sorted linked list. * @return a string representation of the sorted linked list @@ -135,7 +116,7 @@ public int maxValue() { @Override public String toString() { if (this.head != null) { - ArrayList elements = new ArrayList<>(); + List elements = new ArrayList<>(); Node temp = this.head; while (temp != null) { elements.add(String.valueOf(temp.value)); @@ -147,8 +128,8 @@ public String toString() { } } - public class Node { - public int value; + public final class Node { + public final int value; public Node next; public Node() { diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 8d130a9731c0..7bc5006a9433 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -37,25 +37,6 @@ public void testSearch() { assertTrue(list.search(5)); assertFalse(list.search(10)); } - - @Test - public void testMinValue() { - SortedLinkedList list = new SortedLinkedList(); - list.insert(5); - list.insert(3); - list.insert(7); - assertEquals(3, list.minValue()); - } - - @Test - public void testMaxValue() { - SortedLinkedList list = new SortedLinkedList(); - list.insert(5); - list.insert(3); - list.insert(7); - assertEquals(7, list.maxValue()); - } - @Test public void testEmptyList() { SortedLinkedList list = new SortedLinkedList(); From 90c97828f81d6b712dee67f2a73a593c4b7ace96 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 10:29:59 +0500 Subject: [PATCH 15/28] "Updated toString method to include brackets and removed default Node constructor." --- .../datastructures/lists/SortedLinkedList.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 81f12d83bb10..4cf782679b7c 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -122,9 +122,10 @@ public String toString() { elements.add(String.valueOf(temp.value)); temp = temp.next; } - return String.join(", ", elements); + return "[" + String.join(", ", elements) + "]"; + } else { - return ""; + return "[]"; } } @@ -132,11 +133,6 @@ public final class Node { public final int value; public Node next; - public Node() { - this.value = 0; - this.next = null; - } - public Node(int value) { this.value = value; this.next = null; From ebc8d33fe7796c0cef3c04ccf5d401362d6de8fd Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 10:34:33 +0500 Subject: [PATCH 16/28] tests updated --- .../datastructures/lists/SortedLinkedListTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 7bc5006a9433..4877e6db4ec4 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -14,7 +14,7 @@ public void testInsert() { list.insert(5); list.insert(3); list.insert(7); - assertEquals("3, 5, 7", list.toString()); + assertEquals("[3, 5, 7]", list.toString()); } @Test @@ -24,7 +24,7 @@ public void testDelete() { list.insert(3); list.insert(7); assertTrue(list.delete(5)); - assertEquals("3, 7", list.toString()); + assertEquals("[3, 7]", list.toString()); assertFalse(list.delete(10)); } @@ -40,7 +40,7 @@ public void testSearch() { @Test public void testEmptyList() { SortedLinkedList list = new SortedLinkedList(); - assertEquals("", list.toString()); + assertEquals("[]", list.toString()); assertFalse(list.delete(5)); assertFalse(list.search(5)); } From 073be5ae0152ebc6aa6e440154c6fdbca6504225 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 14:13:54 +0500 Subject: [PATCH 17/28] Digit Separation algorithm added for positive numbers --- .../DigitSeparationPositiveNumber.java | 40 ++++++++++++ .../DigitSeparationPositiveNumberTest.java | 64 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumber.java create mode 100644 src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumberTest.java diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumber.java b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumber.java new file mode 100644 index 000000000000..df4159f96ddf --- /dev/null +++ b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumber.java @@ -0,0 +1,40 @@ +package com.thealgorithms.greedyalgorithms; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * This class provides methods to separate the digits of a large positive number into a list. + */ +public class DigitSeparationPositiveNumber { + public DigitSeparationPositiveNumber() { + } + /** + * Separates the digits of a large positive number into a list in reverse order. + * @param largeNumber The large number to separate digits from. + * @return A list of digits in reverse order. + */ + public List digitSeparationReverseOrder(long largeNumber) { + List result = new ArrayList<>(); + if (largeNumber!=0) { + while (largeNumber > 0) { + result.add(largeNumber%10); + largeNumber = largeNumber/10; + } + } else { + result.add(0L); + } + return result; + } + /** + * Separates the digits of a large positive number into a list in forward order. + * @param largeNumber The large number to separate digits from. + * @return A list of digits in forward order. + */ + public List digitSeparationForwardOrder(long largeNumber) { + List result = this.digitSeparationReverseOrder(largeNumber); + Collections.reverse(result); + return result; + } +} diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumberTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumberTest.java new file mode 100644 index 000000000000..75c48262051e --- /dev/null +++ b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumberTest.java @@ -0,0 +1,64 @@ +package com.thealgorithms.greedyalgorithms; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.List; + +public class DigitSeparationPositiveNumberTest { + + @Test + public void testDigitSeparationReverseOrder_SingleDigit() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationReverseOrder(5); + assertEquals(List.of(5L), result); + } + + @Test + public void testDigitSeparationReverseOrderMultipleDigits() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationReverseOrder(123); + assertEquals(List.of(3L, 2L, 1L), result); + } + + @Test + public void testDigitSeparationReverseOrderLargeNumber() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationReverseOrder(123456789); + assertEquals(List.of(9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), result); + } + + @Test + public void testDigitSeparationReverseOrderZero() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationReverseOrder(0); + assertEquals(List.of(0L), result); + } + + @Test + public void testDigitSeparationForwardOrderSingleDigit() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationForwardOrder(5); + assertEquals(List.of(5L), result); + } + + @Test + public void testDigitSeparationForwardOrderMultipleDigits() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationForwardOrder(123); + assertEquals(List.of(1L, 2L, 3L), result); + } + + @Test + public void testDigitSeparationForwardOrderLargeNumber() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationForwardOrder(123456789); + assertEquals(List.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), result); + } + + @Test + public void testDigitSeparationForwardOrderZero() { + DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + List result = digitSeparation.digitSeparationForwardOrder(0); + assertEquals(List.of(0L), result); + } +} From f6452c24368af501d940ac71b13171217e4654db Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 14:26:02 +0500 Subject: [PATCH 18/28] Renamed files --- ...ositiveNumber.java => DigitSeparation.java} | 4 ++-- ...umberTest.java => DigitSeparationTest.java} | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) rename src/main/java/com/thealgorithms/greedyalgorithms/{DigitSeparationPositiveNumber.java => DigitSeparation.java} (93%) rename src/test/java/com/thealgorithms/greedyalgorithms/{DigitSeparationPositiveNumberTest.java => DigitSeparationTest.java} (70%) diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumber.java b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java similarity index 93% rename from src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumber.java rename to src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java index df4159f96ddf..88356d1be68c 100644 --- a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumber.java +++ b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java @@ -7,8 +7,8 @@ /** * This class provides methods to separate the digits of a large positive number into a list. */ -public class DigitSeparationPositiveNumber { - public DigitSeparationPositiveNumber() { +public class DigitSeparation { + public DigitSeparation() { } /** * Separates the digits of a large positive number into a list in reverse order. diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumberTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java similarity index 70% rename from src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumberTest.java rename to src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java index 75c48262051e..1cd7a05afee9 100644 --- a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationPositiveNumberTest.java +++ b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java @@ -4,60 +4,60 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; -public class DigitSeparationPositiveNumberTest { +public class DigitSeparationTest { @Test public void testDigitSeparationReverseOrder_SingleDigit() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationReverseOrder(5); assertEquals(List.of(5L), result); } @Test public void testDigitSeparationReverseOrderMultipleDigits() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationReverseOrder(123); assertEquals(List.of(3L, 2L, 1L), result); } @Test public void testDigitSeparationReverseOrderLargeNumber() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationReverseOrder(123456789); assertEquals(List.of(9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), result); } @Test public void testDigitSeparationReverseOrderZero() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationReverseOrder(0); assertEquals(List.of(0L), result); } @Test public void testDigitSeparationForwardOrderSingleDigit() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationForwardOrder(5); assertEquals(List.of(5L), result); } @Test public void testDigitSeparationForwardOrderMultipleDigits() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationForwardOrder(123); assertEquals(List.of(1L, 2L, 3L), result); } @Test public void testDigitSeparationForwardOrderLargeNumber() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationForwardOrder(123456789); assertEquals(List.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), result); } @Test public void testDigitSeparationForwardOrderZero() { - DigitSeparationPositiveNumber digitSeparation = new DigitSeparationPositiveNumber(); + DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationForwardOrder(0); assertEquals(List.of(0L), result); } From 15faa54de3338f58d5a8bb884246834506e3ec0c Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 14:31:25 +0500 Subject: [PATCH 19/28] linter errors resolved --- .../com/thealgorithms/greedyalgorithms/DigitSeparation.java | 6 +++--- .../thealgorithms/greedyalgorithms/DigitSeparationTest.java | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java index 88356d1be68c..628dadcffa61 100644 --- a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java +++ b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java @@ -17,10 +17,10 @@ public DigitSeparation() { */ public List digitSeparationReverseOrder(long largeNumber) { List result = new ArrayList<>(); - if (largeNumber!=0) { + if (largeNumber != 0) { while (largeNumber > 0) { - result.add(largeNumber%10); - largeNumber = largeNumber/10; + result.add(largeNumber % 10); + largeNumber = largeNumber / 10; } } else { result.add(0L); diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java index 1cd7a05afee9..d5c204b833a8 100644 --- a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java +++ b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.List; public class DigitSeparationTest { From 278b3d01849146c40035c6a8a50e5bb0b7ee8f1f Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 14:36:35 +0500 Subject: [PATCH 20/28] linter styles corrected --- .../thealgorithms/greedyalgorithms/DigitSeparationTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java index d5c204b833a8..78ec47091839 100644 --- a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java +++ b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java @@ -1,14 +1,13 @@ package com.thealgorithms.greedyalgorithms; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; - +import org.junit.jupiter.api.Test; public class DigitSeparationTest { @Test - public void testDigitSeparationReverseOrder_SingleDigit() { + public void testDigitSeparationReverseOrderSingleDigit() { DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationReverseOrder(5); assertEquals(List.of(5L), result); From 44b985e4a26950cfa39dbcd8c325a764a8a99cd3 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 15:09:45 +0500 Subject: [PATCH 21/28] Support for negative numbers added --- .../greedyalgorithms/DigitSeparation.java | 4 ++-- .../greedyalgorithms/DigitSeparationTest.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java index 628dadcffa61..bee5f98cd2ee 100644 --- a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java +++ b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java @@ -18,8 +18,8 @@ public DigitSeparation() { public List digitSeparationReverseOrder(long largeNumber) { List result = new ArrayList<>(); if (largeNumber != 0) { - while (largeNumber > 0) { - result.add(largeNumber % 10); + while (largeNumber != 0) { + result.add(Math.abs(largeNumber % 10)); largeNumber = largeNumber / 10; } } else { diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java index 78ec47091839..7c445a5c0f85 100644 --- a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java +++ b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java @@ -34,6 +34,13 @@ public void testDigitSeparationReverseOrderZero() { assertEquals(List.of(0L), result); } + @Test + public void testDigitSeparationReverseOrderNegativeNumbers() { + DigitSeparation digitSeparation = new DigitSeparation(); + List result = digitSeparation.digitSeparationReverseOrder(-123); + assertEquals(List.of(3L, 2L, 1L), result); + } + @Test public void testDigitSeparationForwardOrderSingleDigit() { DigitSeparation digitSeparation = new DigitSeparation(); @@ -61,4 +68,11 @@ public void testDigitSeparationForwardOrderZero() { List result = digitSeparation.digitSeparationForwardOrder(0); assertEquals(List.of(0L), result); } + + @Test + public void testDigitSeparationForwardOrderNegativeNumber() { + DigitSeparation digitSeparation = new DigitSeparation(); + List result = digitSeparation.digitSeparationForwardOrder(-123); + assertEquals(List.of(3L, 2L, 1L), result); + } } From 4f6de8abdcd86768581e890cda3c6930c0f063ed Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 15:13:20 +0500 Subject: [PATCH 22/28] Tests corrected --- .../com/thealgorithms/greedyalgorithms/DigitSeparationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java index 7c445a5c0f85..1fe018ecce18 100644 --- a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java +++ b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java @@ -73,6 +73,6 @@ public void testDigitSeparationForwardOrderZero() { public void testDigitSeparationForwardOrderNegativeNumber() { DigitSeparation digitSeparation = new DigitSeparation(); List result = digitSeparation.digitSeparationForwardOrder(-123); - assertEquals(List.of(3L, 2L, 1L), result); + assertEquals(List.of(1L, 2L, 3L), result); } } From c53d8d86ec04d78e39e87f8d74c42ed82873b3f8 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 08:14:32 +0500 Subject: [PATCH 23/28] feat: Added binary addition algorithm --- .../greedyalgorithms/BinaryAddition.java | 63 ++++++++++++ .../greedyalgorithms/BinaryAdditionTest.java | 97 +++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java create mode 100644 src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java b/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java new file mode 100644 index 000000000000..af69cc47f155 --- /dev/null +++ b/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java @@ -0,0 +1,63 @@ +package com.thealgorithms.greedyalgorithms; + +import java.util.Collections; + +/** + * Solution class to perform binary addition of two binary strings. + */ +public class BinaryAddition { + /** + * Computes the sum of two binary characters and a carry. + * @param a First binary character ('0' or '1'). + * @param b Second binary character ('0' or '1'). + * @param carry The carry from the previous operation ('0' or '1'). + * @return The sum as a binary character ('0' or '1'). + */ + public char sum(char a, char b, char carry) { + int count = 0; + if (a == '1') count++; + if (b == '1') count++; + if (carry == '1') count++; + return count % 2 == 0 ? '0' : '1'; + } + /** + * Computes the carry for the next higher bit from two binary characters and a carry. + * @param a First binary character ('0' or '1'). + * @param b Second binary character ('0' or '1'). + * @param carry The carry from the previous operation ('0' or '1'). + * @return The carry for the next bit ('0' or '1'). + */ + public char carry(char a, char b, char carry) { + int count = 0; + if (a == '1') count++; + if (b == '1') count++; + if (carry == '1') count++; + return count >= 2 ? '1' : '0'; + } + /** + * Adds two binary strings and returns their sum as a binary string. + * @param a First binary string. + * @param b Second binary string. + * @return Binary string representing the sum of the two binary inputs. + */ + public String addBinary(String a, String b) { + // Padding the shorter string with leading zeros + int maxLength = Math.max(a.length(), b.length()); + a = String.join("", Collections.nCopies(maxLength - a.length(), "0")) + a; + b = String.join("", Collections.nCopies(maxLength - b.length(), "0")) + b; + StringBuilder result = new StringBuilder(); + char carry = '0'; + // Iterating over the binary strings from the least significant to the most significant bit + for (int i = maxLength - 1; i >= 0; i--) { + char sum = sum(a.charAt(i), b.charAt(i), carry); + carry = carry(a.charAt(i), b.charAt(i), carry); + result.append(sum); + } + // If there's a remaining carry, append it + if (carry == '1') { + result.append('1'); + } + // Reverse the result as we constructed it from the least significant bit + return result.reverse().toString(); + } +} diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java new file mode 100644 index 000000000000..93d1e89bfee7 --- /dev/null +++ b/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java @@ -0,0 +1,97 @@ +package com.thealgorithms.greedyalgorithms; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class BinaryAdditionTest { + + BinaryAddition binaryAddition = new BinaryAddition(); + + @Test + public void testEqualLengthNoCarry() { + String a = "1010"; + String b = "1101"; + String expected = "10111"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testEqualLengthWithCarry() { + String a = "1111"; + String b = "1111"; + String expected = "11110"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testDifferentLengths() { + String a = "101"; + String b = "11"; + String expected = "1000"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testAllZeros() { + String a = "0"; + String b = "0"; + String expected = "0"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + + @Test + public void testAllOnes() { + String a = "1111"; + String b = "1111"; + String expected = "11110"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testOneZeroString() { + String a = "0"; + String b = "10101"; + String expected = "10101"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + + // Test the other way around + a = "10101"; + b = "0"; + expected = "10101"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testLargeBinaryNumbers() { + String a = "101010101010101010101010101010"; + String b = "110110110110110110110110110110"; + String expected = "1100001100001100001100001100000"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testOneMuchLonger() { + String a = "1"; + String b = "11111111"; + String expected = "100000000"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testEmptyStrings() { + String a = ""; + String b = ""; + String expected = "0"; // Adding two empty strings should return 0 + assertEquals(expected, binaryAddition.addBinary(a, b)); + } + + @Test + public void testAlternatingBits() { + String a = "10101010"; + String b = "01010101"; + String expected = "11111111"; + assertEquals(expected, binaryAddition.addBinary(a, b)); + } +} + From e9d9d9c06ab3982e37390aa70ac366fba536edba Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 08:25:23 +0500 Subject: [PATCH 24/28] "Updated JavaDoc comment to match class name in BinaryAddition.java" --- .../java/com/thealgorithms/greedyalgorithms/BinaryAddition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java b/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java index af69cc47f155..cd46dcd21ab9 100644 --- a/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java +++ b/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java @@ -3,7 +3,7 @@ import java.util.Collections; /** - * Solution class to perform binary addition of two binary strings. + * BinaryAddition class to perform binary addition of two binary strings. */ public class BinaryAddition { /** From eeb8daf96e68daee54ec5d450f87f55fc772944c Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 03:25:47 +0000 Subject: [PATCH 25/28] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1bad5d3b98a3..53b479172147 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -269,6 +269,7 @@ * [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java) * greedyalgorithms * [ActivitySelection](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java) + * [BinaryAddition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java) * [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java) * [DigitSeparation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java) * [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java) @@ -768,6 +769,7 @@ * [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java) * greedyalgorithms * [ActivitySelectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/ActivitySelectionTest.java) + * [BinaryAdditionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java) * [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/CoinChangeTest.java) * [DigitSeparationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java) * [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java) From 77d0b95042c9bb184adbd73130ca54dd64d95dc7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 08:31:27 +0500 Subject: [PATCH 26/28] Updated test file --- .../thealgorithms/greedyalgorithms/BinaryAdditionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java index 93d1e89bfee7..43bcdad16ef8 100644 --- a/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java +++ b/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.greedyalgorithms; +import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; public class BinaryAdditionTest { @@ -82,7 +82,7 @@ public void testOneMuchLonger() { public void testEmptyStrings() { String a = ""; String b = ""; - String expected = "0"; // Adding two empty strings should return 0 + String expected = ""; // Adding two empty strings should return 0 assertEquals(expected, binaryAddition.addBinary(a, b)); } From 516e464132f5fd862a293584cb5595461057297b Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 08:37:22 +0500 Subject: [PATCH 27/28] clang linted --- .../greedyalgorithms/BinaryAddition.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java b/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java index cd46dcd21ab9..074c76b9f33f 100644 --- a/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java +++ b/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java @@ -15,9 +15,15 @@ public class BinaryAddition { */ public char sum(char a, char b, char carry) { int count = 0; - if (a == '1') count++; - if (b == '1') count++; - if (carry == '1') count++; + if (a == '1') { + count++; + } + if (b == '1') { + count++; + } + if (carry == '1') { + count++; + } return count % 2 == 0 ? '0' : '1'; } /** @@ -29,9 +35,15 @@ public char sum(char a, char b, char carry) { */ public char carry(char a, char b, char carry) { int count = 0; - if (a == '1') count++; - if (b == '1') count++; - if (carry == '1') count++; + if (a == '1') { + count++; + } + if (b == '1') { + count++; + } + if (carry == '1') { + count++; + } return count >= 2 ? '1' : '0'; } /** From e623c9b99b742b8d4aaafdf982a0d42cf6151283 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 08:39:47 +0500 Subject: [PATCH 28/28] tests clang linted --- .../com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java index 43bcdad16ef8..893ca02ed8a3 100644 --- a/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java +++ b/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.greedyalgorithms; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; public class BinaryAdditionTest { @@ -39,7 +40,6 @@ public void testAllZeros() { assertEquals(expected, binaryAddition.addBinary(a, b)); } - @Test public void testAllOnes() { String a = "1111"; @@ -94,4 +94,3 @@ public void testAlternatingBits() { assertEquals(expected, binaryAddition.addBinary(a, b)); } } -