Dmitry Noranovich 9 лет назад
Родитель
Сommit
1f24f33e03

+ 15
- 0
config.json Просмотреть файл

@@ -39,6 +39,7 @@
39 39
     "luhn",
40 40
     "pig-latin",
41 41
     "simple-linked-list",
42
+    "sum-of-multiples",
42 43
     "linked-list",
43 44
     "nth-prime",
44 45
     "pascals-triangle",
@@ -47,7 +48,11 @@
47 48
     "largest-series-product",
48 49
     "queen-attack",
49 50
     "minesweeper",
51
+<<<<<<< HEAD
50 52
     "binary-search"
53
+=======
54
+    "series"
55
+>>>>>>> upsetream/master
51 56
   ],
52 57
   "exercises": [
53 58
     {
@@ -226,6 +231,11 @@
226 231
       "topics": []
227 232
     },
228 233
     {
234
+      "slug": "sum-of-multiples",
235
+      "difficulty": 1,
236
+      "topics": []
237
+    },
238
+    {
229 239
       "slug": "linked-list",
230 240
       "difficulty": 1,
231 241
       "topics": []
@@ -266,6 +276,11 @@
266 276
       "topics": []
267 277
     },
268 278
     {
279
+      "slug": "series",
280
+      "difficulty": 1,
281
+      "topics": []
282
+    },
283
+    {
269 284
       "slug": "binary-search",
270 285
       "difficulty": 1,
271 286
       "topics": []

+ 70
- 0
exercises/binary-search/src/example/java/BinarySearch.java Просмотреть файл

@@ -0,0 +1,70 @@
1
+
2
+import java.util.List;
3
+
4
+public class BinarySearch<T extends Comparable<T>> {
5
+
6
+    public static String ARRAY_MUST_BE_SORTED = "Array should be sorted.";
7
+
8
+    private List<T> array;
9
+    private int arraySize;
10
+
11
+    public BinarySearch(List<T> array) {
12
+        if (!isSorted(array)) {
13
+            throw new IllegalArgumentException(ARRAY_MUST_BE_SORTED);
14
+        }
15
+        this.array = array;
16
+        this.arraySize = array.size();
17
+    }
18
+
19
+    public int indexOf(T value) {
20
+        return search(value);
21
+    }
22
+
23
+    public List<T> getArray() {
24
+        return array;
25
+    }
26
+
27
+    private boolean isSorted(List<T> list) {
28
+        T previous, next;
29
+        int listSize;
30
+
31
+        if (list == null || list.isEmpty()) {
32
+            return false;
33
+        }
34
+
35
+        listSize = list.size();
36
+        if (listSize == 1) {
37
+            return true;
38
+        }
39
+
40
+        previous = list.get(0);
41
+        for (int i = 0, n = listSize - 1; i < n; i++) {
42
+            next = list.get(i + 1);
43
+            if (previous.compareTo(next) > 0) {
44
+                return false;
45
+            }
46
+            previous = next;
47
+        }
48
+        return true;
49
+    }
50
+
51
+    private int search(T value) {
52
+        int left = 0;
53
+        int right = this.arraySize - 1;
54
+        int middle;
55
+        T element;
56
+        while (left <= right) {
57
+            middle = (int) Math.floor(0.5 * (left + right));
58
+            element = this.array.get(middle);
59
+            if (value.compareTo(element) > 0) {
60
+                left = middle + 1;
61
+            } else if (value.compareTo(element) < 0) {
62
+                right = middle - 1;
63
+            } else {
64
+                return middle;
65
+            }
66
+        }
67
+        return -1;
68
+    }
69
+}
70
+

+ 4
- 0
exercises/binary-search/src/main/java/BinarySearch.java Просмотреть файл

@@ -0,0 +1,4 @@
1
+
2
+public class BinarySearch {
3
+
4
+}

+ 100
- 0
exercises/binary-search/src/test/java/BinarySearchTest.java Просмотреть файл

@@ -0,0 +1,100 @@
1
+
2
+import java.util.Arrays;
3
+import java.util.Collections;
4
+import java.util.List;
5
+import static org.junit.Assert.assertEquals;
6
+import static org.junit.Assert.assertFalse;
7
+import static org.junit.Assert.assertNotNull;
8
+import org.junit.Ignore;
9
+import org.junit.Rule;
10
+import org.junit.Test;
11
+import org.junit.rules.ExpectedException;
12
+
13
+public class BinarySearchTest {
14
+
15
+    private static final List<Integer> SORTED_LIST
16
+            = Collections.unmodifiableList(
17
+                    Arrays.asList(1, 2, 3, 4, 5, 6)
18
+            );
19
+
20
+    public static final List<Integer> SORTED_LIST_OF_ODD_LENGTH
21
+            = Collections.unmodifiableList(
22
+                    Arrays.asList(0, 1, 2, 2, 3, 10, 12)
23
+            );
24
+
25
+    public static final List<Integer> UNSORTED_LIST
26
+            = Collections.unmodifiableList(
27
+                    Arrays.asList(10, 2, 5, 1)
28
+            );
29
+
30
+    @Rule
31
+    public ExpectedException thrown = ExpectedException.none();
32
+
33
+    @Test
34
+    public void shouldRequireASortedListOK() {
35
+        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
36
+        List<Integer> actual = sut.getArray();
37
+        assertNotNull(actual);
38
+        assertFalse(actual.isEmpty());
39
+        assertEquals(actual.size(), SORTED_LIST.size());
40
+        assertEquals(actual, SORTED_LIST);
41
+    }
42
+
43
+    @Ignore
44
+    @Test
45
+    public void shouldRequireASortedListButNotSorted() {
46
+        thrown.expect(IllegalArgumentException.class);
47
+        thrown.expectMessage(BinarySearch.ARRAY_MUST_BE_SORTED);
48
+        new BinarySearch<Integer>(UNSORTED_LIST);
49
+    }
50
+
51
+    @Ignore
52
+    @Test
53
+    public void shouldFindTheCorrectIndexInTheMiddleOfArray() {
54
+        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
55
+        final int number = 3;
56
+        final int actual = sut.indexOf(number);
57
+        final int expected = 2;
58
+        assertEquals(expected, actual);
59
+    }
60
+
61
+    @Ignore
62
+    @Test
63
+    public void shouldFindTheCorrectIndexAtTheBeginningOfArray() {
64
+        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
65
+        final int number = 1;
66
+        final int actual = sut.indexOf(number);
67
+        final int expected = 0;
68
+        assertEquals(expected, actual);
69
+    }
70
+
71
+    @Ignore
72
+    @Test
73
+    public void shouldFindTheCorrectIndexAtTheEndOfArray() {
74
+        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
75
+        final int number = 6;
76
+        final int actual = sut.indexOf(number);
77
+        final int expected = 5;
78
+        assertEquals(expected, actual);
79
+    }
80
+
81
+    @Ignore
82
+    @Test
83
+    public void shouldFindTheCorrectIndexInTheMiddleOfArrayOfOddLength() {
84
+        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST_OF_ODD_LENGTH);
85
+        final int number = 2;
86
+        final int actual = sut.indexOf(number);
87
+        final int expected = 3;
88
+        assertEquals(expected, actual);
89
+    }
90
+
91
+    @Ignore
92
+    @Test
93
+    public void shouldReturnMinusOneIfNotFound() {
94
+        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
95
+        final int number = 10;
96
+        final int actual = sut.indexOf(number);
97
+        final int expected = -1;
98
+        assertEquals(expected, actual);
99
+    }
100
+}

+ 3
- 3
exercises/etl/src/main/java/Etl.java Просмотреть файл

@@ -2,7 +2,7 @@ import java.util.List;
2 2
 import java.util.Map;
3 3
 
4 4
 public class Etl {
5
-   public Map<String, Integer> transform(Map<Integer, List<String>> old) {
6
-      return null;
7
-   }
5
+    public Map<String, Integer> transform(Map<Integer, List<String>> old) {
6
+        return null;
7
+    }
8 8
 }

+ 1
- 1
exercises/hello-world/GETTING_STARTED.md Просмотреть файл

@@ -20,7 +20,7 @@ $ gradle test
20 20
 
21 21
 ## Iterate through the tests
22 22
 
23
-After your first test passes, remove the `@Ignore` from the next test, and ierate on your solution,
23
+After your first test passes, remove the `@Ignore` from the next test, and iterate on your solution,
24 24
 testing after each change.
25 25
 
26 26
 ## All the tests pass?  Submit your solution!

+ 3
- 3
exercises/hello-world/src/main/java/HelloWorld.java Просмотреть файл

@@ -1,5 +1,5 @@
1 1
 public class HelloWorld {
2
-	public static String hello(String name) {
3
-      return null;
4
-	}
2
+    public static String hello(String name) {
3
+        return null;
4
+    }
5 5
 }

+ 67
- 67
exercises/nucleotide-count/src/test/java/NucleotideTest.java Просмотреть файл

@@ -7,87 +7,87 @@ import org.junit.Ignore;
7 7
 public class NucleotideTest {
8 8
 
9 9
     @Test
10
-  public void testEmptyDnaStringHasNoAdenosine() {
11
-    DNA dna = new DNA("");
12
-    assertThat(dna.count('A')).isEqualTo(0);
13
-  }
10
+    public void testEmptyDnaStringHasNoAdenosine() {
11
+        DNA dna = new DNA("");
12
+        assertThat(dna.count('A')).isEqualTo(0);
13
+    }
14 14
 
15
-  @Ignore
15
+    @Ignore
16 16
     @Test
17
-  public void testEmptyDnaStringHasNoNucleotides() {
18
-    DNA dna = new DNA("");
19
-    assertThat(dna.nucleotideCounts()).hasSize(4).contains(
20
-        entry('A', 0),
21
-        entry('C', 0),
22
-        entry('G', 0),
23
-        entry('T', 0)
24
-    );
25
-  }
17
+    public void testEmptyDnaStringHasNoNucleotides() {
18
+        DNA dna = new DNA("");
19
+        assertThat(dna.nucleotideCounts()).hasSize(4).contains(
20
+            entry('A', 0),
21
+            entry('C', 0),
22
+            entry('G', 0),
23
+            entry('T', 0)
24
+        );
25
+    }
26 26
 
27
-  @Ignore
27
+    @Ignore
28 28
     @Test
29
-  public void testRepetitiveCytidineGetsCounted() {
30
-    DNA dna = new DNA("CCCCC");
31
-    assertThat(dna.count('C')).isEqualTo(5);
32
-  }
29
+    public void testRepetitiveCytidineGetsCounted() {
30
+        DNA dna = new DNA("CCCCC");
31
+        assertThat(dna.count('C')).isEqualTo(5);
32
+    }
33 33
 
34
-  @Ignore
34
+    @Ignore
35 35
     @Test
36
-  public void testRepetitiveSequenceWithOnlyGuanosine() {
37
-    DNA dna = new DNA("GGGGGGGG");
38
-    assertThat(dna.nucleotideCounts()).hasSize(4).contains(
39
-        entry('A', 0),
40
-        entry('C', 0),
41
-        entry('G', 8),
42
-        entry('T', 0)
43
-    );
44
-  }
36
+    public void testRepetitiveSequenceWithOnlyGuanosine() {
37
+        DNA dna = new DNA("GGGGGGGG");
38
+        assertThat(dna.nucleotideCounts()).hasSize(4).contains(
39
+            entry('A', 0),
40
+            entry('C', 0),
41
+            entry('G', 8),
42
+            entry('T', 0)
43
+        );
44
+    }
45 45
 
46
-  @Ignore
46
+    @Ignore
47 47
     @Test
48
-  public void testCountsOnlyThymidine() {
49
-    DNA dna = new DNA("GGGGGTAACCCGG");
50
-    assertThat(dna.count('T')).isEqualTo(1);
51
-  }
48
+    public void testCountsOnlyThymidine() {
49
+        DNA dna = new DNA("GGGGGTAACCCGG");
50
+        assertThat(dna.count('T')).isEqualTo(1);
51
+    }
52 52
 
53
-  @Ignore
53
+    @Ignore
54 54
     @Test
55
-  public void testCountsANucleotideOnlyOnce() {
56
-    DNA dna = new DNA("CGATTGGG");
57
-    dna.count('T');
58
-    assertThat(dna.count('T')).isEqualTo(2);
59
-  }
55
+    public void testCountsANucleotideOnlyOnce() {
56
+        DNA dna = new DNA("CGATTGGG");
57
+        dna.count('T');
58
+        assertThat(dna.count('T')).isEqualTo(2);
59
+    }
60 60
 
61
-  @Ignore
61
+    @Ignore
62 62
     @Test
63
-  public void testDnaCountsDoNotChangeAfterCountingAdenosine() {
64
-    DNA dna = new DNA("GATTACA");
65
-    dna.count('A');
66
-    assertThat(dna.nucleotideCounts()).hasSize(4).contains(
67
-        entry('A', 3),
68
-        entry('C', 1),
69
-        entry('G', 1),
70
-        entry('T', 2)
71
-    );
72
-  }
63
+    public void testDnaCountsDoNotChangeAfterCountingAdenosine() {
64
+        DNA dna = new DNA("GATTACA");
65
+        dna.count('A');
66
+        assertThat(dna.nucleotideCounts()).hasSize(4).contains(
67
+            entry('A', 3),
68
+            entry('C', 1),
69
+            entry('G', 1),
70
+            entry('T', 2)
71
+        );
72
+    }
73 73
 
74
-  @Ignore
74
+    @Ignore
75 75
     @Test(expected = IllegalArgumentException.class)
76
-  public void testValidatesNucleotides() {
77
-    DNA dna = new DNA("GACT");
78
-    dna.count('X');
79
-  }
76
+    public void testValidatesNucleotides() {
77
+        DNA dna = new DNA("GACT");
78
+        dna.count('X');
79
+    }
80 80
 
81
-  @Ignore
81
+    @Ignore
82 82
     @Test
83
-  public void testCountsAllNucleotides() {
84
-    String s = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
85
-    DNA dna = new DNA(s);
86
-    assertThat(dna.nucleotideCounts()).hasSize(4).contains(
87
-        entry('A', 20),
88
-        entry('C', 12),
89
-        entry('G', 17),
90
-        entry('T', 21)
91
-    );
92
-  }
83
+    public void testCountsAllNucleotides() {
84
+        String s = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
85
+        DNA dna = new DNA(s);
86
+        assertThat(dna.nucleotideCounts()).hasSize(4).contains(
87
+            entry('A', 20),
88
+            entry('C', 12),
89
+            entry('G', 17),
90
+            entry('T', 21)
91
+        );
92
+    }
93 93
 }

+ 18
- 0
exercises/series/build.gradle Просмотреть файл

@@ -0,0 +1,18 @@
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
+  testCompile "org.assertj:assertj-core:3.2.0"
12
+}
13
+test {
14
+  testLogging {
15
+    exceptionFormat = 'full'
16
+    events = ["passed", "failed", "skipped"]
17
+  }
18
+}

+ 39
- 0
exercises/series/src/example/java/Series.java Просмотреть файл

@@ -0,0 +1,39 @@
1
+
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 Series {
8
+
9
+    private final int digitsSize;
10
+    private List<Integer> digits;
11
+
12
+    public Series(String string) {
13
+        this.digits = Arrays
14
+                .asList(string.split(("")))
15
+                .stream()
16
+                .map(digit -> Integer.parseInt(digit))
17
+                .collect(Collectors.toList());
18
+        this.digitsSize = this.digits.size();
19
+    }
20
+
21
+    public List<List<Integer>> slices(int num) {
22
+        if (num > this.digitsSize) {
23
+            throw new IllegalArgumentException("Slice size is too big.");
24
+        }
25
+        final int limit = this.digitsSize - num + 1;
26
+        List<List<Integer>> result = new ArrayList<>(limit);
27
+        List<Integer> tmp;
28
+        for (int i = 0; i < limit; i++) {
29
+            tmp = this.digits.subList(i, i + num);
30
+            result.add(tmp);
31
+        }
32
+        return result;
33
+    }
34
+
35
+    public List<Integer> getDigits() {
36
+        return digits;
37
+    }
38
+
39
+}

+ 0
- 0
exercises/series/src/main/java/.keep Просмотреть файл


+ 3
- 0
exercises/series/src/main/java/Series.java Просмотреть файл

@@ -0,0 +1,3 @@
1
+public class Series {
2
+    
3
+}

+ 0
- 0
exercises/series/src/test/java/.keep Просмотреть файл


+ 154
- 0
exercises/series/src/test/java/SeriesTest.java Просмотреть файл

@@ -0,0 +1,154 @@
1
+
2
+import java.util.Arrays;
3
+import java.util.List;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.assertEquals;
7
+import static org.junit.Assert.assertFalse;
8
+import static org.junit.Assert.assertNotNull;
9
+import org.junit.Ignore;
10
+
11
+public class SeriesTest {
12
+
13
+    @Test
14
+    public void hasDigitsShort() {
15
+        Series sut = new Series("01234");
16
+        List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4);
17
+        List<Integer> actual = sut.getDigits();
18
+        assertNotNull(actual);
19
+        assertFalse(actual.isEmpty());
20
+        assertEquals(expected, actual);
21
+    }
22
+
23
+    @Test
24
+    @Ignore
25
+    public void hasDigitsLong() {
26
+        Series sut = new Series("0123456789");
27
+        List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
28
+        List<Integer> actual = sut.getDigits();
29
+        assertNotNull(actual);
30
+        assertFalse(actual.isEmpty());
31
+        assertEquals(expected, actual);
32
+    }
33
+
34
+    @Test
35
+    @Ignore
36
+    public void keepsTheDigitOrderIfReversed() {
37
+        Series sut = new Series("9876543210");
38
+        List<Integer> expected = Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
39
+        List<Integer> actual = sut.getDigits();
40
+        assertNotNull(actual);
41
+        assertFalse(actual.isEmpty());
42
+        assertEquals(expected, actual);
43
+    }
44
+
45
+    @Test
46
+    @Ignore
47
+    public void keepsArbitraryDigitOrder() {
48
+        Series sut = new Series("936923468");
49
+        List<Integer> expected = Arrays.asList(9, 3, 6, 9, 2, 3, 4, 6, 8);
50
+        List<Integer> actual = sut.getDigits();
51
+        assertNotNull(actual);
52
+        assertFalse(actual.isEmpty());
53
+        assertEquals(expected, actual);
54
+    }
55
+
56
+    @Test
57
+    @Ignore
58
+    public void canSliceByOne() {
59
+        Series sut = new Series("01234");
60
+        List<List<Integer>> expected = Arrays.asList(
61
+                Arrays.asList(0),
62
+                Arrays.asList(1),
63
+                Arrays.asList(2),
64
+                Arrays.asList(3),
65
+                Arrays.asList(4)
66
+        );
67
+        List<List<Integer>> actual = sut.slices(1);
68
+        assertNotNull(actual);
69
+        assertFalse(actual.isEmpty());
70
+        assertEquals(expected, actual);
71
+    }
72
+
73
+    @Test
74
+    @Ignore
75
+    public void canSliceByTwo() {
76
+        Series sut = new Series("98273463");
77
+        List<List<Integer>> expected = Arrays.asList(
78
+                Arrays.asList(9, 8),
79
+                Arrays.asList(8, 2),
80
+                Arrays.asList(2, 7),
81
+                Arrays.asList(7, 3),
82
+                Arrays.asList(3, 4),
83
+                Arrays.asList(4, 6),
84
+                Arrays.asList(6, 3)
85
+        );
86
+        List<List<Integer>> actual = sut.slices(2);
87
+        assertNotNull(actual);
88
+        assertFalse(actual.isEmpty());
89
+        assertEquals(expected, actual);
90
+    }
91
+
92
+    @Test
93
+    @Ignore
94
+    public void canSliceByThree() {
95
+        Series sut = new Series("01234");
96
+        List<List<Integer>> expected = Arrays.asList(
97
+                Arrays.asList(0, 1, 2),
98
+                Arrays.asList(1, 2, 3),
99
+                Arrays.asList(2, 3, 4)
100
+        );
101
+        List<List<Integer>> actual = sut.slices(3);
102
+        assertNotNull(actual);
103
+        assertFalse(actual.isEmpty());
104
+        assertEquals(expected, actual);
105
+    }
106
+
107
+    @Test
108
+    @Ignore
109
+    public void canSliceByThreeWithDuplicateDigits() {
110
+        Series sut = new Series("31001");
111
+        List<List<Integer>> expected = Arrays.asList(
112
+                Arrays.asList(3, 1, 0),
113
+                Arrays.asList(1, 0, 0),
114
+                Arrays.asList(0, 0, 1)
115
+        );
116
+        List<List<Integer>> actual = sut.slices(3);
117
+        assertNotNull(actual);
118
+        assertFalse(actual.isEmpty());
119
+        assertEquals(expected, actual);
120
+    }
121
+
122
+    @Test
123
+    @Ignore
124
+    public void canSliceByFour() {
125
+        Series sut = new Series("91274");
126
+        List<List<Integer>> expected = Arrays.asList(
127
+                Arrays.asList(9, 1, 2, 7),
128
+                Arrays.asList(1, 2, 7, 4)
129
+        );
130
+        List<List<Integer>> actual = sut.slices(4);
131
+        assertNotNull(actual);
132
+        assertFalse(actual.isEmpty());
133
+        assertEquals(expected, actual);
134
+    }
135
+
136
+    @Test
137
+    @Ignore
138
+    public void canSliceByFive() {
139
+        Series sut = new Series("81228");
140
+        List<List<Integer>> expected = Arrays.asList(
141
+                Arrays.asList(8, 1, 2, 2, 8)
142
+        );
143
+        List<List<Integer>> actual = sut.slices(5);
144
+        assertNotNull(actual);
145
+        assertFalse(actual.isEmpty());
146
+        assertEquals(expected, actual);
147
+    }
148
+
149
+    @Test(expected = IllegalArgumentException.class)
150
+    @Ignore
151
+    public void throwsAnErrorIfNotEnoughDigitsToSlice() {
152
+        new Series("01032987583").slices(12);
153
+    }
154
+}

+ 2
- 0
exercises/settings.gradle Просмотреть файл

@@ -34,9 +34,11 @@ include 'robot-name'
34 34
 include 'roman-numerals'
35 35
 include 'prime-factors'
36 36
 include 'scrabble-score'
37
+include 'series'
37 38
 include 'sieve'
38 39
 include 'simple-cipher'
39 40
 include 'simple-linked-list'
41
+include 'sum-of-multiples'
40 42
 include 'space-age'
41 43
 include 'strain'
42 44
 include 'triangle'

+ 17
- 0
exercises/sum-of-multiples/build.gradle Просмотреть файл

@@ -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
+}

+ 27
- 0
exercises/sum-of-multiples/src/example/java/SumOfMultiples.java Просмотреть файл

@@ -0,0 +1,27 @@
1
+import java.util.Arrays;
2
+public class SumOfMultiples {
3
+    
4
+    
5
+    public int Sum(int number, int[] set) {
6
+        
7
+        int sum = 0;
8
+        int count = 0;
9
+        
10
+        for (int i = 1; i < number; i++) {
11
+            
12
+            for (int j = 0; j < set.length; j++) {
13
+                if (i % set[j] == 0) {
14
+                    count++;
15
+                }
16
+            }
17
+            
18
+            if (count > 0) {
19
+                sum = sum + i;
20
+                count = 0;
21
+            }
22
+        }
23
+        
24
+        return sum;
25
+    }
26
+    
27
+}

+ 4
- 0
exercises/sum-of-multiples/src/main/java/SumOfMultiples.java Просмотреть файл

@@ -0,0 +1,4 @@
1
+import java.util.Arrays;
2
+public class SumOfMultiples {
3
+    
4
+}

+ 187
- 0
exercises/sum-of-multiples/src/test/SumOfMultiplesTest.java Просмотреть файл

@@ -0,0 +1,187 @@
1
+import static org.junit.Assert.*;
2
+
3
+import org.junit.Ignore;
4
+import org.junit.Test;
5
+
6
+public class SumOfMultiplesTest {
7
+    
8
+    
9
+    @Test
10
+    public void testSumOfMultiplesOf3and4UpToOne() {
11
+        
12
+        SumOfMultiples mySum = new SumOfMultiples();
13
+        int[] set = {
14
+            3,
15
+            5
16
+        };
17
+        int output = mySum.Sum(1, set);
18
+        assertEquals(0, output);
19
+        
20
+    }
21
+    
22
+    
23
+    @Test
24
+    @Ignore
25
+    public void testSumOfMultiplesOf3and5UpToFour() {
26
+        
27
+        SumOfMultiples mySum = new SumOfMultiples();
28
+        int[] set = {
29
+            3,
30
+            5
31
+        };
32
+        int output = mySum.Sum(4, set);
33
+        assertEquals(3, output);
34
+        
35
+    }
36
+    
37
+    
38
+    @Test
39
+    @Ignore
40
+    public void testSumOfMultiplesOf3and5UpToTen() {
41
+        
42
+        SumOfMultiples mySum = new SumOfMultiples();
43
+        int[] set = {
44
+            3,
45
+            5
46
+        };
47
+        int output = mySum.Sum(10, set);
48
+        assertEquals(23, output);
49
+        
50
+    }
51
+    
52
+    
53
+    @Test
54
+    @Ignore
55
+    public void testSumOfMultiplesOf3and5UpToOneHundred() {
56
+        
57
+        SumOfMultiples mySum = new SumOfMultiples();
58
+        int[] set = {
59
+            3,
60
+            5
61
+        };
62
+        int output = mySum.Sum(100, set);
63
+        assertEquals(2318, output);
64
+        
65
+    }
66
+    
67
+    
68
+    @Test
69
+    @Ignore
70
+    public void testSumOfMultiplesOf3and5UpToOneThousand() {
71
+        
72
+        SumOfMultiples mySum = new SumOfMultiples();
73
+        int[] set = {
74
+            3,
75
+            5
76
+        };
77
+        int output = mySum.Sum(1000, set);
78
+        assertEquals(233168, output);
79
+        
80
+    }
81
+    
82
+    
83
+    @Test
84
+    @Ignore
85
+    public void testSumOfMultiplesOf7and13and17UpToTwenty() {
86
+        
87
+        SumOfMultiples mySum = new SumOfMultiples();
88
+        int[] set = {
89
+            7,
90
+            13,
91
+            17
92
+        };
93
+        int output = mySum.Sum(20, set);
94
+        assertEquals(51, output);
95
+        
96
+    }
97
+    
98
+    
99
+    @Test
100
+    @Ignore
101
+    public void testSumOfMultiplesOf4and6UpToFifteen() {
102
+        
103
+        SumOfMultiples mySum = new SumOfMultiples();
104
+        int[] set = {
105
+            4,
106
+            6
107
+        };
108
+        int output = mySum.Sum(15, set);
109
+        assertEquals(30, output);
110
+        
111
+    }
112
+    
113
+    
114
+    @Test
115
+    @Ignore
116
+    public void testSumOfMultiplesOf5and6and8UpToOneHundredFifty() {
117
+        
118
+        SumOfMultiples mySum = new SumOfMultiples();
119
+        int[] set = {
120
+            5,
121
+            6,
122
+            8
123
+        };
124
+        int output = mySum.Sum(150, set);
125
+        assertEquals(4419, output);
126
+        
127
+    }
128
+    
129
+    
130
+    @Test
131
+    @Ignore
132
+    public void testSumOfMultiplesOf5and25UpToTwoHundredSeventyFive() {
133
+        
134
+        SumOfMultiples mySum = new SumOfMultiples();
135
+        int[] set = {
136
+            5,
137
+            25
138
+        };
139
+        int output = mySum.Sum(51, set);
140
+        assertEquals(275, output);
141
+        
142
+    }
143
+    
144
+    
145
+    @Test
146
+    @Ignore
147
+    public void testSumOfMultiplesOf43and47UpToTenThousand() {
148
+        
149
+        SumOfMultiples mySum = new SumOfMultiples();
150
+        int[] set = {
151
+            43,
152
+            47
153
+        };
154
+        int output = mySum.Sum(10000, set);
155
+        assertEquals(2203160, output);
156
+        
157
+    }
158
+    
159
+    
160
+    @Test
161
+    @Ignore
162
+    public void testSumOfMultiplesOfOneUpToOneHundred() {
163
+        
164
+        SumOfMultiples mySum = new SumOfMultiples();
165
+        int[] set = {
166
+            1
167
+        };
168
+        int output = mySum.Sum(100, set);
169
+        assertEquals(4950, output);
170
+        
171
+    }
172
+    
173
+    
174
+    @Test
175
+    @Ignore
176
+    public void testSumOfMultiplesOfNoneUpToTenThousand() {
177
+        
178
+        SumOfMultiples mySum = new SumOfMultiples();
179
+        int[] set = {};
180
+        int output = mySum.Sum(10000, set);
181
+        assertEquals(0, output);
182
+        
183
+    }
184
+    
185
+    
186
+    
187
+}