package test;

import org.junit.Assert;
import org.junit.Test;

 
public class TestHomework {
	public static interface Homework2{		
		public String encode(String input);
		public String decode(String input);		
	}
	
	public static class Homework2ImpleA implements Homework2{
		int sep;			
		
		public Homework2ImpleA(int sep){
			this.sep=sep;			
		}
		
		char[][] buildContainer(int inputsize ){
			
			char[][] rs= new char[this.sep][];
			int d = (inputsize / this.sep);
			int m = (inputsize % this.sep);						 
			for(int i=0,j=m+this.sep-1;i<this.sep;i++,j--){
				rs[i]=new char[d+Math.max((j/this.sep),0)];
											
			} 						
			return rs;
		}
		
		String outputContainer(char[][] container){
			StringBuilder rs = new StringBuilder();
			for(char[] co : container){
				rs.append(new String(co));
			}
			return rs.toString();
		}								
		public String encode(String input){			
			char [] o = input.toCharArray();
			char[][] container=buildContainer(o.length);		
			for(int i=0,p=(i/this.sep);i<o.length;i+=this.sep,p=(i/this.sep)){
				for(int j=0,cp=i+j;j<this.sep && cp <o.length;j++,cp=i+j){
					if(p<container[j].length){
						container[j][p] = o[cp];
					}										
				}
			}
			return outputContainer(container);
		}
		public String decode(String input){			
			char [] o = input.toCharArray();
			char[][] container=buildContainer(o.length);						
			for(int i=0,os=0;i<this.sep;i++){

				System.arraycopy(o, os, container[i], 0, container[i].length);
				os+=container[i].length;
			}
			
			for(int i=0;i<container.length;i++){
				for(int j=0 ;j<container[i].length;j++){
					o[(j*sep)+i]=container[i][j];
				}
			}
			
			return new String(o);
		}			
	}
	
	
	@Test
	public void doHomework(){
		Homework2 t = new Homework2ImpleA(2);
		for(String [] str : new String[][]{
				{"135246","123456"}
				,{"acebd","abcde"}
				,{"wrdol","world"}
				}){
			String test = str[0];
			String check = str[1];	
			String decode = t.decode(test);
			System.out.printf("%s : %s ==  %s \n",test,check,decode);
			Assert.assertEquals(check, decode);			
		}			
	}

	@Test
	public void doHomework2_2(){
		Homework2 t = new Homework2ImpleA(2);
		for(String [] str : new String[][]{
				{"123456","135246"}
				,{"abcde","acebd"}
				,{"world","wrdol"}
				
				}){
			String test = str[0];
			String check = str[1];
			String encode = t.encode(test);
			String decode = t.decode(encode);
			System.out.printf("%s,%s,%s,%s\n",test,check,encode,decode);
			Assert.assertEquals(check, encode);		
			Assert.assertEquals(test, decode);	
		}		
	}	
	
	@Test
	public void doHomework2_3(){
		Homework2 t = new Homework2ImpleA(3);
		for(String [] str : new String[][]{
				{"123456","142536"}
				,{"abcde","adbec"}
				,{"world","wlodr"}				
				}){
			String test = str[0];
			String check = str[1];
			String encode = t.encode(test);
			String decode = t.decode(encode);
			System.out.printf("%s,%s,%s,%s\n",test,check,encode,decode);
			Assert.assertEquals(check, encode);		
			Assert.assertEquals(test, decode);	
		}		
	}	
	
	@Test
	public void doHomework2_4(){
		Homework2 t = new Homework2ImpleA(4);
		for(String [] str : new String[][]{
				{"1234","1234"}
				,{"12345","15234"}
				,{"123456","152634"}				
				,{"1234567","1526374"}
				,{"12345678","15263748"}
				,{"123456789","159263748"}
				}){
			String test = str[0];
			String check = str[1];
			String encode = t.encode(test);
			String decode = t.decode(encode);
			System.out.printf("%s,%s,%s,%s\n",test,check,encode,decode);
			Assert.assertEquals(check, encode);		
			Assert.assertEquals(test, decode);	
		}		
	}	
	
}
