Created
April 8, 2015 18:28
-
-
Save logicx24/4d82fc49a3ee087e4026 to your computer and use it in GitHub Desktop.
Natero Code Questions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| class BlockingQueue { | |
| private ArrayList<Character> queue; | |
| public BlockingQueue() { | |
| queue = new ArrayList<Character>(); | |
| } | |
| public synchronized String take() { | |
| if (queue.size() == 0) { | |
| return "Empty queue"; | |
| } | |
| String toReturn = queue.get(queue.size() - 1).toString(); | |
| queue.remove(queue.size() - 1); | |
| return toReturn; | |
| } | |
| public synchronized void put(String string) { | |
| char[] tempArr = string.toCharArray(); | |
| if ((queue.size() + tempArr.length) > 100) { | |
| return; | |
| } | |
| Character[] charObjectArray = new Character[tempArr.length]; | |
| for (int i = 0; i < tempArr.length; i++) { | |
| charObjectArray[i] = new Character(tempArr[i]); | |
| } | |
| for (Character character : charObjectArray) { | |
| queue.add(character); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| BlockingQueue queue = new BlockingQueue(); | |
| queue.put("wimbledon"); | |
| queue.put("fgfsgfgadkfodjfijijijikafhauhuvanrna;k"); | |
| System.out.println(queue.take()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.File; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| import java.io.BufferedWriter; | |
| import java.io.OutputStreamWriter; | |
| import java.io.FileOutputStream; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| class Reduce { | |
| public static void reduceByKey(String input1, String input2) { | |
| ArrayList<String> inputFiles = new ArrayList<String>(); | |
| inputFiles.add(input1); | |
| inputFiles.add(input2); | |
| try { | |
| ArrayList<BufferedReader> fileReads = new ArrayList<BufferedReader>(); | |
| for (String file : inputFiles) { | |
| fileReads.add(new BufferedReader(new FileReader(new File(file)))); //I'm not sure how to limit this to 100 lines, because there are a varying number of bytes/line. | |
| } | |
| HashMap<String, ArrayList> fileHash = new HashMap<String, ArrayList>(); | |
| for (BufferedReader file : fileReads) { | |
| String line; | |
| while((line = file.readLine()) != null) { | |
| String[] keyVal = line.split(",", -1); | |
| ArrayList<String> temp = fileHash.get(keyVal[0]); | |
| if (temp == null) { | |
| temp = new ArrayList<String>(); | |
| } | |
| temp.add(keyVal[1]); | |
| fileHash.put(keyVal[0], temp); | |
| } | |
| } | |
| for (BufferedReader file : fileReads) { | |
| file.close(); | |
| } | |
| BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.txt"), "utf-8")); | |
| for (ArrayList<String> value : fileHash.values()) { | |
| for (String elem : value) { | |
| output.write(elem + ","); | |
| } | |
| output.newLine(); | |
| } | |
| output.close(); | |
| } catch (IOException ex) { | |
| ex.printStackTrace(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| Reduce.reduceByKey("txt1.txt", "txt2.txt"); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.File; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| import java.io.BufferedWriter; | |
| import java.io.OutputStreamWriter; | |
| import java.io.FileOutputStream; | |
| class SortedLists { | |
| public static void mergeFiles(String outputFile, String inputFile1, String inputFile2, String inputFile3) { | |
| String[] inputFilenames = new String[3]; | |
| inputFilenames[0] = inputFile1; | |
| inputFilenames[1] = inputFile2; | |
| inputFilenames[2] = inputFile3; | |
| String outputFilename = outputFile; | |
| try { | |
| BufferedReader[] inputs = new BufferedReader[3]; | |
| int i = 0; | |
| for (String inputFile : inputFilenames) { | |
| BufferedReader buffread = new BufferedReader(new FileReader(new File(inputFile))); | |
| inputs[i] = buffread; | |
| i++; | |
| } | |
| BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFilename), "utf-8")); | |
| boolean moreIterations = true; | |
| boolean advanceInput1 = true; | |
| boolean advanceInput2 = true; | |
| boolean advanceInput3 = true; | |
| String currInput1 = ""; | |
| String currInput2 = ""; | |
| String currInput3 = ""; | |
| while (moreIterations) { | |
| if (advanceInput1) { | |
| currInput1 = inputs[0].readLine(); | |
| } | |
| if (advanceInput2) { | |
| currInput2 = inputs[1].readLine(); | |
| } | |
| if (advanceInput3) { | |
| currInput3 = inputs[2].readLine(); | |
| } | |
| if (currInput1 != null && (currInput2 == null || currInput1.compareTo(currInput2) <= 0) && (currInput3 == null || currInput1.compareTo(currInput3) <= 0)) { | |
| output.write(currInput1); | |
| output.newLine(); | |
| advanceInput1 = true; | |
| advanceInput2 = false; | |
| advanceInput3 = false; | |
| } else if (currInput2 != null && (currInput1 == null || currInput2.compareTo(currInput1) <= 0) && (currInput3 == null || currInput2.compareTo(currInput3) <= 0)) { | |
| output.write(currInput2); | |
| output.newLine(); | |
| advanceInput1 = false; | |
| advanceInput2 = true; | |
| advanceInput3 = false; | |
| } else if (currInput3 != null && (currInput2 == null || currInput3.compareTo(currInput2) <= 0) && (currInput1 == null || currInput3.compareTo(currInput1) <= 0)) { | |
| output.write(currInput3); | |
| output.newLine(); | |
| advanceInput1 = false; | |
| advanceInput2 = false; | |
| advanceInput3 = true; | |
| } | |
| if (currInput1 == null && currInput2 == null && currInput3 == null) { | |
| moreIterations = false; | |
| } | |
| } | |
| output.close(); | |
| for (BufferedReader buff : inputs) { | |
| buff.close(); | |
| } | |
| } catch (IOException ex) { | |
| ex.printStackTrace(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| SortedLists.mergeFiles("output.txt", "txt1.txt", "txt2.txt", "txt3.txt"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment