U E D R , A S I H C RSS

Jolly Jumpers/신재동

JollyJumpers/신재동

JollyJumper.java

~cpp 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class JollyJumper {
	public boolean isJollyJumper(Vector aList) {
		int listSize = aList.size();
		for(int i = 0; i < listSize - 1; i++) {
			if(listSize - 1 < getGap(aList, i))
				return false;
		}
		return true;
	}
	
	public int getGap(Vector aList, int i) {
		int gap = ((Integer)aList.get(i)).intValue() - ((Integer)aList.get(i + 1)).intValue();
		return gap > 0 ?  gap : -1 * gap;
	}
	
	private void inputNumbers(Vector list) {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			
			String [] numbersStr = line.split(" ");
			for(int i = 0; i < numbersStr.length; i++) {
				list.add(new Integer(Integer.parseInt(numbersStr[i])));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		JollyJumper jollyJumper = new JollyJumper();
		
		Vector list = new Vector();
		jollyJumper.inputNumbers(list);
		
		if(jollyJumper.isJollyJumper(list))
			System.out.println("Jolly");
		else
			System.out.println("Not jolly");
	}
}

JollyJumperTest.java

~cpp 
import java.util.Vector;

import junit.framework.TestCase;

public class JollyJumperTest extends TestCase {
	public void testJollyJumper() {
		Vector vec = new Vector();
		vec.add(new Integer(1));
		
		JollyJumper jollyJumper = new JollyJumper();
		assertEquals(true, jollyJumper.isJollyJumper(vec));
	}
	
	public void testGetGap() {
		Vector vec = new Vector();
		vec.add(new Integer(1));
		vec.add(new Integer(4));
		vec.add(new Integer(10));
		
		JollyJumper jollyJumper = new JollyJumper();
		assertEquals(3, jollyJumper.getGap(vec, 0));
		assertEquals(6, jollyJumper.getGap(vec, 1));
	}
	
	public void testJollyJumperTwo() {
		Vector vec = new Vector();
		vec.add(new Integer(5));
		vec.add(new Integer(1));
		vec.add(new Integer(4));
		vec.add(new Integer(2));
		vec.add(new Integer(3));
		
		JollyJumper jollyJumper = new JollyJumper();
		assertEquals(true, jollyJumper.isJollyJumper(vec));
	}
	
	public void testJollyJumperThree() {
		Vector vec = new Vector();
		vec.add(new Integer(5));
		vec.add(new Integer(1));
		vec.add(new Integer(4));
		vec.add(new Integer(2));
		vec.add(new Integer(-1));
		vec.add(new Integer(6));
		
		JollyJumper jollyJumper = new JollyJumper();
		assertEquals(false, jollyJumper.isJollyJumper(vec));
	}
}

이번엔 TDD로... 쉬운 알고리즘이라 테스트를 몇 개 안만들고 끝냈다. --재동
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:34
Processing time 0.0110 sec