Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1.  
  2.  
  3. public class Main {
  4.  
  5.  
  6. /* TODO: checkForInterleaving method must call itself recursively
  7. * in order to find different permutations of x and y within itself.
  8. *
  9. * Example is on line 38
  10. *
  11. */
  12.  
  13. public static int[] copyArrayFromPosition(int[] array, int position) {
  14. int[] newArray = new int[array.length-position];
  15. for (int i = 0; i <array.length-position; i++) {
  16. newArray[i] = array[i+position];
  17. }
  18. return newArray;
  19. }
  20.  
  21.  
  22. public static boolean checkForInterleaving(int[] x, int[] y, int[] s, String priority) {
  23.  
  24. int x_counter = 0;
  25. int y_counter = 0;
  26. int matchedUpto;
  27. boolean is_interleaving = true;
  28.  
  29.  
  30. for (int i = 0; i < s.length; i++) {
  31.  
  32. /* This needs to be recursive
  33. * A large array needs to be created in order to keep track of all
  34. * permutations of organizing x and y
  35. * ie, if x and y match at some point the remaining indices
  36. * need to also interleave given that certain Xes and Yes have matched
  37. * to a certain point.
  38. */
  39. if(s[i] != x[x_counter] && s[i] != y[y_counter]) {
  40. is_interleaving = false;
  41. break;
  42. }
  43. if(s[i] == x[x_counter] && s[i] == y[y_counter]) {
  44. s = copyArrayFromPosition(s, i);
  45.  
  46. }
  47.  
  48. if(priority == "x") {
  49. if(s[i] == x[x_counter]) {
  50. x_counter++;
  51. } else if(s[i] == y[y_counter]) {
  52. y_counter++;
  53. }
  54. else {
  55. if (s[i] == y[y_counter]) {
  56. y_counter++;
  57. }
  58. else if(s[i] == x[x_counter]) {
  59. x_counter++;
  60. }
  61. }
  62.  
  63. }
  64.  
  65.  
  66. if(x_counter > x.length-1 ) {
  67. x_counter = 0;
  68. }
  69.  
  70. if(y_counter > y.length-1) {
  71. y_counter = 0;
  72. }
  73. }
  74. if(x_counter != x.length || y_counter != y.length) {
  75. is_interleaving = false;
  76. }
  77. return is_interleaving;
  78. }
  79.  
  80. /* Returns an array with last element deleted */
  81.  
  82. public static int[] deleteFromEnd(int[] array) {
  83. int[] newArray;
  84. newArray = new int[array.length-2];
  85. for(int i = 0; i < array.length-2; i++) {
  86. newArray[i] = array[i];
  87. }
  88. return newArray;
  89. }
  90.  
  91. public static int[] deleteFromBeginning(int[] array) {
  92. int[] newArray;
  93. newArray = new int[array.length-2];
  94. for( int i = 0; i < array.length-2; i++) {
  95. newArray[i] = array[i+1];
  96. }
  97. return newArray;
  98. }
  99.  
  100.  
  101. public static void printOutArray(int[] array) {
  102. for(int i = 0; i < array.length; i++ ) {
  103. System.out.print(array[i] + ", ");
  104. }
  105. System.out.println("");
  106. }
  107.  
  108.  
  109.  
  110. public static void main(String[] args) {
  111. int s[] = new int[] {1,0,0,0,1,0,1,0,1,1,1,1};
  112. int x[] = new int[] {1,0,1};
  113. int y[] = new int[] {0};
  114. int x_size = x.length;
  115. int y_size = y.length;
  116. int s_size = s.length;
  117. int[] deleteFromEndS = s;
  118. int[] deleteFromBeginningS = s;
  119.  
  120. printOutArray(s);
  121. while(checkForInterleaving(x,y,s,"x") != true && s_size > (x_size + y_size)) {
  122. deleteFromEndS =deleteFromEnd(s);
  123. }
  124. while(checkForInterleaving(x,y,s,"x") != true && s_size > (x_size + y_size)) {
  125. deleteFromBeginningS= deleteFromBeginning(s);
  126. }
  127.  
  128.  
  129. printOutArray(s);
  130.  
  131. // TODO: Check for interleaving from the front, Backtrack if failing to check for
  132. // different orderings of x and y
  133. }
  134.  
  135.  
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement