fork(6) download
  1. package algorithms;
  2.  
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. /**
  8.  * @author m1st
  9.  */
  10. public class AlgorithmsStarter {
  11. public static void main(String[] args) throws IOException {
  12. /**
  13.  * Problem 1.
  14.  * Given array a of N elements transpose it as follows
  15.  * {a[N-1], a[N-2],…, a[0]}
  16.  * Memory consumption (not including the space for array) – O(1)
  17.  * Running time – O(N)
  18.  */
  19. Object[] array = {1, 5, 2, 9, 0};
  20. AlgorithmsSolver.transposeArray(array);
  21. AlgorithmsSolver.println("Problem 1: " + Arrays.asList(array));
  22.  
  23. /**
  24.  * Problem 2.
  25.  * Given sorted arrays a of n elements and b of m elements produce sorted array c of n+m elements that consists of all elements from arrays a and b.
  26.  * Program reads and stores the arrays in files a, b and c one number per line.
  27.  * Memory consumption – O(1)
  28.  * Running time – O(n+m)
  29.  */
  30. AlgorithmsSolver.produceSortedArray("C:\\a.txt", "C:\\b.txt", "C:\\c.txt");
  31. AlgorithmsSolver.println("Problem 2: see stored file 'c.txt'");
  32.  
  33. /**
  34.  * Problem 3.
  35.  * The following function double rand() returns a random floating point number evenly distributed over interval [0;1).
  36.  * Write an expression (or a function) that returns
  37.  */
  38. //a) a random integer N so that 7 <= N <= 23
  39. AlgorithmsSolver.println("Problem 3 (a random integer N so that 7 <= N <= 23): " + AlgorithmsSolver.randInt(7, 23));
  40.  
  41. //b) a random number N from set {3.5, 4.5, 5.5, …, 13.5}
  42. List<Float> numbers = Arrays.asList(3.5f, 4.5f, 5.5f, 6.5f, 7.5f, 8.5f, 9.5f, 10.5f, 11.5f, 12.5f, 13.5f);
  43. AlgorithmsSolver.println("Problem 3 (a random number N from set {3.5, 4.5, 5.5, …, 13.5}): " + AlgorithmsSolver.randFloat(numbers));
  44.  
  45. //c) a random string from set { “asdf”, “a”, “jkl;”, “cc”, “zaxd” }
  46. List<String> strings = Arrays.asList("asdf", "a", "jkl;", "cc", "zaxd");
  47. AlgorithmsSolver.println("Problem 3 (a random string from set { “asdf”, “a”, “jkl;”, “cc”, “zaxd” }): " + AlgorithmsSolver.randString(strings));
  48.  
  49. /**
  50.  * Problem 4.
  51.  * Given array a of N (N > 1) elements and integer K (0 < K <= N-1) transpose the array as follows
  52.  * {a[K], …, a[N-1], a[0],…, a[K-1]}
  53.  * Memory consumption (not including the space for array) – O(1)
  54.  * Running time – O(N).
  55.  *
  56.  * What data structure(s) can be used to implement the mini-framework for this problem effectively?
  57.  */
  58. // todo code & answer here
  59. AlgorithmsSolver.println("Problem 4: todo");
  60.  
  61. /**
  62.  * Problem 5.
  63.  * Write a program that prints all permutations (in any order) of sequence {1, 2, 3, …, N}.
  64.  * Hint: there are N! permutations.
  65.  */
  66. // Could be initialized with N elements and it generates all permutations
  67. AlgorithmsSolver.println("Problem 5 (start):");
  68. AlgorithmsSolver.permutations(1, 2, 3);
  69. AlgorithmsSolver.println("Problem 5 (end).");
  70.  
  71. /**
  72.  * Problem 6.
  73.  * Write a program that takes a string as a parameter and prints out all substrings of the following format: <tag>some text</tag>.
  74.  * Here ‘tag’ can be any alphanumeric character string.
  75.  * The input string is assumed not to have nested tags.
  76.  * Hint: regular expressions should be used for string matching
  77.  *
  78.  * What updates will be required in your program in case of nested tags?
  79.  */
  80. // Enchantments: Takes a file as a parameter. The case of nested tags resolved also.
  81. AlgorithmsSolver.println("Problem 6 (start):");
  82. AlgorithmsSolver.allSubstringsOfNestedTags("C:\\nested_tags.html"); // However, I think that regular expressions are not the best answer here. I'd use XPath to find elements I'm interested in.
  83. AlgorithmsSolver.println("Problem 6 (end).");
  84. }
  85. }
  86.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty