HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Fragments of paper

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fragmentspaperstackoverflow

Problem

The exercise is explained in the comments. Do you have any suggestion?

```
package com.atreceno.it.javanese.attic;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* Imagine you have several copies of the same page of text. Someone rips each
* page up into fragments. Write a program to reassemble a given set of text
* fragments into their original sequence. The program should have a main method
* accepting the path to a text file containing text fragments separated by a
* semicolon. Each line in the file is a different test case.
*
* @author atreceno
*
*/
public class Defragmenter {

public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(reassemble(line));
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Assemble the fragments in the correct order.
*
* @param line
* String containing the fragments of the document.
* @return the line in the correct order.
*/
private static String reassemble(String line) {

// Each line contains text fragments separated by a semicolon.
String[] fragments = line.split(";");
List list = new ArrayList(Arrays.asList(fragments));

// Sort the fragments by size
Collections.sort(list, new Comparator() {
@Override
public int compare(String o1, String o2) {
return o2.length() - o1.length();
}
});

// Pick up the first fragment
String text = list.get(0);
list.remove(0);

// Start the algorithm

Solution

Your code is really good and very well written. I think that's why you haven't received a response; not much to critique! You have good comments, good style, pretty much good everything. I wish I saw code like this in my production environment.

The only thing I noticed that you might clean your code up a bit is this part:

int max = 0;
int idx = 0;
int match = 0;
int m = 0;
int n = 0;


You can shorten this long block of variable declarations as the following. Makes the code look a bit cleaner.

int max = 0, idx = 0, match = 0, m = 0, n = 0;


Also, if possible, could you give the variables better names? The first three are fairly self-explanatory, but once you get into all your random letters, it gets pretty hard to follow what you're trying to do in your code. For example:

int l = k + n <= m ? n : m - k;


Without having written the code, this statement takes awhile to figure out. It would go a long way to have meaningful variable names rather than random letters. Java is supposed to be verbose, so don't be afraid to make it so.

I won't say I understand what your code is doing with 100% certainty, but you can see how something like this is a bit more understandable, hopefully:

int length = lookBack + findMeLength <= trueLength ? findMeLength : trueLength - lookBack


But seriously, very high quality code!

Code Snippets

int max = 0;
int idx = 0;
int match = 0;
int m = 0;
int n = 0;
int max = 0, idx = 0, match = 0, m = 0, n = 0;
int l = k + n <= m ? n : m - k;
int length = lookBack + findMeLength <= trueLength ? findMeLength : trueLength - lookBack

Context

StackExchange Code Review Q#39143, answer score: 3

Revisions (0)

No revisions yet.