Sets
Set Overview
HashSet vs TreeSet
Union and Intersection of Java Sets (or Lists)
Project #1: Null Cipher
This problem is taken from Problem #1 in the 2022 Lockheed Martin Coding Contest.
The null cipher is effective because it appears to be a perfectly harmless message. The secret
message - known as a ciphertext - is hidden within another message by adding in a large number of "null" values, either words or letters, which have nothing to do with the original message. Ideally, an eavesdropper would see the message and not realize there was a second message hidden inside, but the intended recipient would know to remove certain words or characters to restore the original message.
Lockheed Martin is working with the National Security Agency to test a slightly different form of null cipher. The NSA intends to embed a message within a string of random characters. Their hope is that an eavesdropper will suspect a hidden message, but will assume that the random nature of the message means that it is encrypted using a cipher, and will waste time attempting to break it. In reality, the message will simply be scattered throughout the text string. Any character that is part of the actual message will immediately follow an English vowel; that is, one of the letters a, e, i, o, or u. When those characters occur in the actual message, they will follow a different vowel; the character after them is not part of the message. For example, the string below can be read as "hello world":
fksahnlgueyilfhnalfkjnhdssaokjfhndsfiwaourhnfdjgbalfkjshedfnsf
Given some sample strings generated by the NSA, design a program that can extract the original
messages.
Sample Input for Null Cipher Problem
Here are three test cases:
fksahnlgueyilfhnalfkjnhdssaokjfhndsfiwaourhnfdjgbalfkjshedfnsf
mkjmnacioudhrieeqwthyiugueresjfgwatfhwghfnhgnffn
elruoqywicwnjksakvfbsgyohuehnghiefhggadfgsfsfs
Sample Output for Null Cipher Problem
For each test case, your program must print a single line containing the plaintext message extracted
from the input string. For the three test cases, above, here is what your program should print.
helloworld
codequest
lockheed
Starting Code for Null Cipher Problem
public class P01NullCipher {
public P01NullCipher() {
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
String line = scanner.nextLine();
cipher(line);
}
}
public static void cipher(String line) {
/* you have to write this part. put the vowels into a Set. then loop through the String and see if each character is a vowel or not. if it is not a vowel ignore it. if it is a vowel, put the next character in the solution string. after the entire string is processed, print the answer string */
}
}
Project #2: Set Practice Using LeetCode
Solve the First-Missing-Positive problem on LeetCode using a HashSet.
Solve the Valid-Parenthesis problem on LeetCode using a HashSet and a Stack.
Project #3 on Sets: Manipulating Coordinates
Create a Set to store the following coordinate pairs using the Coordinate class shown below.
(3, 4), (2, 3), (7, 9), (2, 11), (9, 9), (5, 7), (1, 0), (0, 0), (0, 9)
Then use an iterator to loop through these objects and delete the ones that have odd numbers for both their X and Y ordinates. Be sure to print the set using the built-in toString() method before and after deletion.
When storing the Coordinate objects in the set, do not create individual variables for each Coordinate object. Instead, add them as anonymous objects like this:
set.add( new Coordinate(3,4) );
Project #4 Taxicab Distances and The Last Place You Look
Do the problem called The Last Place You Look from the 2021 Lockheed Martin Coding Competition. First, take a look at the video, below. To do this problem, you will also need the Coordinate class shown below. Note that this project is significantly more challenging than the other projects in this Unit.
A Coordinate Class for Us to Use
import java.util.Objects;
public class Coordinate {
// use default access modifier
final int X;
final int Y;
// toString
@Override
public String toString() {
return "( " + this.X + "," + this.Y + " )";
}
// constructor
public Coordinate(int X, int Y) {
this.X = X;
this.Y = Y;
}
// copy constructor
public Coordinate(Coordinate other) {
this.X = other.X;
this.Y = other.Y;
}
// standard equals method generated by IntelliJ
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Coordinate that = (Coordinate) o;
return X == that.X && Y == that.Y;
}
@Override
public int hashCode() {
// simple hash function
int has = 23;
hash = hash * 31 + this.X;
hash = hash * 31 + this.Y;
return hash;
}
}