Useful_Tools 020212

2015-01-13

azim58 - Useful_Tools 020212


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class UsefulTools


UsefulTools()
{





this function accepts an arraylist containing arraylists which hold
numbers
the function will then return an arraylist containing only the numbers
considered "consistent"
public ArrayList findConsistentNumbers(ArrayList list_of_lists)

double threshold = 1;
int occurrence_frequency = 20;
ArrayList consistentList = new ArrayList();
ArrayList list_of_matches = new ArrayList();
ArrayList occurrences = new ArrayList();
first sort all of the lists
for(int i=0; i<list_of_lists.size(); i++)
{
if(list_of_lists.get(i) instanceof ArrayList)
{
Collections.sort((ArrayList)list_of_lists.get(i));

else

break;

}
perform these operations for every arraylist
for(int i=0; i<list_of_lists.size(); i++)

if(list_of_lists.get(i) instanceof ArrayList)
{
ArrayList current_list = (ArrayList)list_of_lists.get(i);
for(int j=0; j<current_list.size(); j++)
{
boolean match = false;
note I changed k=i+1 to k=0 here to include more similar numbers
for(int k =0; k<list_of_lists.size(); k++) compare the item in the
current list with the next list
{
ArrayList compare_list = (ArrayList)list_of_lists.get(k);
for(int l=0; l<compare_list.size(); l++)
{
double compare_number = -1;
don't match a number with itself in the same list
if(i!=k)
{
compare_number =
Double.valueOf(compare_list.get(l).toString()).doubleValue();

double current_number =
Double.valueOf(current_list.get(j).toString()).doubleValue();
if the current number in the compare list is too large, then break out of
this loop
if(compare_number - current_number>threshold)

break;

if(compare_number==current_number ||
((compare_number-current_number)<threshold &&
(compare_number-current_number)>0) ||
((current_number-compare_number)<threshold &&
(current_number-compare_number)>0) )

if there hasn't been a match before, add this number to the list of
matches
set the number of occurrences to 1 so far
if(match==false)
{
list_of_matches.add(current_list.get(j));
occurrences.add(Integer.valueOf(1));
match = true;

else

otherwise just increase the number of occurrences
Integer integer =
Integer.valueOf(Integer.valueOf(occurrences.get(occurrences.size()-1).toStr
ing()).intValue()+1);


occurrences.set(occurrences.size()-1, integer);

}
}
}
}
}
else

break;

}
for(int i=0; i<list_of_matches.size(); i++)

if the number was found more than x % of the time add it to the
consistent list
if(Double.valueOf(occurrences.get(i).toString()).doubleValue()/list_of_list
s.size()*100>occurrence_frequency)
{
consistentList.add(list_of_matches.get(i));

}
return consistentList;
}


this shifts all of the numbers in an arraylist by a certain amount
public ArrayList shiftNumbers(double amount, ArrayList the_list)

for(int i=0; i<the_list.size(); i++)
{
double d=0;
try {
String s = the_list.get(i).toString();
d = Double.valueOf(s.trim()).doubleValue();
catch (NumberFormatException nfe)

System.out.println("NumberFormatException: " + nfe.getMessage());

the_list.set(i, Double.valueOf(d+amount));
}
return the_list;
}


public ArrayList stringWithCommasToArrayList(String s)



ArrayList return_list = new ArrayList();
String[] input_array = s.split(",");
for(int i=0; i<input_array.length; i++)
{
return_list.add(input_array[i]);

for(int i=0; i<return_list.size(); i++)

String s_variable = return_list.get(i).toString();
if(s_variable.substring(0, 1).equals(" "))
{
s_variable = s_variable.substring(1,s_variable.length());
return_list.set(i, s_variable);

}
return return_list;
}


public ArrayList convertDoublestoArray(double[] s)

ArrayList the_list = new ArrayList();
for(int i=0; i<s.length; i++)
{
the_list.add(Double.valueOf(s[i]));

return the_list;
}


this is a recursive function that will take an array list that may be
made of
arraylists and make it one list that is only filled with objects
public ArrayList flattenList(ArrayList main_list)

ArrayList new_list = new ArrayList();
go through every element in the main_list
for(int i=0; i<main_list.size(); i++)
{


ArrayList temporary_list = new ArrayList();
if this element is an arraylist
if(main_list.get(i) instanceof ArrayList)
{
store the first element of the main list into a temporary list
temporary_list = (ArrayList)main_list.get(i);
go through all of the elements in this arraylist that is a sublist of the
main list
for(int j=0; j<temporary_list.size(); j++)
{
if this element of this sublist is not an array list
we've reached a base case so we should add this single object
to the new list.
if(!(temporary_list.get(j) instanceof ArrayList))
{
new_list.add(temporary_list.get(j));

if we haven't reached the base case
add this array list to the new_list and then recursively call
the flattenList function to flatten this new list
eventually the base case will be reached.
else

new_list.add(temporary_list.get(j));
new_list = flattenList(new_list);

}
}
if the object wasn't an array list to start with, then just add it to the
new list
else

new_list.add(main_list.get(i));



}
return the new flattened list we've made
return new_list;
}


public ArrayList removeDuplicates(ArrayList l1)

ArrayList l2 = new ArrayList();
for(int l1_count=0; l1_count<l1.size(); l1_count++)
{
boolean has_element=false;
for(int l2_count=0; l2_count<l2.size(); l2_count++)
{
if(l1.get(l1_count).equals(l2.get(l2_count)))
{
has_element = true;

}
if(!has_element)

l2.add(l1.get(l1_count));

}
return l2;
}


this function returns an arraylist with two numbers the starting index of
the string within the sequence and the ending index of the string
public ArrayList findPositionOfString(String string, String query)

ArrayList position_data = new ArrayList();
int start_index = 0;


while(true)
{
int current_index = string.indexOf(query, start_index);
if(current_index>-1)
{
position_data.add(Integer.valueOf(current_index+1));
position_data.add(Integer.valueOf(current_index+query.length()));
start_index = current_index+1;

else

break;

}
return position_data;
}


public boolean approximatelyEquivalent(double first_number, double
second_number, double threshold)

if(first_number==second_number || ((second_number-first_number)<threshold
&& (second_number-first_number)>0) ||
((first_number-second_number)<threshold &&
(first_number-second_number)>0) )
{
return true;

else

return false;

}


public int getMatches(String input, String match_to_count)



int start_index = 0;
int count = 0;
while(true)
{
int current_index = input.indexOf(match_to_count, start_index);
if(current_index>-1)
{
start_index = current_index+1;
count++;

else

break;

}
return count;
}


public String getFirstMatchofRegEx(String input_string, String regex)

Pattern pattern =
Pattern.compile(regex);

Matcher matcher =
pattern.matcher(input_string);


String first_match = "";
boolean found = false;
while (matcher.find()) {
if(found ==false)
{
first_match = matcher.group();

found = true;
}
return first_match;
}


public ArrayList getAllMatchesofRegEx(String input_string, String regex)

ArrayList return_list = new ArrayList();
boolean all_found = false;
while(!all_found)
{
String match = getFirstMatchofRegEx(input_string, regex);
if(match.equals(""))
{
all_found=true;

else

return_list.add(match);
int start_position = input_string.indexOf(match);
input_string =
input_string.substring(start_position+1,input_string.length());

}
return return_list;
}


public boolean isMatchofRegEx(String input_string, String regex)

Pattern pattern =
Pattern.compile(regex);

Matcher matcher =
pattern.matcher(input_string);


String first_match = "";
boolean found = false;
while (matcher.find()) {
if(found ==false)
{
first_match = matcher.group();

found = true;
}
return found;
}


public String generateCodeForClass(String class_name, String variables)

ArrayList list_variables = stringWithCommasToArrayList(variables);
return generateCodeForClass(class_name, list_variables);



each item in the variables ArrayList should be a string with the type and
name of the variable
public String generateCodeForClass(String class_name, ArrayList variables)

ArrayList type = new ArrayList();
ArrayList name = new ArrayList();
String word = "[A-za-z0-9_]+";
for(int i=0; i<variables.size(); i++)
{
String s_variable = variables.get(i).toString();
String s_type = getFirstMatchofRegEx(s_variable, word + " ");
s_type = getFirstMatchofRegEx(s_type, word);
String s_name = getFirstMatchofRegEx(s_variable, " " + word);
s_name = getFirstMatchofRegEx(s_name, word);
type.add(s_type);
name.add(s_name);



String return_string = "public class " + class_name + "
";

for(int i=0; i<variables.size(); i++)
{
return_string+= "\n" + "\t" + "private " + variables.get(i).toString() +
";";

return_string+="\n\n\tpublic " + class_name + "(" + variables.get(0);
for(int i=1; i<variables.size(); i++)

return_string+=", " + variables.get(i);

return_string+=")\n\t
";

for(int i=0; i<variables.size(); i++)
{
return_string+="\n\t\tthis." + name.get(i) + " = " + name.get(i) + ";";

return_string+="\n\t}\n\n";
for(int i=0; i<variables.size(); i++)

return_string+= "\n" + "\t" + "public void set_" +
name.get(i).toString() + "(" + type.get(i).toString() + " " +
name.get(i).toString() + ")"
+ "\n" + "\t" + "{" + "\n" +"\t\t" + "this." +
name.get(i).toString() + " = " + name.get(i).toString() + ";"
+ "\n" + "\t" + "
" + "\n" + "\t" + "public " + type.get(i) + " get_" +
name.get(i).toString() + "()" + "\n\t
"

+"\n\t\t" + "return " + name.get(i).toString() + ";" + "\n\t" + "
"
;
}
return_string += "\n}";




return return_string;
}






/*This function takes in an arraylist consisting of lists. The first item
of each list should be

new list containing lists
public ArrayList analyzeLists(ArrayList lists)

ArrayList return_list = new ArrayList();
int number_of_lists = lists.size();
int number_of_combination_lists = factorial(number_of_lists)+1;
in order to explore all permutations of combinations of lists I will
simply count up to the number of possible list combinations in binary.
for(int i=0; i<=number_of_combination_lists; i++)
{
String current_combination = Integer.toString(i, 2);
ArrayList new_list = new ArrayList();




for(int list_number=0; list_number<current_combination.length();
list_number++)
{
boolean included = false;
try
{
if(current_combination.substring(list_number, list_number+1).equals("1"))
{
included = true;

if(included == true)

new_list.add( lists.get(current_combination.length()-1-list_number) );

}
catch(Exception e)




}


now that we have all of the lists that should be combined, let's combine
them.
When we combine them we should combine them in order from the largest
list to the smallest
so let's sort them
There are better ways to sort, but this is a fairly quick way to think of
ArrayList sorted_list = new ArrayList();
boolean sorted = false;


while(!sorted)

boolean something_was_sorted = false;
for(int new_list_num=1; new_list_num<new_list.size(); new_list_num++)
{
for(int from_beginning=0; from_beginning<new_list_num; from_beginning++)
{
int new_list_num_length = ((ArrayList)new_list.get(new_list_num)).size();
int from_beginning_length = 0;
from_beginning_length = ((ArrayList)new_list.get(from_beginning)).size();
if( new_list_num_length>from_beginning_length )
{


ArrayList temp = new ArrayList( (ArrayList)new_list.get(new_list_num) );
new_list.remove(new_list_num);
new_list.add(from_beginning, temp);
something_was_sorted = true;
break;

}


}
if(something_was_sorted == false)

sorted = true;

}
the list is sorted so let's combine all the lists contained in new_list
and add this list to the return_list
try

ArrayList intersection_list = new ArrayList((ArrayList)new_list.get(0));


String name_of_list = (String)intersection_list.get(0);


for(int intersection_count=1; intersection_count<new_list.size();
intersection_count++)
{
String additional_name =
(String)((ArrayList)new_list.get(intersection_count)).get(0);
name_of_list+= "_and_" + additional_name;
intersection_list.retainAll((ArrayList)new_list.get(intersection_count));

if(new_list.size()>1)

intersection_list.add(0, name_of_list);

return_list.add(intersection_list);
}
catch(Exception e)




}






return return_list;
}

Evaluate n!
public static int factorial( int n )

if( n <= 1 ) base case
return 1;
else
return n * factorial( n - 1 );



public ArrayList storeTextFiletoArrayList(String file_path)

ArrayList return_list = new ArrayList();
try{
FileInputStream fstream = new FileInputStream(file_path);
Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
Read File Line By Line
while ((strLine = br.readLine()) != null) {


return_list.add(strLine);

Close the input stream
in.close();
}catch (Exception e)
Catch exception if any

System.err.println("Error: " + e.getMessage());

return return_list;
}


public String storeTextFiletoString(String file_path)

String return_string = "";
ArrayList return_list = new ArrayList();
try{
FileInputStream fstream = new FileInputStream(file_path);
Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
Read File Line By Line
while ((strLine = br.readLine()) != null) {


return_list.add(strLine);

Close the input stream
in.close();
}catch (Exception e)
//Catch exception if any

System.err.println("Error: " + e.getMessage());

for(int i=0; i<return_list.size(); i++)

return_string+=return_list.get(i) + "\r";

return return_string;
}


public double evaluateExpression(String expression)

Expression expression_object = new Expression(expression);
return expression_object.evaluate();



public void createTextFile(String directory, String file_name, String
text)

try
{
Writer output = null;
File file = new File(directory+"\\" + file_name );
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();

catch(Exception e)




}


public String getTime()

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);

}