diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 3f635f41..6631507a 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -4,6 +4,9 @@ on: push: branches: - Development + pull_request: + branches: + - Development jobs: test: diff --git a/src/main/java/com/others/Ackermann.java b/src/main/java/com/others/Ackermann.java new file mode 100644 index 00000000..a06cfaa6 --- /dev/null +++ b/src/main/java/com/others/Ackermann.java @@ -0,0 +1,34 @@ +package src.main.java.com.others; + + +public class Ackermann { + + + /** + * Ackermann function - simplest and earliest-discovered examples of a total computable function + * that is not primitive recursive. + * + * Defined only for NONNEGATIVE integers !!! + * + * Time complexity is super-exponential. O(n(^)) + * Any input m higher tahn (3,3) will result in StackOverflow + * @param m + * @param n + * @return + * + * + */ + public long Ack(long m, long n) { + + if (m == 0) + return n + 1; + + if (n == 0) + return Ack(m - 1, 1); + + return Ack(m - 1, Ack(m, n - 1)); + } + +} + + diff --git a/src/main/java/com/others/RoundRobin.java b/src/main/java/com/others/RoundRobin.java new file mode 100644 index 00000000..b50cebeb --- /dev/null +++ b/src/main/java/com/others/RoundRobin.java @@ -0,0 +1,141 @@ +package src.main.java.com.others; + +import java.util.ArrayList; + +/** + * This class implements the Round Robin Algorithm which is an cpu scheduling algorithm. + * + * @author George Giannios + */ +public class RoundRobin { + /** + * This method calculates the waiting time for all processes + * + * @param burstTime an array with burst time for all processes + * @param quantum the quantum quantity + * @return an array with waiting time for all processes + */ + public int[] calcWaitingTime(int[] burstTime, int quantum) { + int n = burstTime.length; + //create a copy of burstTime table to executeTime table + int[] executeTIme = new int[n]; + for (int i = 0; i < n; i++) { + executeTIme[i] = burstTime[i]; + } + + //initialize the waiting time table and set all waiting times equal to zero + int[] waitingTime = new int[n]; + for (int i = 0; i < n; i++) { + waitingTime[i] = 0; + } + + //initialize an array list to emulate the queue of ready processes + ArrayList readyQueue = new ArrayList<>(); + for (int i = 0; i < n; i++) { + readyQueue.add(i); + } + + //the total time that processes need to be finished + int time = 0; + int i = 0; + //calculate waiting times while there are uncompleted processes + while (!readyQueue.isEmpty()) { + //check if a process has finished + if (executeTIme[i] >= 0) { + if (executeTIme[i] - quantum > 0) { + //add time that have been passed + time += quantum; + //this is the remaining burst time for the process i + executeTIme[i] -= quantum; + + } else if (executeTIme[i] - quantum == 0) { + //add time that have been passed + time += quantum; + //calculate the total waiting time + waitingTime[i] = time - burstTime[i]; + + //mark the process as finished + executeTIme[i] = -1; + //remove the process that have finished by shrinking queue's length + readyQueue.remove(readyQueue.size() - 1); + + } else { + //add time that have been passed + time += executeTIme[i]; + //calculate the total waiting time + waitingTime[i] = time - burstTime[i]; + + //mark the process as finished + executeTIme[i] = -1; + //remove the process that have finished by shrinking queue's length + readyQueue.remove(readyQueue.size() - 1); + } + } + i++; + if (i >= n) { + i = 0; + } + } + + return waitingTime; + } + + + /** + * This method calculates turn around time for all processes + * + * @param burstTime an array with burst time for all processes + * @param waitingTime an array with waiting time for all processes + * @return an array with turnaround time for all processes + */ + public int[] calcTurnAroundTime(int[] burstTime, int[] waitingTime) { + int n = burstTime.length; + //initialize the turnaround time table + int[] turnAroundTime = new int[n]; + + //calculate turnaround time for each process (T.T= W.T + B.T) + for (int i = 0; i < n; i++) { + turnAroundTime[i] = waitingTime[i] + burstTime[i]; + } + + //return the turnaround time table + return turnAroundTime; + } + + + /** + * This method prints the results and calculates the average waiting and turnaround times + * + * @param burstTime an array with burst time for all processes + * @param quantum the quantum quantity + */ + void printAvgTimes(int[] burstTime, int quantum) { + int n = burstTime.length; + int totalWaitingTime = 0; + int totalTurnAroundTime = 0; + + // Find waiting time of all processes + int[] waitingTime = calcWaitingTime(burstTime, quantum); + + // Find turn around time for all processes + int[] turnAroundTime = calcTurnAroundTime(burstTime, waitingTime); + + // Display processes along with all details + System.out.println("Process " + " Burst Time " + + " Waiting Time " + " Turnaround Time"); + System.out.println("======= ========== ============ ==============="); + // Calculate total waiting time and total turn around time + for (int i = 0; i < n; i++) { + totalWaitingTime += waitingTime[i]; + totalTurnAroundTime += turnAroundTime[i]; + System.out.println(i + "\t\t " + burstTime[i] + "\t\t\t " + + waitingTime[i] + "\t\t\t " + turnAroundTime[i]); + } + + System.out.println("\nAverage waiting time = " + + (float) totalWaitingTime / (float) n); + System.out.println("Average turnaround time = " + + (float) totalTurnAroundTime / (float) n); + } +} + diff --git a/src/main/java/com/string/Alphabetical.java b/src/main/java/com/string/Alphabetical.java new file mode 100644 index 00000000..84b0f1c8 --- /dev/null +++ b/src/main/java/com/string/Alphabetical.java @@ -0,0 +1,20 @@ +package com.string; + +public class Alphabetical { + + /** + * Check if a string is alphabetical order or not + * + * @param s a string + * @return {@code true} if given string is alphabetical order, otherwise {@code false} + */ + public static boolean isAlphabetical(String s) { + s = s.toLowerCase(); + for (int i = 0; i < s.length() - 1; ++i) { + if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/com/string/Interleave.java b/src/main/java/com/string/Interleave.java new file mode 100644 index 00000000..67a8311b --- /dev/null +++ b/src/main/java/com/string/Interleave.java @@ -0,0 +1,151 @@ +package com.string; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class Interleave { + @Test + public void testInterleaveRegularString() { + String string1 = "Hello"; + String string2 = "World"; + String expected = "HWeolrllod"; + String actual = interleave(string1, string2); + assertEquals("Incorrect result from method.", expected, actual); + } + + @Test + public void testInterleaveEmptyString() { + String string1 = ""; + String string2 = ""; + String string3 = "a"; + String string4 = "abc"; + String expected1 = ""; + String actual1 = interleave(string1, string2); + String expected2 = "a"; + String actual2 = interleave(string1, string3); + String expected3 = "abc"; + String actual3 = interleave(string1, string4); + assertEquals("Incorrect result from method.", expected1, actual1); + assertEquals("Incorrect result from method.", expected2, actual2); + assertEquals("Incorrect result from method.", expected3, actual3); + } + + @Test + public void testInterleaveSingleString() { + String string1 = "a"; + String string2 = "b"; + String expected = "ab"; + String actual = interleave(string1, string2); + assertEquals("Incorrect result from method.", expected, actual); + } + + @Test + public void testInterleaveIntString() { + String string1 = "1"; + String string2 = "7"; + String expected = "17"; + String actual = interleave(string1, string2); + assertEquals("Incorrect result from method.", expected, actual); + } + + @Test + public void testInterleaveMixedString() { + String string1 = "1a2b3c4d"; + String string2 = "5e6f7g8h"; + String expected = "15ae26bf37cg48dh"; + String actual = interleave(string1, string2); + assertEquals("Incorrect result from method.", expected, actual); + } + + @Test + public void testInterleaveSymbols() { + String string1 = "a@b%c/"; + String string2 = "d#e$g%."; + String expected = "ad@#be%$cg/%."; + String actual = interleave(string1, string2); + assertEquals("Incorrect result from method.", expected, actual); + } + + @Test + public void testInterleaveSpaces() { // This string interleave algorithm defines a space as a valid character. + String string1 = " "; + String string2 = "a"; + String string3 = "5 g"; + String string4 = " 4 d "; + String expected1 = " a"; + String actual1 = interleave(string1, string2); + String expected2 = "a5 g"; + String actual2 = interleave(string2, string3); + String expected3 = "5 4g d "; + String actual3 = interleave(string3, string4); + assertEquals("Incorrect result from method.", expected1, actual1); + assertEquals("Incorrect result from method.", expected2, actual2); + assertEquals("Incorrect result from method.", expected3, actual3); + } + + /** + * This method "interweaves" two input strings one character at a time. The first character of the + * first parameter string always starts the resulting string, unless that character is a space or is empty. + * This string interleaving method takes a space in a string (e.g. " ") into consideration. + * + * For example, if string1 = "abc" and string2 = "def", then the result would be "adbecf", as the first character + * of the string1 is 'a', then the first character of string2 is 'd', and so forth. + * + * For more information on interleaving, check out: https://en.wikipedia.org/wiki/Interleave_sequence + * + * @param string1 + * @param string2 + * @return string resulting from the interweaving of the two input strings; string1 and string2. + */ + public String interleave(String string1, String string2) { + String result = ""; // The final interleaved string to return. + List list1 = new ArrayList<>(); // The ArrayList of string1, with each character being an individual element. + List list2 = new ArrayList<>(); // The ArrayList of string2, in a similar manner as above. + + for (int i = 0; i < string1.length(); i++) // Convert string1 into list1. + list1.add(string1.charAt(i)); + + for (int i = 0; i < string2.length(); i++) // Convert string2 into list2. + list2.add(string2.charAt(i)); + + if (string1.length() == string2.length()) { // Interleaving when string1 and string2 are equal length. + for (int j = 0; j < list1.size(); j++) { + result = result + list1.get(j); + result = result + list2.get(j); + } + return result; + } + + if (string1.length() > string2.length()) { // Interleaving when string1 is longer than string2. + while (list2.size() > 0) { + result = result + list1.get(0); + list1.remove(0); + result = result + list2.get(0); + list2.remove(0); + } + for (char character : list1) { // Concatenate the rest of the characters in list1 to the result. + result = result + character; + } + return result; + } + + if (string2.length() > string1.length()) { // Interleaving when string2 is longer than string1. + while (list1.size() > 0) { + result = result + list1.get(0); + list1.remove(0); + result = result + list2.get(0); + list2.remove(0); + } + for (char character : list2) { // Concatenate the rest of the characters in list2 to the result. + result = result + character; + } + return result; + } + + return result; + } +} diff --git a/src/main/java/com/string/ReverseWords.java b/src/main/java/com/string/ReverseWords.java new file mode 100644 index 00000000..9a4dec90 --- /dev/null +++ b/src/main/java/com/string/ReverseWords.java @@ -0,0 +1,30 @@ +package com.string; + +public class ReverseWords { + /** + * Converts all of the words in this {@code String} to reversed words + * + * @param s the string to convert + * @return the {@code String}, converted to a string with reveresed words. + */ + + public static String returnReverseWords(String s) { + StringBuilder sb = new StringBuilder(); + StringBuilder word = new StringBuilder(); + + for(int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if(c == ' ') { + + sb.append(word); + sb.append(" "); + word.setLength(0); + continue; + } + word.insert(0, c); + } + sb.append(word); + + return sb.toString(); + } +} diff --git a/src/main/java/com/string/Upper.java b/src/main/java/com/string/Upper.java new file mode 100644 index 00000000..87e2abe1 --- /dev/null +++ b/src/main/java/com/string/Upper.java @@ -0,0 +1,20 @@ +package com.string; + +public class Upper { + + /** + * Converts all of the characters in this {@code String} to upper case + * + * @param s the string to convert + * @return the {@code String}, converted to uppercase. + */ + public static String toUpperCase(String s) { + char[] values = s.toCharArray(); + for (int i = 0; i < values.length; ++i) { + if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) { + values[i] = Character.toUpperCase(values[i]); + } + } + return new String(values); + } +} diff --git a/src/test/java/com/others/AckermannTest.java b/src/test/java/com/others/AckermannTest.java new file mode 100644 index 00000000..c45b5542 --- /dev/null +++ b/src/test/java/com/others/AckermannTest.java @@ -0,0 +1,19 @@ +package src.test.java.com.others; + +import src.main.java.com.others.Ackermann; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class AckermannTest { + + @Test + public void testAckermann() { + Ackermann ackTest = new Ackermann(); + assertEquals("Error", 1, ackTest.Ack(0, 0)); + assertEquals("Error", 3, ackTest.Ack(1, 1)); + assertEquals("Error", 7, ackTest.Ack(2, 2)); + } + + + +} diff --git a/src/test/java/com/others/RoundRobinTest.java b/src/test/java/com/others/RoundRobinTest.java new file mode 100644 index 00000000..b3fa8252 --- /dev/null +++ b/src/test/java/com/others/RoundRobinTest.java @@ -0,0 +1,26 @@ +package src.test.java.com.others; + +import org.junit.Assert; +import org.junit.Test; + +import src.main.java.com.others.RoundRobin; + +public class RoundRobinTest { + private final int[] burstTime = {5, 15, 4, 3}; + private final RoundRobin roundRobin = new RoundRobin(); + + @Test + public void testWaitingTime() { + int[] expectedTime = {9, 12, 14, 9}; + int[] realtime = roundRobin.calcWaitingTime(burstTime, 3); + Assert.assertArrayEquals(realtime, expectedTime); + } + + @Test + public void testTurnAroundTIme() { + int[] expectedTIme = {14, 27, 18, 12}; + int[] waitingTime = {9, 12, 14, 9}; + int[] realTime = roundRobin.calcTurnAroundTime(burstTime, waitingTime); + Assert.assertArrayEquals(realTime, expectedTIme); + } +} diff --git a/src/test/java/com/string/AlphabeticalTest.java b/src/test/java/com/string/AlphabeticalTest.java new file mode 100644 index 00000000..7291546a --- /dev/null +++ b/src/test/java/com/string/AlphabeticalTest.java @@ -0,0 +1,13 @@ +package com.string; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class AlphabeticalTest extends Alphabetical { + + @Test + void testAlphabetical() { + Assertions.assertEquals(true, isAlphabetical("abc"), "The string is in order"); + Assertions.assertEquals(false, isAlphabetical("testing"), "The string is not in order"); + } +} \ No newline at end of file diff --git a/src/test/java/com/string/ReverseWordsTest.java b/src/test/java/com/string/ReverseWordsTest.java new file mode 100644 index 00000000..590c50f8 --- /dev/null +++ b/src/test/java/com/string/ReverseWordsTest.java @@ -0,0 +1,13 @@ +package com.string; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class ReverseWordsTest extends ReverseWords { + + @Test + void testReverseWords() { + Assertions.assertEquals(true, returnReverseWords("this is my car").equals("siht si ym rac"), "Correct"); + Assertions.assertEquals(true, returnReverseWords("ABC 123").equals("CBA 321"), "Correct"); + } +} \ No newline at end of file diff --git a/src/test/java/com/string/UpperTest.java b/src/test/java/com/string/UpperTest.java new file mode 100644 index 00000000..0f99b70d --- /dev/null +++ b/src/test/java/com/string/UpperTest.java @@ -0,0 +1,15 @@ +package com.string; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class UpperTest extends Upper { + + @Test + void testUpper() { + Assertions.assertEquals(toUpperCase("abc"), ("abc").toUpperCase(), "The strings are equals"); + //Assertions fail for functional reasons + Assertions.assertEquals(toUpperCase("abc"), "abc", "The strings are not equals"); + } + +} \ No newline at end of file