Skip to content

Commit bdbf824

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents d3a9ce0 + 458f5c1 commit bdbf824

15 files changed

Lines changed: 360 additions & 12 deletions

File tree

‎.Rhistory‎

Whitespace-only changes.

‎config.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"language": "Java",
44
"repository": "https://github.com/exercism/xjava",
55
"active": true,
6+
67
"exercises": [
78
{
89
"slug": "hello-world",
@@ -313,6 +314,16 @@
313314
"slug": "ocr-numbers",
314315
"difficulty": 1,
315316
"topics": []
317+
},
318+
{
319+
"slug": "isogram",
320+
"difficulty": 1,
321+
"topics": []
322+
},
323+
{
324+
"slug": "book-store",
325+
"difficulty": 1,
326+
"topics": []
316327
}
317328
],
318329
"deprecated": [
@@ -335,3 +346,4 @@
335346
"say"
336347
]
337348
}
349+

‎exercises/book-store/build.gradle‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}

‎exercises/book-store/src/example/java/.keep‎

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.awt.print.Printable;
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class Bookstore{
8+
9+
private static int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5;
10+
private List<Integer> books;
11+
private static double[] DISCOUNT_TIERS = {0,5,10,20,25};
12+
13+
public Bookstore (List<Integer> books){
14+
this.books = books;
15+
}
16+
17+
18+
19+
20+
public double calculateTotalCost(){
21+
return calculateTotalCost(this.books,0);
22+
}
23+
24+
private double calculateTotalCost (List<Integer> books,double priceSoFar ){
25+
double minPrice = Double.MAX_VALUE;
26+
27+
28+
if(books.size() == 0){
29+
return priceSoFar;
30+
}
31+
32+
List<Integer> groups = (ArrayList<Integer>) books.stream().distinct().collect(Collectors.toList());
33+
34+
35+
double price = 0;
36+
37+
for(int i = 0;i<groups.size();i++){
38+
books.remove(groups.get(i));
39+
}
40+
41+
try {
42+
price = calculateTotalCost(books,priceSoFar + costPerGroup(groups.size()));
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
}
46+
47+
minPrice = Math.min(minPrice, price);
48+
return minPrice;
49+
50+
51+
}
52+
53+
private double costPerGroup(int groupSize) throws Exception{
54+
if (groupSize < 1 || groupSize > MAX_GROUP_SIZE){
55+
throw new Exception("Invalid group size : " + groupSize );
56+
}
57+
return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize-1])/100;
58+
}
59+
60+
61+
}

‎exercises/book-store/src/main/java/.keep‎

Whitespace-only changes.

‎exercises/book-store/src/test/java/.keep‎

Whitespace-only changes.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import static org.junit.Assert.assertEquals;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
7+
import org.junit.Test;
8+
9+
public class BookstoreTest{
10+
11+
@Test
12+
public void onlyASingleBook(){
13+
List<Integer> books = new ArrayList<>(Arrays.asList(1));
14+
Bookstore bookstore = new Bookstore(books);
15+
assertEquals(8,bookstore.calculateTotalCost(),2);
16+
}
17+
18+
@Ignore
19+
@Test
20+
public void twoOfSameBook(){
21+
List<Integer> books = new ArrayList<>(Arrays.asList(1,1));
22+
Bookstore bookstore = new Bookstore(books);
23+
assertEquals(16,bookstore.calculateTotalCost(),2);
24+
}
25+
26+
@Ignore
27+
@Test
28+
public void emptyBasket(){
29+
List<Integer> books = new ArrayList<>();
30+
Bookstore bookstore = new Bookstore(books);
31+
assertEquals(0,bookstore.calculateTotalCost(),2);
32+
}
33+
34+
@Ignore
35+
@Test
36+
public void twoDifferentBooks(){
37+
List<Integer> books = new ArrayList<>(Arrays.asList(1,2));
38+
Bookstore bookstore = new Bookstore(books);
39+
assertEquals(15.20,bookstore.calculateTotalCost(),2);
40+
}
41+
42+
@Ignore
43+
@Test
44+
public void threeDifferentBooks(){
45+
List<Integer> books = new ArrayList<>(Arrays.asList(1,2,3));
46+
Bookstore bookstore = new Bookstore(books);
47+
assertEquals(21.6,bookstore.calculateTotalCost(),2);
48+
}
49+
50+
@Ignore
51+
@Test
52+
public void fourDifferentBooks(){
53+
ArrayList<Integer> books = new ArrayList<>(Arrays.asList(1,2,3,4));
54+
Bookstore bookstore = new Bookstore(books);
55+
assertEquals(25.6,bookstore.calculateTotalCost(),2);
56+
57+
}
58+
59+
@Ignore
60+
@Test
61+
public void fiveDifferentBooks(){
62+
ArrayList<Integer> books = new ArrayList<>(Arrays.asList(1,2,3,4,5));
63+
Bookstore bookstore = new Bookstore(books);
64+
assertEquals(30,bookstore.calculateTotalCost(),2);
65+
}
66+
67+
@Ignore
68+
@Test
69+
public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){
70+
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,5));
71+
Bookstore bookstore = new Bookstore(books);
72+
assertEquals(51.20,bookstore.calculateTotalCost(),2);
73+
}
74+
75+
@Ignore
76+
@Test
77+
public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){
78+
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,4));
79+
Bookstore bookstore = new Bookstore(books);
80+
assertEquals(40.8,bookstore.calculateTotalCost(),2);
81+
}
82+
83+
@Ignore
84+
@Test
85+
public void twoEachOfFirst4BooksAnd1CopyEachOfRest(){
86+
Integer[] p = {1,1,2,2,3,3,4,4,5};
87+
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5));
88+
Bookstore bookstore = new Bookstore(books);
89+
assertEquals(55.60,bookstore.calculateTotalCost(),2);
90+
}
91+
92+
@Ignore
93+
@Test
94+
public void twoCopiesOfEachBook(){
95+
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5));
96+
Bookstore bookstore = new Bookstore(books);
97+
assertEquals(60.00,bookstore.calculateTotalCost(),2);
98+
}
99+
100+
@Ignore
101+
@Test
102+
public void threeCopiesOfFirstBookAnd2EachOfRemaining(){
103+
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5,1));
104+
Bookstore bookstore = new Bookstore(books);
105+
assertEquals(68.00,bookstore.calculateTotalCost(),2);
106+
}
107+
108+
@Ignore
109+
@Test
110+
public void threeEachOFirst2BooksAnd2EachOfRemainingBooks(){
111+
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5,1,2));
112+
Bookstore bookstore = new Bookstore(books);
113+
assertEquals(75.20,bookstore.calculateTotalCost(),2);
114+
}
115+
116+
}
117+

‎exercises/hamming/src/example/java/Hamming.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
public class Hamming {
2+
private final int hammingDistance;
23

3-
public static int compute(String leftStrand, String rightStrand) {
4+
public Hamming(String leftStrand, String rightStrand) {
5+
hammingDistance = computeHammingDistance(leftStrand, rightStrand);
6+
}
7+
8+
public int getHammingDistance() {
9+
return hammingDistance;
10+
}
11+
12+
private int computeHammingDistance(String leftStrand, String rightStrand) {
413
if(leftStrand.length() != rightStrand.length()) {
514
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");
615
}
@@ -13,7 +22,7 @@ public static int compute(String leftStrand, String rightStrand) {
1322
return distance;
1423
}
1524

16-
private static int hammingContributionAt(int index, String leftStrand, String rightStrand) {
25+
private int hammingContributionAt(int index, String leftStrand, String rightStrand) {
1726
return leftStrand.charAt(index) != rightStrand.charAt(index) ? 1 : 0;
1827
}
1928

‎exercises/hamming/src/test/java/HammingTest.java‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,63 @@
77
import org.junit.rules.ExpectedException;
88

99
public class HammingTest {
10-
1110

1211
@Rule
1312
public ExpectedException thrown = ExpectedException.none();
1413

1514
@Test
1615
public void testNoDifferenceBetweenIdenticalStrands() {
17-
assertThat(Hamming.compute("A", "A"), is(0));
16+
assertThat(new Hamming("A", "A").getHammingDistance(), is(0));
1817
}
1918

2019
@Ignore
2120
@Test
2221
public void testHammingDistanceForSingleNucleotideStrand() {
23-
assertThat(Hamming.compute("A", "G"), is(1));
22+
assertThat(new Hamming("A", "G").getHammingDistance(), is(1));
2423
}
2524

2625
@Ignore
2726
@Test
2827
public void testHammingDistanceForSmallStrand() {
29-
assertThat(Hamming.compute("AG", "CT"), is(2));
28+
assertThat(new Hamming("AG", "CT").getHammingDistance(), is(2));
3029
}
3130

3231
@Ignore
3332
@Test
3433
public void testSmallHammingDistance() {
35-
assertThat(Hamming.compute("AT", "CT"), is(1));
34+
assertThat(new Hamming("AT", "CT").getHammingDistance(), is(1));
3635
}
3736

3837
@Ignore
3938
@Test
4039
public void testSmallHammingDistanceInLongerStrand() {
41-
assertThat(Hamming.compute("GGACG", "GGTCG"), is(1));
40+
assertThat(new Hamming("GGACG", "GGTCG").getHammingDistance(), is(1));
4241
}
4342

4443
@Ignore
4544
@Test
4645
public void testValidatesFirstStrandNotLonger() {
4746
thrown.expect(IllegalArgumentException.class);
48-
Hamming.compute("AAAG", "AAA");
47+
new Hamming("AAAG", "AAA");
4948
}
5049

5150
@Ignore
5251
@Test
5352
public void testValidatesOtherStrandNotLonger() {
5453
thrown.expect(IllegalArgumentException.class);
55-
Hamming.compute("AAA", "AAAG");
54+
new Hamming("AAA", "AAAG");
5655
}
5756

5857
@Ignore
5958
@Test
6059
public void testLargeHammingDistance() {
61-
assertThat(Hamming.compute("GATACA", "GCATAA"), is(4));
60+
assertThat(new Hamming("GATACA", "GCATAA").getHammingDistance(), is(4));
6261
}
6362

6463
@Ignore
6564
@Test
6665
public void testHammingDistanceInVeryLongStrand() {
67-
assertThat(Hamming.compute("GGACGGATTCTG", "AGGACGGATTCT"), is(9));
66+
assertThat(new Hamming("GGACGGATTCTG", "AGGACGGATTCT").getHammingDistance(), is(9));
6867
}
6968

7069
}

0 commit comments

Comments
 (0)