Skip to content

Instantly share code, notes, and snippets.

@meiwin
Created April 3, 2012 13:54
Show Gist options
  • Save meiwin/2292190 to your computer and use it in GitHub Desktop.
Save meiwin/2292190 to your computer and use it in GitHub Desktop.
mig33 Algorithm (1)
// Write java program that:
// takes its input from two files and prints out lines that exists in both files
package mig33;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
public class Problem1 {
private static Set<Integer> lineHashes(String filePath) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(filePath))));
Set<Integer> hashes = new HashSet<Integer>();
String line = null;
while ((line = r.readLine()) != null) {
hashes.add(line.hashCode());
}
r.close();
return hashes;
}
public static void main(String[] args) throws IOException {
// extract hashes from file1
Set<Integer> hashes1 = lineHashes(args[0]);
BufferedReader r2 = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(args[1]))));
String line = null;
Set<Integer> printedHashes = new HashSet<Integer>();
while ((line = r2.readLine()) != null) {
Integer hashCode = line.hashCode();
if (hashes1.contains(hashCode) && !printedHashes.contains(hashCode)) {
printedHashes.add(hashCode);
System.out.println(line);
}
}
r2.close();
}
}
// takes its input from two files but prints out lines that are unique instead
package mig33;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Problem2 {
private static Map<Integer,String> extractLines(String filePath) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
Map<Integer,String> lines = new HashMap<Integer, String>();
String line = null;
while ((line = r.readLine()) != null) {
lines.put(line.hashCode(), line);
}
r.close();
return lines;
}
private static void printUniques(Map<Integer,String> map1, Map<Integer,String> map2) {
for (Integer hashCode : map1.keySet()) {
if (!map2.containsKey(hashCode)) {
System.out.println(map1.get(hashCode));
}
}
}
public static void main(String[] args) throws IOException {
// read file 1 and store it hashes and value
Map<Integer,String> lines1 = extractLines(args[0]);
// read file 2 and store it hashes and value
Map<Integer,String> lines2 = extractLines(args[1]);
// print uniques from lines1
printUniques(lines1,lines2);
// print uniques from lines2
printUniques(lines2,lines1);
}
}
// takes its input from two files but prints only lines that exist in the first file but not in the second
package mig33;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
public class Problem3 {
private static Set<Integer> lineHashes(String filePath) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
Set<Integer> hashes = new HashSet<Integer>();
String line = null;
while ((line = r.readLine()) != null) {
hashes.add(line.hashCode());
}
r.close();
return hashes;
}
public static void main(String[] args) throws IOException {
// extract hashes from file 2
Set<Integer> hashes2 = lineHashes(args[1]);
// print uniques from lines1
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
String line = null;
Set<Integer> printedHashes = new HashSet<Integer>();
while ((line = r.readLine()) != null) {
Integer hashCode = line.hashCode();
if (!hashes2.contains(hashCode) &&
!printedHashes.contains(hashCode)) {
System.out.println(line);
printedHashes.add(hashCode);
}
}
r.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment