Split algorithms to separate files
This commit is contained in:
146
src/main/java/mattrixwv/ArrayAlgorithms.java
Normal file
146
src/main/java/mattrixwv/ArrayAlgorithms.java
Normal file
@@ -0,0 +1,146 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/ArrayAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 07-03-21
|
||||
//This class contains algorithms for vectors that I've found it useful to keep around
|
||||
/*
|
||||
Copyright (C) 2021 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package mattrixwv;
|
||||
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class ArrayAlgorithms{
|
||||
//This function returns the sum of all elements in the list
|
||||
public static int getSum(ArrayList<Integer> nums){
|
||||
//If a blank list was passed to the function return 0 as the sum
|
||||
if(nums.size() == 0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
int sum = 0;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(int num : nums){
|
||||
sum += num;
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
public static long getLongSum(ArrayList<Long> nums){
|
||||
//If a blank list was passed to the function return 0 as the sum
|
||||
if(nums.size() == 0){
|
||||
return 0L;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
long sum = 0L;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(long num : nums){
|
||||
sum += num;
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
public static BigInteger getBigSum(ArrayList<BigInteger> nums){
|
||||
//If a blank list was passed to the function return 0 as the sum
|
||||
if(nums.size() == 0){
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
BigInteger sum = BigInteger.valueOf(0);
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(BigInteger num : nums){
|
||||
sum = sum.add(num);
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
//This function returns the product of all elements in the list
|
||||
public static int getProd(ArrayList<Integer> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
int product = 1; //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(int num : nums){
|
||||
product *= num;
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static long getLongProd(ArrayList<Long> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
return 0L;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
long product = 1L; //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(long num : nums){
|
||||
product *= num;
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static BigInteger getBigProd(ArrayList<BigInteger> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
BigInteger product = BigInteger.valueOf(1); //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(BigInteger num : nums){
|
||||
product = product.multiply(num);
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
//Print a list
|
||||
public static <T> String printList(ArrayList<T> list){
|
||||
StringBuilder listString = new StringBuilder("[");
|
||||
for(int cnt = 0;cnt < list.size();++cnt){
|
||||
listString.append(list.get(cnt));
|
||||
if(cnt < list.size() - 1){
|
||||
listString.append(", ");
|
||||
}
|
||||
}
|
||||
listString.append("]");
|
||||
return listString.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/Algorithms.java
|
||||
//JavaClasses/src/main/java/mattrixwv/NumberAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 03-02-19
|
||||
//Modified: 06-29-21
|
||||
//This class holds many algorithms that I have found it useful to keep around
|
||||
//As such all of the functions in here are static and meant to be used as stand alone functions
|
||||
// Created: 07-03-21
|
||||
//Modified: 07-03-21
|
||||
//This class contains algorithms for numbers that I've found it useful to keep around
|
||||
/*
|
||||
Copyright (C) 2021 Matthew Ellison
|
||||
Copyright (C) 2021 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -31,7 +30,7 @@ import java.util.Collections;
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class Algorithms{
|
||||
public class NumberAlgorithms{
|
||||
//?This is here just to prove that templates exist and for a possible rewrite at a later time
|
||||
public static <T> T getNum(T num1){
|
||||
return num1;
|
||||
@@ -756,203 +755,6 @@ public class Algorithms{
|
||||
}
|
||||
return num1.or(num2);
|
||||
}
|
||||
//This function returns the sum of all elements in the list
|
||||
public static int getSum(ArrayList<Integer> nums){
|
||||
//If a blank list was passed to the function return 0 as the sum
|
||||
if(nums.size() == 0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
int sum = 0;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(int num : nums){
|
||||
sum += num;
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
public static long getLongSum(ArrayList<Long> nums){
|
||||
//If a blank list was passed to the function return 0 as the sum
|
||||
if(nums.size() == 0){
|
||||
return 0L;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
long sum = 0L;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(long num : nums){
|
||||
sum += num;
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
public static BigInteger getBigSum(ArrayList<BigInteger> nums){
|
||||
//If a blank list was passed to the function return 0 as the sum
|
||||
if(nums.size() == 0){
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
BigInteger sum = BigInteger.valueOf(0);
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(BigInteger num : nums){
|
||||
sum = sum.add(num);
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
//This function returns the product of all elements in the list
|
||||
public static int getProd(ArrayList<Integer> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
int product = 1; //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(int num : nums){
|
||||
product *= num;
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static long getLongProd(ArrayList<Long> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
return 0L;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
long product = 1L; //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(long num : nums){
|
||||
product *= num;
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static BigInteger getBigProd(ArrayList<BigInteger> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
BigInteger product = BigInteger.valueOf(1); //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(BigInteger num : nums){
|
||||
product = product.multiply(num);
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
//This function returns true if key is found in ary
|
||||
public static Boolean isFound(ArrayList<Integer> ary, Integer key){
|
||||
//Look through every element in the array, looing for the key element
|
||||
for(Integer num : ary){
|
||||
//If there is an element in the array that is the same as key return true
|
||||
if(num.equals(key)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//If you made it to the end of the array without finding a match return false because the element was not found
|
||||
return false;
|
||||
}
|
||||
public static Boolean isLongFound(ArrayList<Long> ary, Long key){
|
||||
//Look through every element in the array, looing for the key element
|
||||
for(Long num : ary){
|
||||
//If there is an element in the array that is the same as key return true
|
||||
if(num.equals(key)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//If you made it to the end of the array without finding a match return false because the element was not found
|
||||
return false;
|
||||
}
|
||||
public static Boolean isBigFound(ArrayList<BigInteger> ary, BigInteger key){
|
||||
//Look through every element in the array, looing for the key element
|
||||
for(BigInteger num : ary){
|
||||
//If there is an element in the array that is the same as key return true
|
||||
if(num.equals(key)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//If you made it to the end of the array without finding a match return false because the element was not found
|
||||
return false;
|
||||
}
|
||||
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
public static ArrayList<String> getPermutations(String master){
|
||||
return getPermutations(master, 0);
|
||||
}
|
||||
private static ArrayList<String> getPermutations(String master, int num){
|
||||
ArrayList<String> perms = new ArrayList<String>();
|
||||
//Check if the number is out of bounds
|
||||
if((num >= master.length()) || (num < 0)){
|
||||
//Do nothing and return an empty arraylist
|
||||
}
|
||||
//If this is the last possible recurse just return the current string
|
||||
else if(num == (master.length() - 1)){
|
||||
perms.add(master);
|
||||
}
|
||||
//If there are more possible recurses, recurse with the current permutation
|
||||
else{
|
||||
ArrayList<String> temp = getPermutations(master, num + 1);
|
||||
perms.addAll(temp);
|
||||
//You need to swap the current letter with every possible letter after it
|
||||
//The ones needed to swap before will happen automatically when the function recurses
|
||||
for(int cnt = 1;(num + cnt) < master.length();++cnt){
|
||||
master = swapString(master, num, (num + cnt));
|
||||
temp = getPermutations(master, num + 1);
|
||||
perms.addAll(temp);
|
||||
master = swapString(master, num, (num + cnt));
|
||||
}
|
||||
|
||||
//The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
|
||||
if(num == 0){
|
||||
Collections.sort(perms);
|
||||
}
|
||||
}
|
||||
|
||||
//Return the arraylist that was built
|
||||
return perms;
|
||||
}
|
||||
private static String swapString(String str, int first, int second){
|
||||
char[] tempStr = str.toCharArray();
|
||||
char temp = tempStr[first];
|
||||
tempStr[first] = tempStr[second];
|
||||
tempStr[second] = temp;
|
||||
|
||||
String swappedString = new String(tempStr);
|
||||
return swappedString;
|
||||
}
|
||||
//This function returns the number of times the character occurs in the string
|
||||
public static long findNumOccurrence(String str, char c){
|
||||
return str.chars().filter(ch -> ch == c).count();
|
||||
}
|
||||
//Returns true if the string passed in is a palindrome
|
||||
public static boolean isPalindrome(String str){
|
||||
String rev = new StringBuilder(str).reverse().toString();
|
||||
if(str.equals(rev)){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//Converts a number to its binary equivalent
|
||||
public static String toBin(int num){
|
||||
//Convert the number to a binary string
|
||||
@@ -966,16 +768,4 @@ public class Algorithms{
|
||||
//Conver the number to binary string
|
||||
return num.toString(2);
|
||||
}
|
||||
//Print a list
|
||||
public static <T> String printList(ArrayList<T> list){
|
||||
StringBuilder listString = new StringBuilder("[");
|
||||
for(int cnt = 0;cnt < list.size();++cnt){
|
||||
listString.append(list.get(cnt));
|
||||
if(cnt < list.size() - 1){
|
||||
listString.append(", ");
|
||||
}
|
||||
}
|
||||
listString.append("]");
|
||||
return listString.toString();
|
||||
}
|
||||
}
|
||||
90
src/main/java/mattrixwv/StringAlgorithms.java
Normal file
90
src/main/java/mattrixwv/StringAlgorithms.java
Normal file
@@ -0,0 +1,90 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 07-03-21
|
||||
//This class contains algorithms for strings that I've found it useful to keep around
|
||||
/*
|
||||
Copyright (C) 2021 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package mattrixwv;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
|
||||
public class StringAlgorithms{
|
||||
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
public static ArrayList<String> getPermutations(String master){
|
||||
return getPermutations(master, 0);
|
||||
}
|
||||
private static ArrayList<String> getPermutations(String master, int num){
|
||||
ArrayList<String> perms = new ArrayList<String>();
|
||||
//Check if the number is out of bounds
|
||||
if((num >= master.length()) || (num < 0)){
|
||||
//Do nothing and return an empty arraylist
|
||||
}
|
||||
//If this is the last possible recurse just return the current string
|
||||
else if(num == (master.length() - 1)){
|
||||
perms.add(master);
|
||||
}
|
||||
//If there are more possible recurses, recurse with the current permutation
|
||||
else{
|
||||
ArrayList<String> temp = getPermutations(master, num + 1);
|
||||
perms.addAll(temp);
|
||||
//You need to swap the current letter with every possible letter after it
|
||||
//The ones needed to swap before will happen automatically when the function recurses
|
||||
for(int cnt = 1;(num + cnt) < master.length();++cnt){
|
||||
master = swapString(master, num, (num + cnt));
|
||||
temp = getPermutations(master, num + 1);
|
||||
perms.addAll(temp);
|
||||
master = swapString(master, num, (num + cnt));
|
||||
}
|
||||
|
||||
//The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
|
||||
if(num == 0){
|
||||
Collections.sort(perms);
|
||||
}
|
||||
}
|
||||
|
||||
//Return the arraylist that was built
|
||||
return perms;
|
||||
}
|
||||
private static String swapString(String str, int first, int second){
|
||||
char[] tempStr = str.toCharArray();
|
||||
char temp = tempStr[first];
|
||||
tempStr[first] = tempStr[second];
|
||||
tempStr[second] = temp;
|
||||
|
||||
String swappedString = new String(tempStr);
|
||||
return swappedString;
|
||||
}
|
||||
//This function returns the number of times the character occurs in the string
|
||||
public static long findNumOccurrence(String str, char c){
|
||||
return str.chars().filter(ch -> ch == c).count();
|
||||
}
|
||||
//Returns true if the string passed in is a palindrome
|
||||
public static boolean isPalindrome(String str){
|
||||
String rev = new StringBuilder(str).reverse().toString();
|
||||
if(str.equals(rev)){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user