Skip to content

Instantly share code, notes, and snippets.

@hd42
Last active July 18, 2016 13:42
Show Gist options
  • Save hd42/b06a4f533a388563f1edbea70c898082 to your computer and use it in GitHub Desktop.
Save hd42/b06a4f533a388563f1edbea70c898082 to your computer and use it in GitHub Desktop.
package software.schwering.javacodechallenge;
import java.math.BigDecimal;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class StockMarket {
private final BigDecimal[] prices;
public StockMarket(List<BigDecimal> prices) {
this.prices = prices.toArray(new BigDecimal[prices.size()]);
}
public StockMarket(String s) {
this(Pattern.compile(" ").splitAsStream(s).map(BigDecimal::new).collect(Collectors.toList()));
}
public static void main(String... args) {
System.out.println(new StockMarket(args[0]).bestDeal());
}
public PriceRange bestDeal() {
return findPossibleDeals().last();
}
SortedSet<PriceRange> findPossibleDeals() {
TreeSet<PriceRange> priceRanges = new TreeSet<>();
for (int i = 0; i < prices.length - 2; i++) {
for (int j = i + 2; j < prices.length; j++) {
if (prices[j].compareTo(prices[i]) > 0) {
priceRanges.add(new PriceRange(prices[i], prices[j]));
}
}
}
return priceRanges;
}
public static class PriceRange implements Comparable<PriceRange> {
private final BigDecimal buyPrice;
private final BigDecimal sellPrice;
public PriceRange(BigDecimal buyPrice, BigDecimal sellPrice) {
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
}
public BigDecimal getGain() {
return sellPrice.subtract(buyPrice);
}
@Override
public int compareTo(PriceRange o) {
return getGain().compareTo(o.getGain());
}
@Override
public String toString() {
return buyPrice.toString() + ' ' + sellPrice.toString();
}
@Override
public int hashCode() {
return buyPrice.hashCode() * 31 + sellPrice.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof PriceRange)) {
return false;
}
PriceRange other = ((PriceRange) obj);
return buyPrice.equals(other.buyPrice) && sellPrice.equals(other.sellPrice);
}
}
}
package software.schwering.javacodechallenge;
import org.junit.Test;
import java.math.BigDecimal;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class StockMarketTest {
@Test
public void findPossibleDeals() {
assertThat(new StockMarket("1.0 2.0 0.5 3.0").findPossibleDeals(), hasItems(r("2.0", "3.0"), r("1.0", "3.0")));
}
private static StockMarket.PriceRange r(String buy, String sell) {
return new StockMarket.PriceRange(new BigDecimal(buy), new BigDecimal(sell));
}
@Test
public void findPossibleDeals_emptyIfNotEnoughPricesWithAtLeastOneTickDistance() {
assertThat(new StockMarket("1.0 2.0").findPossibleDeals(), hasItems());
}
@Test
public void findPossibleDeals_emptyIfOnlyBadDeals() {
assertThat(new StockMarket("5.0 4.0 3.0 2.0 1.0").findPossibleDeals(), hasItems());
}
@Test
public void bestDeal() {
StockMarket stockMarket = new StockMarket("9.20 8.03 10.02 8.08 8.14 8.10 8.31 8.28 8.35 8.34 8.39 8.45 8.38 8.38 8.32 8.36 8.28 8.28 8.38 8.48 8.49 8.54 8.73 8.72 8.76 8.74 8.87 8.82 8.81 8.82 8.85 8.85 8.86 8.63 8.70 8.68 8.72 8.77 8.69 8.65 8.70 8.98 8.98 8.87 8.71 9.17 9.34 9.28 8.98 9.02 9.16 9.15 9.07 9.14 9.13 9.10 9.16 9.06 9.10 9.15 9.11 8.72 8.86 8.83 8.70 8.69 8.73 8.73 8.67 8.70 8.69 8.81 8.82 8.83 8.91 8.80 8.97 8.86 8.81 8.87 8.82 8.78 8.82 8.77 8.54 8.32 8.33 8.32 8.51 8.53 8.52 8.41 8.55 8.31 8.38 8.34 8.34 8.19 8.17 8.16");
StockMarket.PriceRange bestDeal = stockMarket.bestDeal();
assertThat(bestDeal.toString(), is(equalTo("8.03 9.34")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment