Skip to content

Instantly share code, notes, and snippets.

@rchasman
Created August 31, 2012 23:54
Show Gist options
  • Save rchasman/3561371 to your computer and use it in GitHub Desktop.
Save rchasman/3561371 to your computer and use it in GitHub Desktop.
This was the first assignment given to us by McCann, it parses passed arguments to be unique, and ordered from largest to smallest.
/*=============================================================================
| Assignment: Program #1: Short to Long Uniques Argument List
| Author: Roey Chasman (rchasman@email.arizona.edu)
| Grader: Sam Martin
|
| Course: 352
| Instructor: L. McCann
| Due Date: 8/30/12 5:00pm
|
| Description: A program that accepts a variety of command-line arguments,
| removing duplicates, and then outputs the reduced argument
| list on a single line, with the arguments in descending order
| by length, when length is the same, honor the entered order.
|
| Deficiencies: No deficencies are known to this program.
*===========================================================================*/
import java.util.*; // For access to data structures and methods.
public class Prog1c {
public static void main (String[] args) {
// Were any passed arguments to this program?
if (args.length > 0) {
// Convert the array of passed arguments to a LinkedList
LinkedList<String> list = new LinkedList<String>(Arrays.asList(args));
// Call methods functionally to produce desired output and print it.
System.out.print(stringify(sort(deduplicate(requote(list)))) + '\n');
}
}
/*---------------------------------------------------------------------
| Method requote(LinkedList<String> input)
|
| Purpose: The requote method looks for Strings in the passed
| LinkedList<String> that contain spaces and wraps them with
| quotation marks.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Parameters:
| LinkedList<String> input --
| The input parameter should contain a LinkedList of Strings to
| check if they need to be wrapped in quotes.
|
| Returns: Returns a LinkedList<String> of the processed contents of
| input.
*-------------------------------------------------------------------*/
public static LinkedList<String> requote (LinkedList<String> input) {
// Declare an empty output LinkedList to be pushed to.
LinkedList<String> output = new LinkedList<String>();
// For every String in the passed LinkedList
for(String s : input) {
// Check if it contains any spaces
if (s.contains(" "))
// If so then wrap the value in quotes and push it to output
output.add("\"" + s + "\"");
else
// If not then just push it unchanged to the output
output.add(s);
}
return output;
}
/*---------------------------------------------------------------------
| Method deduplicate(LinkedList<String> input)
|
| Purpose: The deduplicate method strips out the duplicate values of a
| passed LinkedList.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Parameters:
| LinkedList<String> input --
| The input parameter should contain a LinkedList of Strings to
| be deduplicated.
|
| Returns: Returns a LinkedList<String> of the deduplicated contents of
| input.
*-------------------------------------------------------------------*/
public static LinkedList<String> deduplicate (LinkedList<String> input) {
// Converting the LinkedList to a LinkedHashSet removes duplicates
// while preserving order.
LinkedHashSet<String> output = new LinkedHashSet<String>(input);
// On return convert it back into a LinkedList, deduplication is
// preserved
return new LinkedList<String>(output);
}
/*---------------------------------------------------------------------
| Method sort(LinkedList<String> input)
|
| Purpose: The sort method sorts the contents of a passed LinkedList
| by String length from higher to lower. In the case of a tie
| the order passed is retained.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Parameters:
| LinkedList<String> input --
| The input parameter should contain a LinkedList of Strings to
| be sorted.
|
| Returns: Returns a LinkedList<String> of the sorted contents of
| input.
*-------------------------------------------------------------------*/
public static LinkedList<String> sort (LinkedList<String> input) {
// Declare an empty output LinkedList to be pushed to.
LinkedList<String> output = new LinkedList<String>();
// Keep iteration going until input is depleted of values.
while(!(input.isEmpty())) {
// Set a comparator as the first value in the LinkedList
String comparator = input.get(0);
// For all the values in the LinkedList compare to see if there any
// that are larger than the comparator, if so then set the
// comparator to this new longest String.
for(String s : input) {
// We don't want to include quotes or hypens in our length
// comparison so we call the ruler method on the Strings to
// measure our Strings before comparing them.
if(ruler(s) > ruler(comparator))
comparator = s;
}
// When we run out of values that could be larger, we now have our
// longest String which is added to our output LinkedList.
output.add(comparator);
// The longest value is then removed from the input LinkedList so
// that we can find the next largest String later.
input.remove(comparator);
}
return output;
}
/*---------------------------------------------------------------------
| Method ruler(String input)
|
| Purpose: The ruler method measures the length of a passed String,
| but compensates for characters that are not to be counted:
| namely quotation marks (") and hyphen marks (-).
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Parameters:
| String input --
| The input parameter should contain a String whose length
| is to be measured.
|
| Returns: Returns a int representing the formulated length of the
| String input.
*-------------------------------------------------------------------*/
public static int ruler (String input) {
// Initialize output as the default clause: the passed length
int output = input.length();
// Empty? Length is 0
if(input == "")
output = 0;
// Is the first char in the String a hyphen?
else if(input.charAt(0) == '-')
// Get the length of the String with no leading hyphens in it.
output = input.replaceAll("^[-]+", "").length();
// Is the first char in the String a quote?
else if (input.charAt(0) == '"')
// Output is the length - 2 to compensate for each quote.
output = input.length() - 2;
// No conditions met? Return default clause set on init.
return output;
}
/*---------------------------------------------------------------------
| Method stringify(LinkedList<String> input)
|
| Purpose: The stringify method sets the contents of a passed LinkedList
| to a printable string with spaces in between each value.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Pre-condition: None, this is a static method that does not require or
| change any global variables.
|
| Parameters:
| LinkedList<String> input --
| The input parameter should contain a LinkedList of Strings to
| be converted into a space seperated string of the contents of
| the LinkedList.
|
| Returns: Returns a String of the contents of input delimited by
| spaces, leading and trailing spaces are trimmed.
*-------------------------------------------------------------------*/
public static String stringify (LinkedList<String> input) {
String output = "";
// For every String in the passed LinkedList append the value and
// a trailing space to the output String.
for(String s : input)
output += (s + " ");
// Return output.trim() removing trailing and leading whitespace
return output.trim();
}
}
@rchasman
Copy link
Author

This was coded the semester proceeding a summer internship at the iPlant Collaborative where I learned functional paradigms with Clojure. Reviewing the code now makes this seem humorously apparent in the structure of the program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment