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.
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.
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);
}
}
}
- 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);
}
}
}
- 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);
}
}
}
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);
}
}
}
- 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);
}
}
}
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.