fork(1) download
  1. package test;
  2.  
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5.  
  6.  
  7. public class TestHomework {
  8. public static interface Homework2{
  9. public String encode(String input);
  10. public String decode(String input);
  11. }
  12.  
  13. public static class Homework2ImpleA implements Homework2{
  14. int sep;
  15.  
  16. public Homework2ImpleA(int sep){
  17. this.sep=sep;
  18. }
  19.  
  20. char[][] buildContainer(int inputsize ){
  21.  
  22. char[][] rs= new char[this.sep][];
  23. int d = (inputsize / this.sep);
  24. int m = (inputsize % this.sep);
  25. for(int i=0,j=m+this.sep-1;i<this.sep;i++,j--){
  26. rs[i]=new char[d+Math.max((j/this.sep),0)];
  27.  
  28. }
  29. return rs;
  30. }
  31.  
  32. String outputContainer(char[][] container){
  33. StringBuilder rs = new StringBuilder();
  34. for(char[] co : container){
  35. rs.append(new String(co));
  36. }
  37. return rs.toString();
  38. }
  39. public String encode(String input){
  40. char [] o = input.toCharArray();
  41. char[][] container=buildContainer(o.length);
  42. for(int i=0,p=(i/this.sep);i<o.length;i+=this.sep,p=(i/this.sep)){
  43. for(int j=0,cp=i+j;j<this.sep && cp <o.length;j++,cp=i+j){
  44. if(p<container[j].length){
  45. container[j][p] = o[cp];
  46. }
  47. }
  48. }
  49. return outputContainer(container);
  50. }
  51. public String decode(String input){
  52. char [] o = input.toCharArray();
  53. char[][] container=buildContainer(o.length);
  54. for(int i=0,os=0;i<this.sep;i++){
  55.  
  56. System.arraycopy(o, os, container[i], 0, container[i].length);
  57. os+=container[i].length;
  58. }
  59.  
  60. for(int i=0;i<container.length;i++){
  61. for(int j=0 ;j<container[i].length;j++){
  62. o[(j*sep)+i]=container[i][j];
  63. }
  64. }
  65.  
  66. return new String(o);
  67. }
  68. }
  69.  
  70.  
  71. @Test
  72. public void doHomework(){
  73. Homework2 t = new Homework2ImpleA(2);
  74. for(String [] str : new String[][]{
  75. {"135246","123456"}
  76. ,{"acebd","abcde"}
  77. ,{"wrdol","world"}
  78. }){
  79. String test = str[0];
  80. String check = str[1];
  81. String decode = t.decode(test);
  82. System.out.printf("%s : %s == %s \n",test,check,decode);
  83. Assert.assertEquals(check, decode);
  84. }
  85. }
  86.  
  87. @Test
  88. public void doHomework2_2(){
  89. Homework2 t = new Homework2ImpleA(2);
  90. for(String [] str : new String[][]{
  91. {"123456","135246"}
  92. ,{"abcde","acebd"}
  93. ,{"world","wrdol"}
  94.  
  95. }){
  96. String test = str[0];
  97. String check = str[1];
  98. String encode = t.encode(test);
  99. String decode = t.decode(encode);
  100. System.out.printf("%s,%s,%s,%s\n",test,check,encode,decode);
  101. Assert.assertEquals(check, encode);
  102. Assert.assertEquals(test, decode);
  103. }
  104. }
  105.  
  106. @Test
  107. public void doHomework2_3(){
  108. Homework2 t = new Homework2ImpleA(3);
  109. for(String [] str : new String[][]{
  110. {"123456","142536"}
  111. ,{"abcde","adbec"}
  112. ,{"world","wlodr"}
  113. }){
  114. String test = str[0];
  115. String check = str[1];
  116. String encode = t.encode(test);
  117. String decode = t.decode(encode);
  118. System.out.printf("%s,%s,%s,%s\n",test,check,encode,decode);
  119. Assert.assertEquals(check, encode);
  120. Assert.assertEquals(test, decode);
  121. }
  122. }
  123.  
  124. @Test
  125. public void doHomework2_4(){
  126. Homework2 t = new Homework2ImpleA(4);
  127. for(String [] str : new String[][]{
  128. {"1234","1234"}
  129. ,{"12345","15234"}
  130. ,{"123456","152634"}
  131. ,{"1234567","1526374"}
  132. ,{"12345678","15263748"}
  133. ,{"123456789","159263748"}
  134. }){
  135. String test = str[0];
  136. String check = str[1];
  137. String encode = t.encode(test);
  138. String decode = t.decode(encode);
  139. System.out.printf("%s,%s,%s,%s\n",test,check,encode,decode);
  140. Assert.assertEquals(check, encode);
  141. Assert.assertEquals(test, decode);
  142. }
  143. }
  144.  
  145. }
  146.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Standard output is empty