Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- public class Library {
- public static String[] getPermutations(String s){
- StringBuilder sb = new StringBuilder();
- boolean[] used = new boolean[s.length()];
- ArrayList<String> list = getPerms(s, 0, used, sb, new ArrayList<String>());
- return list.toArray(new String[list.size()]);
- }
- private static ArrayList<String> getPerms(String input, int pos, boolean[] used, StringBuilder sb, ArrayList<String> list){
- if (pos == input.length()){
- list.add(sb.toString());
- return list;
- }
- for (int i = 0; i < input.length(); i++){
- //If the character has already been used, don't append it.
- if (used[i]) continue;
- used[i] = true;
- sb.append(input.charAt(i)); //append char to string builder
- getPerms(input, pos + 1, used, sb, list); //recursive call
- sb.setLength(sb.toString().length() - 1); //remove last char
- used[i] = false;
- }
- return list;
- }
- public static void printArray(int[][] array){
- for(int i = 0; i < array.length; i++){
- for(int j = 0; j < array[i].length; j++){
- System.out.print(array[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static void printArray(Object[] array){
- for(Object o : array) System.out.println(array[0] instanceof String ? o : o.toString());
- }
- public static void printArray(int[] array){
- for(int i : array) System.out.println(i);
- }
- public static void printArray(String[] array){
- for(String s : array) System.out.println(s);
- }
- /**
- * @return sum of all values in array
- */
- public static int getSum(int[] array){
- int sum = 0;
- for(int i : array) sum += i;
- return sum;
- }
- public static int[] shuffle(int[] n){
- int j, t;
- for(int i = n.length-1; i > 0; i--){
- j = (int) (Math.random()*i)+1;
- t = n[i];
- n[i] = n[j];
- n[j] = t;
- }
- return n;
- }
- /**
- * - Bubble Sort -
- */
- public static int[] sort(int[] values){
- int j = 0;
- boolean flag = true;
- while(flag){
- flag = false;
- for(int i = 0; i < values.length-1; i++){
- if(values[i] < values[i+1]){
- j = values[i];
- values[i] = values[i+1];
- values[i+1] = j;
- flag = true;
- }
- }
- }
- return values;
- }
- public static String[] sortByLength(String[] s){
- String j = "";
- boolean flag = true;
- while(flag){
- flag = false;
- for(int i = 0; i < s.length-1; i++){
- if(s[i].length() < s[i+1].length()){
- j = s[i];
- s[i] = s[i+1];
- s[i+1] = j;
- flag = true;
- }
- }
- }
- return s;
- }
- public static String[] toStringArray(ArrayList<String> s){
- return s.toArray(new String[s.size()]);
- }
- /**
- * @return True if the parameter is an integer
- */
- public boolean isNumber(String s){
- try{
- Integer.parseInt(s);
- return true;
- } catch(Exception e){
- return false;
- }
- }
- /**
- * @return true if the parameter is a consonant
- */
- public static boolean isConsonant(char c) throws Exception{
- if(Character.isAlphabetic(c)){
- return !isVowel(c);
- } else{
- throw new Exception("Parameter is not alphabetic");
- }
- }
- /**
- * @return true if the parameter is a vowel
- */
- public static boolean isVowel(char c) throws Exception{
- if(Character.isAlphabetic(c)){
- return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
- } else{
- throw new Exception("Parameter is not alphabetic");
- }
- }
- /**
- * @param the upper limit of the list of prime numbers
- */
- public static boolean[] getPrimes(int limit){
- boolean[] isPrime = new boolean[limit + 1];
- for (int i = 2; i <= limit; i++) {
- isPrime[i] = true;
- }
- for (int i = 2; i*i <= limit; i++) {
- if (isPrime[i]) {
- for (int j = i; i*j <= limit; j++) {
- isPrime[i*j] = false;
- }
- }
- }
- return isPrime;
- }
- public static boolean isPrime(int n){
- return n >= Integer.MAX_VALUE ? false : getPrimes(n)[n];
- }
- public static String[] readFromFile(String filename){
- ArrayList<String> t = new ArrayList<String>();
- try{
- BufferedReader buffRead = new BufferedReader(new FileReader(new File(filename.endsWith(".txt") ? filename : filename + ".txt")));
- String line = buffRead.readLine();
- while(line != null){
- t.add(line);
- line = buffRead.readLine();
- }
- buffRead.close();
- } catch(IOException e){
- e.printStackTrace();
- }
- return t.toArray(new String[t.size()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment