fork download
  1. package algorithms;
  2.  
  3. import org.apache.commons.io.FileUtils;
  4. import org.paukov.combinatorics.Factory;
  5. import org.paukov.combinatorics.Generator;
  6. import org.paukov.combinatorics.ICombinatoricsVector;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.PrintWriter;
  12. import java.util.*;
  13.  
  14. import static java.util.Arrays.asList;
  15. import static java.util.Collections.shuffle;
  16.  
  17. /**
  18.  * @author m1st
  19.  * todo: JUnit tests, JavaDocs, Comments, less Memory and Running time consumption, more Negative cases and Exceptions handling
  20.  */
  21. public class AlgorithmsSolver {
  22. public static void println(Object obj) {
  23. System.out.println(obj);
  24. }
  25.  
  26. public static Object[] transposeArray(Object[] array) {
  27. Collections.reverse(Arrays.asList(array));
  28. return array;
  29. }
  30.  
  31. public static Object[] readFile(String filePath) throws FileNotFoundException {
  32. ArrayList<Object> list = new ArrayList<>();
  33. Scanner scanner = new Scanner(new File(filePath));
  34. while (scanner.hasNextLine()) {
  35. list.add(scanner.nextLine());
  36. }
  37. scanner.close();
  38. return list.toArray();
  39. }
  40.  
  41. public static void writeFile(String filePath, Object[] data) throws FileNotFoundException {
  42. PrintWriter writer = new PrintWriter(new File(filePath));
  43. for (Object object : data) {
  44. writer.println(object);
  45. }
  46. writer.close();
  47. }
  48.  
  49. /**
  50.   * The key is to use merge sorting.
  51.   */
  52. public static void produceSortedArray(String aPath, String bPath, String cPath) throws FileNotFoundException {
  53. Object[] a = readFile(aPath);
  54. Object[] b = readFile(bPath);
  55. ArrayList list = new ArrayList(a.length + b.length);
  56. list.addAll(asList(a));
  57. list.addAll(asList(b));
  58. Collections.sort(list);
  59. Object[] c = list.toArray();
  60. writeFile(cPath, c);
  61. }
  62.  
  63. public static int randInt(int min, int max) {
  64. Random rand = new Random();
  65. return rand.nextInt(max - min + 1) + min;
  66. }
  67.  
  68. public static float randFloat(List<Float> list) {
  69. shuffle(list);
  70. return list.get(0);
  71. }
  72.  
  73. public static String randString(List<String> list) {
  74. shuffle(list);
  75. return list.get(0);
  76. }
  77.  
  78. /**
  79.   * Don't reinvent the wheel. Usage of CombinatoricsLib library.
  80.   */
  81. public static void permutations(Integer... numbers) {
  82. ICombinatoricsVector<Integer> initialVector = Factory.createVector(numbers);
  83. Generator<Integer> generator = Factory.createPermutationGenerator(initialVector);
  84. for (ICombinatoricsVector<Integer> perm : generator) {
  85. System.out.println(perm);
  86. }
  87. }
  88.  
  89. /**
  90.   * Don't reinvent the wheel. Usage of Commons IO library.
  91.   */
  92. public static void allSubstringsOfNestedTags(String filePath) throws IOException {
  93. File file = new File(filePath);
  94. String nestedTags = FileUtils.readFileToString(file);
  95. String noTagsString = null;
  96. if (nestedTags != null) {
  97. noTagsString = nestedTags.replaceAll("\\<.*?\\>", "").replaceAll("\\r\\n\\t", "").replaceAll("\\r\\n", "");
  98. }
  99. String[] result = new String[6];
  100. if (noTagsString != null) {
  101. result = noTagsString.split("\\s");
  102. }
  103. String sub;
  104. for (String string : result) {
  105. int length = string.length();
  106. for (int c = 0; c < length; c++) {
  107. for (int i = 1; i <= length - c; i++) {
  108. sub = string.substring(c, c + i);
  109. System.out.println(sub);
  110. }
  111. }
  112. }
  113. }
  114. }
  115.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty