[BAEL-12908] - Created algorithms-miscellaneous-3 module
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.algorithms.enumstatemachine;
|
||||
|
||||
public enum LeaveRequestState {
|
||||
|
||||
Submitted {
|
||||
@Override
|
||||
public LeaveRequestState nextState() {
|
||||
System.out.println("Starting the Leave Request and sending to Team Leader for approval.");
|
||||
return Escalated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String responsiblePerson() {
|
||||
return "Employee";
|
||||
}
|
||||
},
|
||||
Escalated {
|
||||
@Override
|
||||
public LeaveRequestState nextState() {
|
||||
System.out.println("Reviewing the Leave Request and escalating to Department Manager.");
|
||||
return Approved;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String responsiblePerson() {
|
||||
return "Team Leader";
|
||||
}
|
||||
},
|
||||
Approved {
|
||||
@Override
|
||||
public LeaveRequestState nextState() {
|
||||
System.out.println("Approving the Leave Request.");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String responsiblePerson() {
|
||||
return "Department Manager";
|
||||
}
|
||||
};
|
||||
|
||||
public abstract String responsiblePerson();
|
||||
|
||||
public abstract LeaveRequestState nextState();
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.algorithms.twopointertechnique;
|
||||
|
||||
public class LinkedListFindMiddle {
|
||||
|
||||
public <T> T findMiddle(MyNode<T> head) {
|
||||
MyNode<T> slowPointer = head;
|
||||
MyNode<T> fastPointer = head;
|
||||
|
||||
while (fastPointer.next != null && fastPointer.next.next != null) {
|
||||
fastPointer = fastPointer.next.next;
|
||||
slowPointer = slowPointer.next;
|
||||
}
|
||||
return slowPointer.data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.algorithms.twopointertechnique;
|
||||
|
||||
public class MyNode<E> {
|
||||
MyNode<E> next;
|
||||
E data;
|
||||
|
||||
public MyNode(E value) {
|
||||
data = value;
|
||||
next = null;
|
||||
}
|
||||
|
||||
public MyNode(E value, MyNode<E> n) {
|
||||
data = value;
|
||||
next = n;
|
||||
}
|
||||
|
||||
public void setNext(MyNode<E> n) {
|
||||
next = n;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.algorithms.twopointertechnique;
|
||||
|
||||
public class RotateArray {
|
||||
|
||||
public void rotate(int[] input, int step) {
|
||||
step %= input.length;
|
||||
reverse(input, 0, input.length - 1);
|
||||
reverse(input, 0, step - 1);
|
||||
reverse(input, step, input.length - 1);
|
||||
}
|
||||
|
||||
private void reverse(int[] input, int start, int end) {
|
||||
while (start < end) {
|
||||
int temp = input[start];
|
||||
input[start] = input[end];
|
||||
input[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.algorithms.twopointertechnique;
|
||||
|
||||
public class TwoSum {
|
||||
|
||||
public boolean twoSum(int[] input, int targetValue) {
|
||||
|
||||
int pointerOne = 0;
|
||||
int pointerTwo = input.length - 1;
|
||||
|
||||
while (pointerOne < pointerTwo) {
|
||||
int sum = input[pointerOne] + input[pointerTwo];
|
||||
|
||||
if (sum == targetValue) {
|
||||
return true;
|
||||
} else if (sum < targetValue) {
|
||||
pointerOne++;
|
||||
} else {
|
||||
pointerTwo--;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean twoSumSlow(int[] input, int targetValue) {
|
||||
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
for (int j = 1; j < input.length; j++) {
|
||||
if (input[i] + input[j] == targetValue) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.algorithms.enumstatemachine;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class LeaveRequestStateUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLeaveRequest_whenStateEscalated_thenResponsibleIsTeamLeader() {
|
||||
LeaveRequestState state = LeaveRequestState.Escalated;
|
||||
|
||||
assertEquals(state.responsiblePerson(), "Team Leader");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenLeaveRequest_whenStateApproved_thenResponsibleIsDepartmentManager() {
|
||||
LeaveRequestState state = LeaveRequestState.Approved;
|
||||
|
||||
assertEquals(state.responsiblePerson(), "Department Manager");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLeaveRequest_whenNextStateIsCalled_thenStateIsChanged() {
|
||||
LeaveRequestState state = LeaveRequestState.Submitted;
|
||||
|
||||
state = state.nextState();
|
||||
assertEquals(state, LeaveRequestState.Escalated);
|
||||
|
||||
state = state.nextState();
|
||||
assertEquals(state, LeaveRequestState.Approved);
|
||||
|
||||
state = state.nextState();
|
||||
assertEquals(state, LeaveRequestState.Approved);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.algorithms.twopointertechnique;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class LinkedListFindMiddleUnitTest {
|
||||
|
||||
LinkedListFindMiddle linkedListFindMiddle = new LinkedListFindMiddle();
|
||||
|
||||
@Test
|
||||
public void givenLinkedListOfMyNodes_whenLinkedListFindMiddle_thenCorrect() {
|
||||
|
||||
MyNode<String> head = createNodesList(8);
|
||||
|
||||
assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("4");
|
||||
|
||||
head = createNodesList(9);
|
||||
|
||||
assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("5");
|
||||
}
|
||||
|
||||
private static MyNode<String> createNodesList(int n) {
|
||||
|
||||
MyNode<String> head = new MyNode<String>("1");
|
||||
MyNode<String> current = head;
|
||||
|
||||
for (int i = 2; i <= n; i++) {
|
||||
MyNode<String> newNode = new MyNode<String>(String.valueOf(i));
|
||||
current.setNext(newNode);
|
||||
current = newNode;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.algorithms.twopointertechnique;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RotateArrayUnitTest {
|
||||
|
||||
private RotateArray rotateArray = new RotateArray();
|
||||
|
||||
private int[] inputArray;
|
||||
|
||||
private int step;
|
||||
|
||||
@Test
|
||||
public void givenAnArrayOfIntegers_whenRotateKsteps_thenCorrect() {
|
||||
|
||||
inputArray = new int[] { 1, 2, 3, 4, 5, 6, 7 };
|
||||
step = 4;
|
||||
|
||||
rotateArray.rotate(inputArray, step);
|
||||
|
||||
assertThat(inputArray).containsExactly(new int[] { 4, 5, 6, 7, 1, 2, 3 });
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.algorithms.twopointertechnique;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TwoSumUnitTest {
|
||||
|
||||
private TwoSum twoSum = new TwoSum();
|
||||
|
||||
private int[] sortedArray;
|
||||
|
||||
private int targetValue;
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairExists() {
|
||||
|
||||
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
|
||||
targetValue = 12;
|
||||
|
||||
assertTrue(twoSum.twoSumSlow(sortedArray, targetValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairDoesNotExists() {
|
||||
|
||||
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
|
||||
targetValue = 20;
|
||||
|
||||
assertFalse(twoSum.twoSumSlow(sortedArray, targetValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenTwoSum_thenPairExists() {
|
||||
|
||||
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
|
||||
targetValue = 12;
|
||||
|
||||
assertTrue(twoSum.twoSum(sortedArray, targetValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenTwoSum_thenPairDoesNotExists() {
|
||||
|
||||
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
|
||||
targetValue = 20;
|
||||
|
||||
assertFalse(twoSum.twoSum(sortedArray, targetValue));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user