Skip to content

Commit b4f081f

Browse files
committed
Day 8
1 parent 35b8738 commit b4f081f

14 files changed

+256
-0
lines changed

ch-21/src/CountKeywords.java

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package PACKAGE_NAME;public class CountKeywords {
2+
}

ch-21/src/DisplayNonDuplicate.java

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public class DisplayNonDuplicate {
2+
}

ch-21/src/GuessCapitalsGame.java

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package PACKAGE_NAME;public class GuessCapitalsGame {
2+
}

ch-21/src/NumberOfOccurrences.java

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package PACKAGE_NAME;public class NumberOfOccurrences {
2+
}

ch-21/src/file.txt

Whitespace-only changes.

ch-22/.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

ch-22/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-22/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-22/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-22/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-22/ch-22.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

ch-22/src/ConsecutiveSubstring.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*
5+
* *22.1 (Maximum consecutive increasingly ordered substring)
6+
7+
* Write a program that
8+
* prompts the user to enter a string and displays the maximum consecutive
9+
* increasingly ordered substring. Analyze the time complexity of your program.
10+
*
11+
*/
12+
13+
// Code below has a time complexity of O(n^2)
14+
15+
public class ConsecutiveSubstring {
16+
public static void main(String[] args) {
17+
Scanner scanner = new Scanner(System.in);
18+
System.out.println("Enter a sequence of strings: ");
19+
String sequence = scanner.nextLine();
20+
21+
String longestSequence = "";
22+
String sortedSequence = "";
23+
int start = 0;
24+
for(int i = 0; i < sequence.length() + 1; i++) {
25+
sortedSequence = sequence.substring(start, i);
26+
if(!isAlphabetical(sortedSequence)){
27+
start = i - 1;
28+
if(sortedSequence.length() > longestSequence.length()) {
29+
longestSequence = sortedSequence.substring(0, sortedSequence.length() - 1);
30+
continue;
31+
}
32+
33+
sortedSequence = "";
34+
}
35+
36+
if(sortedSequence.length() > longestSequence.length()) {
37+
longestSequence = sortedSequence;
38+
}
39+
}
40+
41+
System.out.println("Longest Sequence in string is: " + longestSequence);
42+
}
43+
44+
public static boolean isAlphabetical(String s) {
45+
char[] charArray = s.toCharArray();
46+
47+
for(int i = 1; i < charArray.length; i++) {
48+
char current = charArray[i];
49+
char previous = charArray[i - 1];
50+
if(current < previous) {
51+
return false;
52+
}
53+
}
54+
55+
return true;
56+
}
57+
}
58+

ch-22/src/PatternMatching.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*
5+
* *22.3 (Pattern matching)
6+
7+
* Write a program that prompts the user to enter two strings
8+
* and tests whether the second string is a substring of the first string. Suppose
9+
* the neighboring characters in the string are distinct. (Don’t use the indexOf
10+
* method in the String class.) Analyze the time complexity of your algorithm.
11+
* Your algorithm needs to be at least O(n) time.
12+
*
13+
*/
14+
public class PatternMatching {
15+
public static void main(String[] args) {
16+
Scanner scanner = new Scanner(System.in);
17+
System.out.println("Enter string number one: ");
18+
String s1 = scanner.nextLine();
19+
System.out.println("Enter string number two");
20+
String s2 = scanner.nextLine();
21+
22+
String currentPattern = "";
23+
int startIndex = -1;
24+
for(int i = 0; i < s1.length(); i++) {
25+
if(currentPattern.equals(s2)) {
26+
break;
27+
}
28+
char s1Char = s1.charAt(i);
29+
if(s1Char == s2.charAt(currentPattern.length())) {
30+
if(startIndex == -1)
31+
startIndex = i;
32+
currentPattern += s1Char;
33+
continue;
34+
}
35+
currentPattern = "";
36+
startIndex = -1;
37+
}
38+
39+
System.out.println("Pattern starts at index: " + startIndex);
40+
}
41+
}

ch-22/src/PrimeNumbers.java

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.io.*;
2+
import java.math.BigInteger;
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
/**
8+
*
9+
* **22.8 (All prime numbers up to 10,000,000,000)
10+
11+
* Write a program that finds
12+
* all prime numbers up to 10,000,000,000. There are approximately
13+
* 455,052,511 such prime numbers. Your program should meet the following
14+
* requirements:
15+
16+
* ■ Your program should store the prime numbers in a binary data file, named
17+
* PrimeNumbers.dat. When a new prime number is found, the number is
18+
* appended to the file.
19+
20+
* ■ To find whether a new number is prime, your program should load the
21+
* prime numbers from the file to an array of the long type of size 10000.
22+
* If no number in the array is a divisor for the new number, continue to read
23+
* the next 10000 prime numbers from the data file, until a divisor is found
24+
* or all numbers in the file are read. If no divisor is found, the new number
25+
* is prime.
26+
27+
* ■ Since this program takes a long time to finish, you should run it as a batch
28+
* job from a UNIX machine. If the machine is shut down and rebooted, your
29+
* program should resume by using the prime numbers stored in the binary data
30+
* file rather than start over from scratch.
31+
*
32+
*/
33+
public class PrimeNumbers {
34+
public static void main(String[] args) {
35+
long startNumber = 2;
36+
long maxNumber = 10_000_000_000L;
37+
List<Long> primes = loadStoredPrimes();
38+
39+
for (long num = startNumber; num <= maxNumber; num++) {
40+
if (isPrime(num, primes)) {
41+
storePrime(num);
42+
primes.add(num);
43+
}
44+
}
45+
}
46+
47+
public static List<Long> loadStoredPrimes() {
48+
List<Long> primes = new ArrayList<>();
49+
50+
try(
51+
Scanner scanner = new Scanner(new File("PrimeNumbers.dat"))
52+
) {
53+
while (scanner.hasNextLong()) {
54+
primes.add(scanner.nextLong());
55+
}
56+
} catch (FileNotFoundException ex) {
57+
System.out.println("File was not found");
58+
}
59+
return primes;
60+
}
61+
62+
public static boolean isPrime(long num, List<Long> primes) {
63+
for (long prime : primes) {
64+
if (prime * prime > num) {
65+
break;
66+
}
67+
if (num % prime == 0) {
68+
return false;
69+
}
70+
}
71+
return true;
72+
}
73+
74+
public static void storePrime(long prime) {
75+
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("PrimeNumbers.dat", true))) {
76+
dos.writeLong(prime);
77+
} catch (IOException ex) {
78+
System.out.println("Error writing to file: " + ex.getMessage());
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)