Programming Challenge Description: You will be given a sequence of passages, and
ID: 3844770 • Letter: P
Question
Programming Challenge Description:
You will be given a sequence of passages, and must filter out any passage whose text (sequence of whitespace-delimited words) is wholly contained as a sub-passage of one or more of the other passages.
When comparing for containment, certain rules must be followed:
The case of alphabetic characters should be ignored
Leading and trailing whitespace should be ignored
Any other block of contiguous whitespace should be treated as a single space
non-alphanumeric character should be ignored, white space should be retained
Duplicates must also be filtered - if two passages are considered equal with respect to the comparison rules listed above, only the shortest should be retained. If they are also the same length, the earlier one in the input sequence should be kept. The retained passages should be output in their original form (identical to the input passage), and in the same order.
Input:
For each test case a single line comprising the passages (strings) to be processed, delimited by | characters. The | characters are not considered part of any passage.
Output:
A single line of filtered passages in the same |-delimited format.
Test 1
Test Input
IBM cognitive computing|IBM "cognitive" computing is a revolution| ibm cognitive computing|'IBM Cognitive Computing' is a revolution?
Expected Output
IBM "cognitive" computing is a revolution
Test 2
Test Input
"Computer Science Department"|Computer-Science-Department|the "computer science department"
Expected Output
Computer-Science-Department|the "computer science department"
Code to be completed in JAVA:
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
}
}
Explanation / Answer
import java.util.*; public class Main{ public static String test1 = "IBM cognitive computing|IBM "cognitive" computing is a revolution|ibm cognitive computing|'IBM Cognitive Computing' is a revolution?"; public static String test2 = "IBM cognitive computing|IBM "cognitive" computing 'is' a revolution|the cognitive computing is a revolution"; public static ArrayList unique = new ArrayList(); public static ArrayList uniqueR = new ArrayList(); public static void main(String [] args){ String [] temp = test1.split("\|"); //.replaceAll("[\-\+\.\^:,"']","") System.out.println(Arrays.toString(temp)); for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.