Guest User

Untitled

a guest
Jul 17th, 2017
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. class Length {
  4. private static final IntUtil u = new IntUtil();
  5. //YOU CANNOT ADD ANY VARIABLE HERE
  6.  
  7. private static int length_easy(int [] s, int x) {
  8. //CANNOT CHANGE BELOW
  9. int l = 0 ;
  10. int gx = x ;
  11. while (true) {
  12. if (s[x] == gx) {
  13. return l ;
  14. }
  15. x = s[x] ;
  16. ++l ;
  17. }
  18. }
  19.  
  20. /*
  21. YOU CANNOT USE ANY static variable in this function
  22. YOU can use as many local variables as you need
  23. Cannot use any loop statements like for, while, do, while, go to
  24. Can use if.
  25. ONLY AFTER THE execution of this routine array s MUST have the same content as you got it.
  26. YOU cannot call any subroutine inside this function except length itself (NOT length_easy)
  27. */
  28. private static int length(int [] s, int x) {
  29. //WRITE CODE HERE
  30. if (s[x] == x) {
  31. return 0;
  32. }
  33. return length(s, s[x]) + 1;
  34. }
  35.  
  36. //NOTHING CAN BE CHANGED BELOW
  37. public static void testbed() {
  38. //CANNOT CHANGE BELOW
  39. int s[] = {5,1,0,4,2,3} ;
  40. int y = length_easy(s,3) ;
  41. System.out.println("length_easy y = " + y);
  42. u.myassert(y == 4) ;
  43. int b[] = {5,1,0,4,2,3} ;
  44. int x = length(s,3) ;
  45. System.out.println("length x = " + x);
  46. u.myassert(x == y) ;
  47. for (int i = 0; i < s.length; ++i) {
  48. u.myassert(s[i] == b[i]);
  49. }
  50. System.out.println("Assert passed");
  51. }
  52.  
  53. public static void testbed1() {
  54. //CANNOT CHANGE BELOW
  55. int s[] = {5,1,0,4,2,3} ;
  56. int b[] = {5,1,0,4,2,3} ;
  57. int l = s.length ;
  58. for (int j = 0; j < l ; ++j) {
  59. System.out.println("---------------------j = " + j + " ------------------");
  60. int y = length_easy(s,j) ;
  61. System.out.println("length_easy y = " + y);
  62. int x = length(s,j) ;
  63. System.out.println("length x = " + x);
  64. u.myassert(x == y) ;
  65. for (int i = 0; i < s.length; ++i) {
  66. u.myassert(s[i] == b[i]);
  67. }
  68. System.out.println("---------------------Assert passed--------------------");
  69. }
  70. }
  71.  
  72. public static void testbed2() {
  73. int n = 5000 ;
  74. System.out.println("Testing on " + n + " numbers");
  75. int[] a = u.generateNumberInIncreasingOrder(n, 1);
  76. //shuffle a
  77. Random r = new Random() ;
  78. for (int i = 0; i < n; ++i) {
  79. int p = RandomInt.getRandomInt(r,0,n); //This gives number between 0 to n-1
  80. int q = RandomInt.getRandomInt(r,0,n);
  81. u.swap(a,p,q);
  82. }
  83. //Keep a in b to compare
  84. int[] b = new int[n] ;
  85. for (int i = 0; i < n; ++i) {
  86. b[i] = a[i] ;
  87. }
  88. for (int j = 0 ; j < n; ++j) {
  89. int y = length_easy(a,j) ;
  90. int x = length(a,j) ;
  91. u.myassert(x == y) ;
  92. for (int i = 0; i < a.length; ++i) {
  93. u.myassert(a[i] == b[i]);
  94. }
  95. }
  96. System.out.println("All " + n + " cases passed. You are great");
  97. }
  98.  
  99.  
  100. public static void main(String[] args) {
  101. System.out.println("Length.java");
  102. testbed() ;
  103. testbed1() ;
  104. testbed2() ;
  105. System.out.println("Length.java DONE");
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment