1. Read the first 50 lines of input and then write them out in reverse order. Read the next 50 lines and then write them out in reverse order. Do this until there are no more lines left to read.
In other words, your output will start with the 50th line, then the 49th, then the 48th, and so on down to the first line. This will be followed by the 100th line, followed by the 99th, and so on down to the 51st line. And so on.
Your code should never have to store more than 50 lines at any given time.
2. Read the whole input one line at a time. Then output all lines sorted by length, with the shortest lines first. In the case where two lines have the same length, resolve their order using the usual sorted order.
3. Read the entire input one line at a time and then output the even numbered lines (starting with the first line, line 0) followed by the odd-numbered lines.
4. Read the input one line at a time and write each line to the output only if you have already read this line before. (The end result is that you remove the first occurrence of each line.)
Take special care so that a file with a lot of duplicate lines does not use more memory than what is required for the number of unique lines.
5. Read the input one line at a time. At any point after reading the first 42 lines, if some line is blank (i.e., a string of length 0) then output the line that occured 42 lines prior to that one. For example, if Line 242 is blank, then your program should output line 200. This program should be implemented so that it never stores more than 43 lines of the input at any given time.
6. Write the generic function Part6.mongeanShuffle(List l) that simulates a perfect Mongean shuffle on the list l. More precisely, given a list that contains the number 0,1,2,3,...,n-1. The result should be a list containing
n-1, n-3, n-5,...,0,1,3,5,...,n-2 (if n is odd) or
n-2, n-4, n-6,...,0,1,3,5,...,n-1 (if n is even)
Write your code so that it works efficiently (in O(n) time) even if l is a LinkedList
Note that this operation is destructive. It does not create a new list. Instead, it modifies the order of the elements in the list l.
EDIT: I definitely need to find a better way of posting code since this way messes up all the indentation. I might just put the files up on mediafire later.
In other words, your output will start with the 50th line, then the 49th, then the 48th, and so on down to the first line. This will be followed by the 100th line, followed by the 99th, and so on down to the 51st line. And so on.
Your code should never have to store more than 50 lines at any given time.
- Part1.java:
- package comp2402a1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Part1 {
/**
* Your code goes here - see Part0 for an example
* @param r
* @param w
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
//Create and initialize variables
String[] s = new String[50];
String line;
int n = 0;
Boolean stop = false;
while (stop == false) { //Reading and Writing Loop
while(n < 50){ //Reading Loop. Assigns first fifty strings from BufferedReader to Array s
if ((line = r.readLine()) != null) {
s[n]=line;
n++;
}
else{
stop = true;
break; //exits Reading Loop, so n isn't incremented anymore
}
}
for (int i = n-1 ; i >= 0; i--){ //Writing Loop. Writes Strings that have been assigned to s and is within range of (0 to (n-1))
if (s[i] != null) {
w.println(s[i]);
}
}
n = 0; //resets n to 0 allowing the Reading Loop to work next time
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 10e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
2. Read the whole input one line at a time. Then output all lines sorted by length, with the shortest lines first. In the case where two lines have the same length, resolve their order using the usual sorted order.
- Part2.java:
- package comp2402a1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Part2 {
/**
* Your code goes here - see Part0 for an example
* @param r
* @param w
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
ArrayListunorderedList = new ArrayList ();
ArrayListorderedList = new ArrayList ();
String line;
Boolean wordAdded = false;
while ((line = r.readLine()) != null){
unorderedList.add(line);
}
orderedList.add(unorderedList.get(0));
unorderedList.remove(0);
for (String us : unorderedList){
for (int i = 0; i < orderedList.size(); i++){
if (us.length() <= orderedList.get(i).length()){
orderedList.add(i, us);
wordAdded=true;
break;
}
}
if (!wordAdded){
orderedList.add(us);
}
wordAdded=false;
}
for (String os : orderedList){
w.println(os);
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 1.0e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
3. Read the entire input one line at a time and then output the even numbered lines (starting with the first line, line 0) followed by the odd-numbered lines.
- Part3.java:
- package comp2402a1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Part3 {
/**
* Your code goes here - see Part0 for an example
* @param r
* @param w
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
ArrayListoddList = new ArrayList ();
ArrayListevenList = new ArrayList ();
String line;
while ((line = r.readLine()) != null) {
evenList.add(line);
if ((line = r.readLine()) != null){
oddList.add(line);
}
}
for(String s : evenList){
w.println(s);
}
for(String s : oddList){
w.println(s);
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 10e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
4. Read the input one line at a time and write each line to the output only if you have already read this line before. (The end result is that you remove the first occurrence of each line.)
Take special care so that a file with a lot of duplicate lines does not use more memory than what is required for the number of unique lines.
- part4.java:
- package comp2402a1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Part4 {
/**
* Your code goes here - see Part0 for an example
* @param r
* @param w
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
ArrayListfirstOccList = new ArrayList ();
String line;
while ((line = r.readLine()) != null){
if (!(firstOccList.contains(line))){
firstOccList.add(line);
}
else if(firstOccList.contains(line)) {
w.println(line);
}
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 1.0e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
5. Read the input one line at a time. At any point after reading the first 42 lines, if some line is blank (i.e., a string of length 0) then output the line that occured 42 lines prior to that one. For example, if Line 242 is blank, then your program should output line 200. This program should be implemented so that it never stores more than 43 lines of the input at any given time.
- Part5.java:
- package comp2402a1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
public class Part5 {
/**
* Your code goes here - see Part0 for an example
* @param r
* @param w
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
LinkedListl = new LinkedList ();
String line;
int cnt = 0;
while ((line = r.readLine()) != null){
if (cnt <= 41){
l.add(line);
cnt++;
}
else {
l.add(line);
if (line.length() == 0){
w.println(l.get(0));
}
l.remove(0);
}
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 1.0e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
6. Write the generic function Part6.mongeanShuffle(List l) that simulates a perfect Mongean shuffle on the list l. More precisely, given a list that contains the number 0,1,2,3,...,n-1. The result should be a list containing
n-1, n-3, n-5,...,0,1,3,5,...,n-2 (if n is odd) or
n-2, n-4, n-6,...,0,1,3,5,...,n-1 (if n is even)
Write your code so that it works efficiently (in O(n) time) even if l is a LinkedList
Note that this operation is destructive. It does not create a new list. Instead, it modifies the order of the elements in the list l.
- Part6.java:
- package comp2402a1;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public abstract class Part6 {
public staticvoid mongeanShuffle(List l) {
T line;
int cnt = 0;
ListIteratorli = l.listIterator();
ArrayListtempListEven = new ArrayList ();
ArrayListtempListOdd = new ArrayList ();
for (int i=0; iif ((i % 2 == 0) && (li.hasNext())) {
line = li.next();
tempListEven.add(line);
}
if ((i % 2 == 1) && (li.hasNext())) {
line = li.next();
tempListOdd.add(line);
}
}
for (int i = tempListEven.size() - 1; i >= 0; i--) {
l.set(cnt, tempListEven.get(i));
cnt++;
}
for (int i = 0; i < tempListOdd.size(); i++) {
l.set(cnt, tempListOdd.get(i));
cnt++;
}
}
/**
* Some example testing code
* @param args
*/
public static void main(String[] args) {
testIt(new ArrayList());
testIt(new LinkedList());
}
public static void testIt(Listl) {
String[] a = {"sheldon", "penny", "leonard", "howard", "raj", "leslie", "wil"};
for (String x : a) l.add(x);
System.out.println(l);
mongeanShuffle(l);
System.out.println(l);
}
}
EDIT: I definitely need to find a better way of posting code since this way messes up all the indentation. I might just put the files up on mediafire later.


