From 8e9482682c205d4d5188ad3eb227efb95b8079b7 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 17 Sep 2020 09:08:23 +0800 Subject: [PATCH 01/10] actions on pull requests --- .github/workflows/gradle.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 3f635f41..05cef81e 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -1,9 +1,8 @@ name: Java gradle CI -on: - push: - branches: - - Development +on: [push, pull_request] + branches: + - Development jobs: test: From bdaabbb2aa8959b8bef58358639a57edd3f1aee3 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 17 Sep 2020 09:17:36 +0800 Subject: [PATCH 02/10] fixed syntax error --- .github/workflows/gradle.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 05cef81e..6631507a 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -1,8 +1,12 @@ name: Java gradle CI -on: [push, pull_request] - branches: - - Development +on: + push: + branches: + - Development + pull_request: + branches: + - Development jobs: test: From 72efcaa5647f25a29b625ccf7b4c9e762fa951a2 Mon Sep 17 00:00:00 2001 From: geogiadim Date: Wed, 16 Sep 2020 19:19:50 +0300 Subject: [PATCH 03/10] Implementation for Round Robin Algorithm in Java with tests --- src/main/java/com/others/RoundRobin.java | 141 +++++++++++++++++++ src/test/java/com/others/RoundRobinTest.java | 26 ++++ 2 files changed, 167 insertions(+) create mode 100644 src/main/java/com/others/RoundRobin.java create mode 100644 src/test/java/com/others/RoundRobinTest.java diff --git a/src/main/java/com/others/RoundRobin.java b/src/main/java/com/others/RoundRobin.java new file mode 100644 index 00000000..cb17853d --- /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 readyQueue = new ArrayList<>(); + for(int i=0;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 Date: Thu, 19 Nov 2020 05:08:24 +1100 Subject: [PATCH 06/10] Add New String Interleave Class and Tests (#2032) * added an Interleave class * added interleaving sequence url * Fixes: #2031 Co-authored-by: u6943702 --- src/main/java/com/string/Interleave.java | 151 +++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/main/java/com/string/Interleave.java 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; + } +} From 1012ff74f3779d12282843ca910a2f97a2782cad Mon Sep 17 00:00:00 2001 From: Lovesh Dongre Date: Sun, 22 Nov 2020 23:14:40 +0530 Subject: [PATCH 07/10] ReverseWord & ReverseWordsTest (#2022) * ReverseWord & ReverseWordsTest * ReverseWord & ReverseWordsTest * fixed ReverseWords * Merge branch 'master' of https://github.com/TheAlgorithms/Java into Development --- src/main/java/com/string/ReverseWords.java | 30 +++++++++++++++++++ .../java/com/string/ReverseWordsTest.java | 15 ++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/com/string/ReverseWords.java create mode 100644 src/test/java/com/string/ReverseWordsTest.java diff --git a/src/main/java/com/string/ReverseWords.java b/src/main/java/com/string/ReverseWords.java new file mode 100644 index 00000000..fc9185c1 --- /dev/null +++ b/src/main/java/com/string/ReverseWords.java @@ -0,0 +1,30 @@ +package com.strings; + +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 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/test/java/com/string/ReverseWordsTest.java b/src/test/java/com/string/ReverseWordsTest.java new file mode 100644 index 00000000..8cfb3b5b --- /dev/null +++ b/src/test/java/com/string/ReverseWordsTest.java @@ -0,0 +1,15 @@ +package com.string; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class ReveresWordsTest { + + @Test + void testReverseWords() { + ReverseWords reverseWords = new ReverseWords(); + Assertions.assertEquals(true, reverseWords.returnReverseWords("this is my car"), "siht si ym rac"); + Assertions.assertEquals(true, reverseWords.returnReverseWords("ABC 123"), "CBA 321"); + + } +} \ No newline at end of file From 8eb27d712f6b0a80880459b4a1531b3512dce8a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20=C5=81ukaszewicz?= <26778292+5n0wwh1t3@users.noreply.github.com> Date: Sun, 22 Nov 2020 18:08:36 +0000 Subject: [PATCH 08/10] Added Ackermann function (#794) * Ackermann added. * Ackermann updated --- src/main/java/com/others/Ackermann.java | 34 +++++++++++++++++++++ src/test/java/com/others/AckermannTest.java | 19 ++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/com/others/Ackermann.java create mode 100644 src/test/java/com/others/AckermannTest.java 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/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)); + } + + + +} From 09765b8ab4b0eacfa55e13dcd4f8fe7b1eaf1179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jake=20Antonio=20Ruiz=20Zeled=C3=B3n?= Date: Mon, 30 Nov 2020 23:49:02 -0600 Subject: [PATCH 09/10] Incorporate the class Upper from string package (#1952) --- src/main/java/com/string/Upper.java | 20 ++++++++++++++++++++ src/test/java/com/string/UpperTest.java | 15 +++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/main/java/com/string/Upper.java create mode 100644 src/test/java/com/string/UpperTest.java 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/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 From bd60e13d7958adc172fc28f09293c49194b942ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jake=20Antonio=20Ruiz=20Zeled=C3=B3n?= Date: Mon, 4 Jan 2021 06:15:31 -0600 Subject: [PATCH 10/10] Fix ReverseWords class and test (#2077) --- src/main/java/com/string/ReverseWords.java | 4 ++-- src/test/java/com/string/ReverseWordsTest.java | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/string/ReverseWords.java b/src/main/java/com/string/ReverseWords.java index fc9185c1..9a4dec90 100644 --- a/src/main/java/com/string/ReverseWords.java +++ b/src/main/java/com/string/ReverseWords.java @@ -1,4 +1,4 @@ -package com.strings; +package com.string; public class ReverseWords { /** @@ -8,7 +8,7 @@ public class ReverseWords { * @return the {@code String}, converted to a string with reveresed words. */ - public String returnReverseWords(String s) { + public static String returnReverseWords(String s) { StringBuilder sb = new StringBuilder(); StringBuilder word = new StringBuilder(); diff --git a/src/test/java/com/string/ReverseWordsTest.java b/src/test/java/com/string/ReverseWordsTest.java index 8cfb3b5b..590c50f8 100644 --- a/src/test/java/com/string/ReverseWordsTest.java +++ b/src/test/java/com/string/ReverseWordsTest.java @@ -3,13 +3,11 @@ package com.string; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -class ReveresWordsTest { +class ReverseWordsTest extends ReverseWords { @Test void testReverseWords() { - ReverseWords reverseWords = new ReverseWords(); - Assertions.assertEquals(true, reverseWords.returnReverseWords("this is my car"), "siht si ym rac"); - Assertions.assertEquals(true, reverseWords.returnReverseWords("ABC 123"), "CBA 321"); - + 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