TDD practice (2)

Practice topic: Decomposing prime factors

Step1:

unit test:

package com.bijian.study.factor.test;

import java.util.Arrays;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import com.bijian.study.factor.Factor;

public class FactorTest {

	private Factor factor;
	
	@Before
	public void setUp() {
		factor = new Factor();
	}
	
	@Test
	public void test_factor() {
		Assert.assertEquals(Arrays.asList(2), factor.getFactors(2));
		Assert.assertEquals(Arrays.asList(3), factor.getFactors(3));
	}
}

Write the code that the test passes:

package com.bijian.study.factor;

import java.util.ArrayList;
import java.util.List;

public class Factor {

	public List<Integer> getFactors(int i) {
		
		List<Integer> resList = new ArrayList<Integer>();
		resList.add(i);
		return resList;
	}
}

 

Step2:

Write another unit test case that fails:

Assert.assertEquals(Arrays.asList(2,2), factor.getFactors(4));

Modify the code to make the unit test case pass:

package com.bijian.study.factor;

import java.util.ArrayList;
import java.util.List;

public class Factor {

	public List<Integer> getFactors(int i) {
		
		List<Integer> resList = new ArrayList<Integer>();
		if(i % 2 == 0 && i != 2) {
			resList.add(2);
			i = i/2;
		}
		resList.add(i);
		return resList;
	}
}

 

Step3:

Then write the following unit test case and find that the last case fails to pass

Assert.assertEquals(Arrays.asList(5), factor.getFactors(5));
Assert.assertEquals(Arrays.asList(2,3), factor.getFactors(6));
Assert.assertEquals(Arrays.asList(7), factor.getFactors(7));
Assert.assertEquals(Arrays.asList(2,2,2), factor.getFactors(8));

Modify the code as follows:

package com.bijian.study.factor;

import java.util.ArrayList;
import java.util.List;

public class Factor {

	public List<Integer> getFactors(int i) {
		
		List<Integer> resList = new ArrayList<Integer>();
		while(i % 2 == 0 && i != 2) {
			resList.add(2);
			i = i/2;
		}
		resList.add(i);
		return resList;
	}
}

 

Step4:

Add the following unit test case

Assert.assertEquals(Arrays.asList(3,3), factor.getFactors(9));

Modify the code:

package com.bijian.study.factor;

import java.util.ArrayList;
import java.util.List;

public class Factor {

	public List<Integer> getFactors(int i) {
		
		List<Integer> resList = new ArrayList<Integer>();
		while(i % 2 == 0 && i != 2) {
			resList.add(2);
			i = i/2;
		}
		while(i % 3 == 0 && i != 3) {
			resList.add(3);
			i = i/3;
		}
		resList.add(i);
		return resList;
	}
}

 

Step5:

The unit test case is added as follows, and it is found that the execution of the last case fails:

Assert.assertEquals(Arrays.asList(2,5), factor.getFactors(10));
Assert.assertEquals(Arrays.asList(11), factor.getFactors(11));
Assert.assertEquals(Arrays.asList(2,2,3), factor.getFactors(12));
Assert.assertEquals(Arrays.asList(2,11,13), factor.getFactors(2*11*13));

Modify the code as follows:

package com.bijian.study.factor;

import java.util.ArrayList;
import java.util.List;

public class Factor {

	public List<Integer> getFactors(int i) {
		
		List<Integer> resList = new ArrayList<Integer>();
		for(int j=2;j<i;j++) {
			while(i % j == 0 && i != j) {
				resList.add(j);
				i = i/j;
			}
		}
		resList.add(i);
		return resList;
	}
}

 

Step6:

Feel free to add unit test cases like this:

Assert.assertEquals(Arrays.asList(3,7,13,23), factor.getFactors(3*7*13*23));

The test passed, but the code is not easy to understand. Let's refactor. The refactored code is as follows:

package com.bijian.study.factor;

import java.util.ArrayList;
import java.util.List;

public class Factor {

	public List<Integer> getFactors(int factor) {
		
		List<Integer> resList = new ArrayList<Integer>();
		for(int j=2;j<factor;j++) {
			while(factor % j == 0 && factor != j) {
				resList.add(j);
				factor = factor/j;
			}
		}
		resList.add(factor);
		return resList;
	}
}

        The above only uses a very simple refactoring method, rename.

        The process of these steps or exercises above is Kata. The idea of ​​Kata (way practice) is to practice coding through repeated practice, forming muscle memory in the process of constantly using various skills. Kata improves the workflow in many ways, such as writing tests, handling errors, and even the use of the editor, and is more familiar with language skills.

 

Another way to decompose the prime factors is attached:

public List<Integer> getFactors2(int factor) {
	List<Integer> resList = new ArrayList<Integer>();
	for(int j=2;j<=factor;j++) {
		if(factor%j == 0) {
			resList.add(j);
			factor = factor/j;
			j=1;
		}
	}
	return resList;
}
  Agile Coach: http://www.maiyuan.me/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326487615&siteId=291194637
TDD