Guest User

Untitled

a guest
Jan 2nd, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. public class Library {
  8.  
  9.     public static String[] getPermutations(String s){
  10.         StringBuilder sb = new StringBuilder();
  11.         boolean[] used = new boolean[s.length()];
  12.         ArrayList<String> list = getPerms(s, 0, used, sb, new ArrayList<String>());
  13.         return list.toArray(new String[list.size()]);
  14.     }
  15.  
  16.     private static ArrayList<String> getPerms(String input, int pos, boolean[] used, StringBuilder sb, ArrayList<String> list){
  17.         if (pos == input.length()){
  18.             list.add(sb.toString());
  19.             return list;
  20.         }
  21.         for (int i = 0; i < input.length(); i++){
  22.             //If the character has already been used, don't append it.
  23.             if (used[i])    continue;
  24.  
  25.             used[i] = true;
  26.             sb.append(input.charAt(i)); //append char to string builder
  27.             getPerms(input, pos + 1, used, sb, list); //recursive call
  28.             sb.setLength(sb.toString().length() - 1); //remove last char
  29.             used[i] = false;
  30.         }
  31.         return list;
  32.     }
  33.  
  34.     public static void printArray(int[][] array){
  35.         for(int i = 0; i < array.length; i++){
  36.             for(int j = 0; j < array[i].length; j++){
  37.                 System.out.print(array[i][j] + "  ");
  38.             }
  39.             System.out.println();
  40.         }
  41.     }
  42.  
  43.     public static void printArray(Object[] array){
  44.         for(Object o : array)   System.out.println(array[0] instanceof String ? o : o.toString());
  45.     }
  46.  
  47.     public static void printArray(int[] array){
  48.         for(int i : array)  System.out.println(i);
  49.     }
  50.  
  51.     public static void printArray(String[] array){
  52.         for(String s : array)   System.out.println(s);
  53.     }
  54.  
  55.     /**
  56.      * @return sum of all values in array
  57.      */
  58.     public static int getSum(int[] array){
  59.         int sum = 0;
  60.         for(int i : array)  sum += i;
  61.         return sum;
  62.     }
  63.    
  64.     public static int[] shuffle(int[] n){
  65.         int j, t;
  66.         for(int i = n.length-1; i > 0; i--){
  67.             j = (int) (Math.random()*i)+1;
  68.             t = n[i];
  69.             n[i] = n[j];
  70.             n[j] = t;
  71.         }
  72.         return n;
  73.     }
  74.  
  75.     /**
  76.      * - Bubble Sort -
  77.      */
  78.     public static int[] sort(int[] values){
  79.         int j = 0;
  80.         boolean flag = true;
  81.         while(flag){
  82.             flag = false;
  83.             for(int i = 0; i < values.length-1; i++){
  84.                 if(values[i] < values[i+1]){
  85.                     j = values[i];
  86.                     values[i] = values[i+1];
  87.                     values[i+1] = j;
  88.                     flag = true;
  89.                 }
  90.             }
  91.         }
  92.         return values;
  93.     }
  94.    
  95.     public static String[] sortByLength(String[] s){
  96.         String j = "";
  97.         boolean flag = true;
  98.         while(flag){
  99.             flag = false;
  100.             for(int i = 0; i < s.length-1; i++){
  101.                 if(s[i].length() < s[i+1].length()){
  102.                     j = s[i];
  103.                     s[i] = s[i+1];
  104.                     s[i+1] = j;
  105.                     flag = true;
  106.                 }
  107.             }
  108.         }
  109.         return s;
  110.     }
  111.  
  112.     public static String[] toStringArray(ArrayList<String> s){
  113.         return s.toArray(new String[s.size()]);
  114.     }
  115.  
  116.  
  117.     /**
  118.      * @return True if the parameter is an integer
  119.      */
  120.     public boolean isNumber(String s){
  121.         try{
  122.             Integer.parseInt(s);
  123.             return true;
  124.         } catch(Exception e){
  125.             return false;
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * @return true if the parameter is a consonant
  131.      */
  132.     public static boolean isConsonant(char c) throws Exception{
  133.         if(Character.isAlphabetic(c)){
  134.             return !isVowel(c);
  135.         } else{
  136.             throw new Exception("Parameter is not alphabetic");
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * @return true if the parameter is a vowel
  142.      */
  143.     public static boolean isVowel(char c) throws Exception{
  144.         if(Character.isAlphabetic(c)){
  145.             return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
  146.         } else{
  147.             throw new Exception("Parameter is not alphabetic");
  148.         }
  149.     }
  150.  
  151.     /**
  152.      * @param the upper limit of the list of prime numbers
  153.      */
  154.     public static boolean[] getPrimes(int limit){
  155.         boolean[] isPrime = new boolean[limit + 1];
  156.         for (int i = 2; i <= limit; i++) {
  157.             isPrime[i] = true;
  158.         }
  159.  
  160.         for (int i = 2; i*i <= limit; i++) {
  161.             if (isPrime[i]) {
  162.                 for (int j = i; i*j <= limit; j++) {
  163.                     isPrime[i*j] = false;
  164.                 }
  165.             }
  166.         }
  167.         return isPrime;
  168.     }
  169.  
  170.     public static boolean isPrime(int n){
  171.         return n >= Integer.MAX_VALUE ? false : getPrimes(n)[n];
  172.     }
  173.    
  174.    
  175.     public static String[] readFromFile(String filename){
  176.         ArrayList<String> t = new ArrayList<String>();
  177.         try{
  178.             BufferedReader buffRead = new BufferedReader(new FileReader(new File(filename.endsWith(".txt") ? filename : filename + ".txt")));
  179.             String line = buffRead.readLine();
  180.             while(line != null){
  181.                 t.add(line);
  182.                 line = buffRead.readLine();
  183.             }
  184.             buffRead.close();
  185.         } catch(IOException e){
  186.             e.printStackTrace();
  187.         }
  188.         return t.toArray(new String[t.size()]);
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment