U E D R , A S I H C RSS

Jolly Jumpers/임인택

  • straight forward 한 방법

~cpp 

public class JollyJumpers {
    private int array[];
    
    public void setArray(int[] arr) {
        array = arr;
    }
    
    public String testify() {
        int size = array.length;
        int count=0;
        boolean isExist[] = new boolean[size-1];
        
        for(int i=0; i<size-1; ++i) {
            int tmp = Math.abs(array[i]-array[i+1]);
            
            if( (tmp<size) && !isExist[tmp-1]) {
                ++count;
                isExist[tmp-1] = true;
            }
        }
        
        return (count == size-1)?"Jolly":"NotJolly";
    }
    
    public static void main(String args[]) {
        JollyJumpers jj = new JollyJumpers();
        
        int arr[] = new int[args.length];
        
        for(int i=0; i<arr.length; ++i) {
            arr[i] = Integer.parseInt(args[i]);
        }
        
        jj.setArray(arr);        
        System.out.println(jj.testify());
    }
}
  • tdd - 먼가 엉성하다. 기존의 코드를 단지 테스트하는 느낌. 수련이 더 필요하다.

~cpp 

package ByTDD;

import junit.framework.TestCase;

public class TestJJ extends TestCase  {
    private JollyJumpers2 jj;
    
    
    public void setUp() {    
        jj = new JollyJumpers2();
    }
    
    public void test배열넣기() {
        int arr1[] = {1,2,3,4};
        jj.setArray(arr1);
        assertEquals(true, jj.compareArray(arr1));
        
        int arr2[] = {1,2,3,4,5};
        assertEquals(false, jj.compareArray(arr2));
        
        int arr3[] = {1,1,2,3};
        assertEquals(false, jj.compareArray(arr3));
    }
    
    public void test임시플래그배열크기() {
        int arr1[] = {1,2,3};
        jj.setArray(arr1);
        
        assertEquals(2, jj.flags.length);
    }
    
    public void test임시플래그채우기() {
        int arr1[] = {1,2,3,4,5};
        jj.setArray(arr1);
        
        boolean barr1[] = {true, false, false, false};
        jj.getFlags();
        
        assertEquals(true, jj.compareArray(barr1));
        
        int arr2[] = {1,4,2,3};
        jj.setArray(arr2);
        
        boolean barr2[] = {true, true, true};
        jj.getFlags();
        
        assertEquals(true, jj.compareArray(barr2));
    }
    
    public void testJollyJumpers() {
        int arr1[] = {1,4,2,3};
        jj.setArray(arr1);
        assertEquals("Jolly", jj.testify());
        
        int arr2[] = {1,2,3,4,5};
        jj.setArray(arr2);
        assertEquals("NotJolly", jj.testify());
    }
}
~cpp 

package ByTDD;

public class JollyJumpers2 {
    public int array[];
    public boolean flags[];
    public int offCnt;

    public void setArray(int[] arr) {
        array = arr;
        flags = new boolean[arr.length-1];
        offCnt = 0;
    }
    
    public boolean compareArray(int[] arr) {
        if( arr.length != array.length )
            return false;
        
        for(int i=0; i<arr.length; ++i) {
            if( arr[i] != array[i] )
                return false;
        }
        
        return true;
    }
    
    public boolean compareArray(boolean[] arr) {
        if( arr.length != flags.length )
            return false;
        
        for(int i=0; i<arr.length; ++i) {
            if( arr[i] != flags[i] ) {
                return false;
            }
        }
        
        return true;
    }

    public void getFlags() {
        for(int i=0; i<array.length-1; ++i) {
            int tmp = Math.abs(array[i]-array[i+1]);
            
            if( tmp<array.length && !flags[tmp-1]) {
                flags[tmp-1] = true;
                ++offCnt;
            }
        }
    }

    public String testify() {
        getFlags();
        return (offCnt==array.length-1)?"Jolly":"NotJolly";
    }
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:34
Processing time 0.0158 sec