Created
February 29, 2012 06:21
-
-
Save benelog/1938477 to your computer and use it in GitHub Desktop.
RankFinder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.benelog.quiz; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class RankFinder<T extends Comparable<T>> { | |
private List<T> topValues; | |
private int rankToFind; | |
public RankFinder(int rankToFind) { | |
if (rankToFind == 0) { | |
rankToFind = 1; | |
} | |
this.rankToFind = rankToFind; | |
this.topValues = new ArrayList<T>(rankToFind); | |
} | |
private void insertTopValue(T num) { | |
for (int i = 0, n = topValues.size(); i < n; i++) { | |
if ((num.compareTo(topValues.get(i)) <= 0)) { | |
topValues.add(i, num); | |
return; | |
} | |
} | |
topValues.add(num); | |
} | |
public void addTargetValue(T num) { | |
if (topValues.isEmpty()) { | |
topValues.add(num); | |
return; | |
} | |
if ((topValues.size() == rankToFind) && (num.compareTo(topValues.get(0)) <= 0)) { | |
return; | |
} | |
insertTopValue(num); | |
if (topValues.size() > rankToFind) { | |
topValues.remove(0); | |
} | |
} | |
public T getRankedValue() { | |
return topValues.get(0); | |
} | |
public List<T> getTopValues() { | |
return topValues; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.benelog.quiz; | |
import static java.util.Arrays.*; | |
import static org.hamcrest.CoreMatchers.*; | |
import static org.junit.Assert.*; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import org.junit.Test; | |
public class RankFinderTest { | |
@Test | |
public void get10thRankOf1000() { | |
//given | |
List<Integer> rawData = new ArrayList<Integer>(); | |
for (int i = 0, n = 1000; i < n; i++) { | |
rawData.add(i); | |
} | |
Collections.shuffle(rawData); | |
int rankToFind = 10; | |
//when | |
RankFinder<Integer> rankFinder = new RankFinder<Integer>(rankToFind); | |
addAll(rawData, rankFinder); | |
Integer rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankedValue, is(990)); | |
assertThat(rankFinder.getTopValues(), | |
is(asList(990, 991, 992, 993, 994, 995, 996, 997, 998, 999))); | |
} | |
@Test | |
public void get10thRankOf2000() { | |
//given | |
List<Integer> rawData = new ArrayList<Integer>(); | |
for (int i = 0, n = 1000; i < n; i++) { | |
rawData.add(i); | |
rawData.add(i); | |
} | |
Collections.shuffle(rawData); | |
int rankToFind = 10; | |
//when | |
RankFinder<Integer> rankFinder = new RankFinder<Integer>(rankToFind); | |
addAll(rawData, rankFinder); | |
Integer rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankedValue, is(995)); | |
assertThat(rankFinder.getTopValues(), | |
is(asList(995, 995, 996, 996, 997, 997, 998, 998, 999, 999))); | |
} | |
@Test | |
public void get1stRankOf20() { | |
//given | |
List<Integer> rawData = new ArrayList<Integer>(); | |
for (int i = 0, n = 10; i < n; i++) { | |
rawData.add(i); | |
rawData.add(i); | |
} | |
Collections.shuffle(rawData); | |
int rankToFind = 1; | |
//when | |
RankFinder<Integer> rankFinder = new RankFinder<Integer>(rankToFind); | |
addAll(rawData, rankFinder); | |
Integer rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankedValue, is(9)); | |
assertThat(rankFinder.getTopValues(), is(asList(9))); | |
} | |
@Test | |
public void zeroRankIsSameMeaningTo1stRank() { | |
//given | |
List<Integer> rawData = asList(1); | |
int rankToFind = 0; | |
//when | |
RankFinder<Integer> rankFinder = new RankFinder<Integer>(rankToFind); | |
addAll(rawData, rankFinder); | |
Integer rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankFinder.getTopValues(), is(asList(1))); | |
assertThat(rankedValue, is(1)); | |
} | |
@Test | |
public void get3rdRankOf14() { | |
//given | |
List<Integer> rawData = asList(400, 9, 13, 25, 3, 200, 300, 1, 10, 2, 3, 4, 5, 3); | |
int rankToFind = 3; | |
//when | |
RankFinder<Integer> rankFinder = new RankFinder<Integer>(rankToFind); | |
addAll(rawData, rankFinder); | |
Integer rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankedValue, is(200)); | |
assertThat(rankFinder.getTopValues(), is(asList(200,300,400))); | |
} | |
public void get3rdRankOf5() { | |
//given | |
List<Integer> rawData = asList(400,400,100,500,200); | |
int rankToFind = 3; | |
//when | |
RankFinder<Integer> rankFinder = new RankFinder<Integer>(rankToFind); | |
addAll(rawData, rankFinder); | |
Integer rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankedValue, is(400)); | |
assertThat(rankFinder.getTopValues(), is(asList(400,400,500))); | |
} | |
@Test | |
public void get2ndRankOf5WithFloat() { | |
//given | |
List<Float> rawData = asList(10.9F, 10.2F, 10.3F, 10.4F, 10.5F); | |
int rankToFind = 2; | |
//when | |
RankFinder<Float> rankFinder = new RankFinder<Float>(rankToFind); | |
addAll(rawData, rankFinder); | |
Float rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankedValue, is(10.5F)); | |
assertThat(rankFinder.getTopValues(), is(asList(10.5F,10.9F))); | |
} | |
@Test | |
public void get4thOfWord() { | |
//given | |
List<String> rawData = asList("e","fc","a","b","k"); | |
int rankToFind = 2; | |
//when | |
RankFinder<String> rankFinder = new RankFinder<String>(rankToFind); | |
addAll(rawData, rankFinder); | |
String rankedValue = rankFinder.getRankedValue(); | |
//then | |
assertThat(rankedValue, is("fc")); | |
} | |
private <T> void addAll(List<T> rawData, RankFinder<? super T> rankFinder) { | |
for (T num : rawData) { | |
rankFinder.addTargetValue(num); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment