Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitda85d41

Browse files
author
JohnKara
committed
Merge remote-tracking branch 'origin/Development' into Development
2 parentsb9dcb85 +bd60e13 commitda85d41

File tree

12 files changed

+485
-0
lines changed

12 files changed

+485
-0
lines changed

‎.github/workflows/gradle.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ on:
44
push:
55
branches:
66
-Development
7+
pull_request:
8+
branches:
9+
-Development
710

811
jobs:
912
test:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagesrc.main.java.com.others;
2+
3+
4+
publicclassAckermann {
5+
6+
7+
/**
8+
* Ackermann function - simplest and earliest-discovered examples of a total computable function
9+
* that is not primitive recursive.
10+
*
11+
* Defined only for NONNEGATIVE integers !!!
12+
*
13+
* Time complexity is super-exponential. O(n(^))
14+
* Any input m higher tahn (3,3) will result in StackOverflow
15+
* @param m
16+
* @param n
17+
* @return
18+
*
19+
*
20+
*/
21+
publiclongAck(longm,longn) {
22+
23+
if (m ==0)
24+
returnn +1;
25+
26+
if (n ==0)
27+
returnAck(m -1,1);
28+
29+
returnAck(m -1,Ack(m,n -1));
30+
}
31+
32+
}
33+
34+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
packagesrc.main.java.com.others;
2+
3+
importjava.util.ArrayList;
4+
5+
/**
6+
* This class implements the Round Robin Algorithm which is an cpu scheduling algorithm.
7+
*
8+
* @author George Giannios
9+
*/
10+
publicclassRoundRobin {
11+
/**
12+
* This method calculates the waiting time for all processes
13+
*
14+
* @param burstTime an array with burst time for all processes
15+
* @param quantum the quantum quantity
16+
* @return an array with waiting time for all processes
17+
*/
18+
publicint[]calcWaitingTime(int[]burstTime,intquantum) {
19+
intn =burstTime.length;
20+
//create a copy of burstTime table to executeTime table
21+
int[]executeTIme =newint[n];
22+
for (inti =0;i <n;i++) {
23+
executeTIme[i] =burstTime[i];
24+
}
25+
26+
//initialize the waiting time table and set all waiting times equal to zero
27+
int[]waitingTime =newint[n];
28+
for (inti =0;i <n;i++) {
29+
waitingTime[i] =0;
30+
}
31+
32+
//initialize an array list to emulate the queue of ready processes
33+
ArrayList<Integer>readyQueue =newArrayList<>();
34+
for (inti =0;i <n;i++) {
35+
readyQueue.add(i);
36+
}
37+
38+
//the total time that processes need to be finished
39+
inttime =0;
40+
inti =0;
41+
//calculate waiting times while there are uncompleted processes
42+
while (!readyQueue.isEmpty()) {
43+
//check if a process has finished
44+
if (executeTIme[i] >=0) {
45+
if (executeTIme[i] -quantum >0) {
46+
//add time that have been passed
47+
time +=quantum;
48+
//this is the remaining burst time for the process i
49+
executeTIme[i] -=quantum;
50+
51+
}elseif (executeTIme[i] -quantum ==0) {
52+
//add time that have been passed
53+
time +=quantum;
54+
//calculate the total waiting time
55+
waitingTime[i] =time -burstTime[i];
56+
57+
//mark the process as finished
58+
executeTIme[i] = -1;
59+
//remove the process that have finished by shrinking queue's length
60+
readyQueue.remove(readyQueue.size() -1);
61+
62+
}else {
63+
//add time that have been passed
64+
time +=executeTIme[i];
65+
//calculate the total waiting time
66+
waitingTime[i] =time -burstTime[i];
67+
68+
//mark the process as finished
69+
executeTIme[i] = -1;
70+
//remove the process that have finished by shrinking queue's length
71+
readyQueue.remove(readyQueue.size() -1);
72+
}
73+
}
74+
i++;
75+
if (i >=n) {
76+
i =0;
77+
}
78+
}
79+
80+
returnwaitingTime;
81+
}
82+
83+
84+
/**
85+
* This method calculates turn around time for all processes
86+
*
87+
* @param burstTime an array with burst time for all processes
88+
* @param waitingTime an array with waiting time for all processes
89+
* @return an array with turnaround time for all processes
90+
*/
91+
publicint[]calcTurnAroundTime(int[]burstTime,int[]waitingTime) {
92+
intn =burstTime.length;
93+
//initialize the turnaround time table
94+
int[]turnAroundTime =newint[n];
95+
96+
//calculate turnaround time for each process (T.T= W.T + B.T)
97+
for (inti =0;i <n;i++) {
98+
turnAroundTime[i] =waitingTime[i] +burstTime[i];
99+
}
100+
101+
//return the turnaround time table
102+
returnturnAroundTime;
103+
}
104+
105+
106+
/**
107+
* This method prints the results and calculates the average waiting and turnaround times
108+
*
109+
* @param burstTime an array with burst time for all processes
110+
* @param quantum the quantum quantity
111+
*/
112+
voidprintAvgTimes(int[]burstTime,intquantum) {
113+
intn =burstTime.length;
114+
inttotalWaitingTime =0;
115+
inttotalTurnAroundTime =0;
116+
117+
// Find waiting time of all processes
118+
int[]waitingTime =calcWaitingTime(burstTime,quantum);
119+
120+
// Find turn around time for all processes
121+
int[]turnAroundTime =calcTurnAroundTime(burstTime,waitingTime);
122+
123+
// Display processes along with all details
124+
System.out.println("Process " +" Burst Time " +
125+
" Waiting Time " +" Turnaround Time");
126+
System.out.println("======= ========== ============ ===============");
127+
// Calculate total waiting time and total turn around time
128+
for (inti =0;i <n;i++) {
129+
totalWaitingTime +=waitingTime[i];
130+
totalTurnAroundTime +=turnAroundTime[i];
131+
System.out.println(i +"\t\t " +burstTime[i] +"\t\t\t " +
132+
waitingTime[i] +"\t\t\t " +turnAroundTime[i]);
133+
}
134+
135+
System.out.println("\nAverage waiting time = " +
136+
(float)totalWaitingTime / (float)n);
137+
System.out.println("Average turnaround time = " +
138+
(float)totalTurnAroundTime / (float)n);
139+
}
140+
}
141+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packagecom.string;
2+
3+
publicclassAlphabetical {
4+
5+
/**
6+
* Check if a string is alphabetical order or not
7+
*
8+
* @param s a string
9+
* @return {@code true} if given string is alphabetical order, otherwise {@code false}
10+
*/
11+
publicstaticbooleanisAlphabetical(Strings) {
12+
s =s.toLowerCase();
13+
for (inti =0;i <s.length() -1; ++i) {
14+
if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <=s.charAt(i +1))) {
15+
returnfalse;
16+
}
17+
}
18+
returntrue;
19+
}
20+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
packagecom.string;
2+
3+
importorg.junit.Test;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclassInterleave {
11+
@Test
12+
publicvoidtestInterleaveRegularString() {
13+
Stringstring1 ="Hello";
14+
Stringstring2 ="World";
15+
Stringexpected ="HWeolrllod";
16+
Stringactual =interleave(string1,string2);
17+
assertEquals("Incorrect result from method.",expected,actual);
18+
}
19+
20+
@Test
21+
publicvoidtestInterleaveEmptyString() {
22+
Stringstring1 ="";
23+
Stringstring2 ="";
24+
Stringstring3 ="a";
25+
Stringstring4 ="abc";
26+
Stringexpected1 ="";
27+
Stringactual1 =interleave(string1,string2);
28+
Stringexpected2 ="a";
29+
Stringactual2 =interleave(string1,string3);
30+
Stringexpected3 ="abc";
31+
Stringactual3 =interleave(string1,string4);
32+
assertEquals("Incorrect result from method.",expected1,actual1);
33+
assertEquals("Incorrect result from method.",expected2,actual2);
34+
assertEquals("Incorrect result from method.",expected3,actual3);
35+
}
36+
37+
@Test
38+
publicvoidtestInterleaveSingleString() {
39+
Stringstring1 ="a";
40+
Stringstring2 ="b";
41+
Stringexpected ="ab";
42+
Stringactual =interleave(string1,string2);
43+
assertEquals("Incorrect result from method.",expected,actual);
44+
}
45+
46+
@Test
47+
publicvoidtestInterleaveIntString() {
48+
Stringstring1 ="1";
49+
Stringstring2 ="7";
50+
Stringexpected ="17";
51+
Stringactual =interleave(string1,string2);
52+
assertEquals("Incorrect result from method.",expected,actual);
53+
}
54+
55+
@Test
56+
publicvoidtestInterleaveMixedString() {
57+
Stringstring1 ="1a2b3c4d";
58+
Stringstring2 ="5e6f7g8h";
59+
Stringexpected ="15ae26bf37cg48dh";
60+
Stringactual =interleave(string1,string2);
61+
assertEquals("Incorrect result from method.",expected,actual);
62+
}
63+
64+
@Test
65+
publicvoidtestInterleaveSymbols() {
66+
Stringstring1 ="a@b%c/";
67+
Stringstring2 ="d#e$g%.";
68+
Stringexpected ="ad@#be%$cg/%.";
69+
Stringactual =interleave(string1,string2);
70+
assertEquals("Incorrect result from method.",expected,actual);
71+
}
72+
73+
@Test
74+
publicvoidtestInterleaveSpaces() {// This string interleave algorithm defines a space as a valid character.
75+
Stringstring1 =" ";
76+
Stringstring2 ="a";
77+
Stringstring3 ="5 g";
78+
Stringstring4 =" 4 d ";
79+
Stringexpected1 =" a";
80+
Stringactual1 =interleave(string1,string2);
81+
Stringexpected2 ="a5 g";
82+
Stringactual2 =interleave(string2,string3);
83+
Stringexpected3 ="5 4g d ";
84+
Stringactual3 =interleave(string3,string4);
85+
assertEquals("Incorrect result from method.",expected1,actual1);
86+
assertEquals("Incorrect result from method.",expected2,actual2);
87+
assertEquals("Incorrect result from method.",expected3,actual3);
88+
}
89+
90+
/**
91+
* This method "interweaves" two input strings one character at a time. The first character of the
92+
* first parameter string always starts the resulting string, unless that character is a space or is empty.
93+
* This string interleaving method takes a space in a string (e.g. " ") into consideration.
94+
*
95+
* For example, if string1 = "abc" and string2 = "def", then the result would be "adbecf", as the first character
96+
* of the string1 is 'a', then the first character of string2 is 'd', and so forth.
97+
*
98+
* For more information on interleaving, check out: https://en.wikipedia.org/wiki/Interleave_sequence
99+
*
100+
* @param string1
101+
* @param string2
102+
* @return string resulting from the interweaving of the two input strings; string1 and string2.
103+
*/
104+
publicStringinterleave(Stringstring1,Stringstring2) {
105+
Stringresult ="";// The final interleaved string to return.
106+
List<Character>list1 =newArrayList<>();// The ArrayList of string1, with each character being an individual element.
107+
List<Character>list2 =newArrayList<>();// The ArrayList of string2, in a similar manner as above.
108+
109+
for (inti =0;i <string1.length();i++)// Convert string1 into list1.
110+
list1.add(string1.charAt(i));
111+
112+
for (inti =0;i <string2.length();i++)// Convert string2 into list2.
113+
list2.add(string2.charAt(i));
114+
115+
if (string1.length() ==string2.length()) {// Interleaving when string1 and string2 are equal length.
116+
for (intj =0;j <list1.size();j++) {
117+
result =result +list1.get(j);
118+
result =result +list2.get(j);
119+
}
120+
returnresult;
121+
}
122+
123+
if (string1.length() >string2.length()) {// Interleaving when string1 is longer than string2.
124+
while (list2.size() >0) {
125+
result =result +list1.get(0);
126+
list1.remove(0);
127+
result =result +list2.get(0);
128+
list2.remove(0);
129+
}
130+
for (charcharacter :list1) {// Concatenate the rest of the characters in list1 to the result.
131+
result =result +character;
132+
}
133+
returnresult;
134+
}
135+
136+
if (string2.length() >string1.length()) {// Interleaving when string2 is longer than string1.
137+
while (list1.size() >0) {
138+
result =result +list1.get(0);
139+
list1.remove(0);
140+
result =result +list2.get(0);
141+
list2.remove(0);
142+
}
143+
for (charcharacter :list2) {// Concatenate the rest of the characters in list2 to the result.
144+
result =result +character;
145+
}
146+
returnresult;
147+
}
148+
149+
returnresult;
150+
}
151+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagecom.string;
2+
3+
publicclassReverseWords {
4+
/**
5+
* Converts all of the words in this {@code String} to reversed words
6+
*
7+
* @param s the string to convert
8+
* @return the {@code String}, converted to a string with reveresed words.
9+
*/
10+
11+
publicstaticStringreturnReverseWords(Strings) {
12+
StringBuildersb =newStringBuilder();
13+
StringBuilderword =newStringBuilder();
14+
15+
for(inti =0;i <s.length();i++) {
16+
charc =s.charAt(i);
17+
if(c ==' ') {
18+
19+
sb.append(word);
20+
sb.append(" ");
21+
word.setLength(0);
22+
continue;
23+
}
24+
word.insert(0,c);
25+
}
26+
sb.append(word);
27+
28+
returnsb.toString();
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp