Skip to content

Commit 0efdd75

Browse files
committed
Problem 4.1 and 4.2 TADM
1 parent d53aed5 commit 0efdd75

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.shekhargulati;
2+
3+
import java.util.Objects;
4+
5+
public class IntPair {
6+
7+
public int first;
8+
public int second;
9+
10+
public IntPair(int first, int second) {
11+
this.first = first;
12+
this.second = second;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return "IntPair{" +
18+
"first=" + first +
19+
", second=" + second +
20+
'}';
21+
}
22+
23+
@Override
24+
public boolean equals(Object o) {
25+
if (this == o) return true;
26+
if (o == null || getClass() != o.getClass()) return false;
27+
IntPair intPair = (IntPair) o;
28+
return first == intPair.first &&
29+
second == intPair.second;
30+
}
31+
32+
@Override
33+
public int hashCode() {
34+
return Objects.hash(first, second);
35+
}
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.shekhargulati.tadm.ch04.excercises;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import java.util.stream.Collectors;
7+
8+
public class Problem4_1 {
9+
10+
public static List<List<Player>> teams(List<Player> players) {
11+
List<Player> sorted = players.stream().sorted((p1, p2) -> p1.rating - p2.rating).collect(Collectors.toList());
12+
int totalPlayers = players.size();
13+
return Arrays.asList(sorted.subList(0, totalPlayers / 2), sorted.subList(totalPlayers / 2, totalPlayers));
14+
}
15+
16+
}
17+
18+
class Player {
19+
String name;
20+
int rating;
21+
22+
public Player(String name, int rating) {
23+
this.name = name;
24+
this.rating = rating;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Player{" +
30+
"name='" + name + '\'' +
31+
", rating=" + rating +
32+
'}';
33+
}
34+
35+
@Override
36+
public boolean equals(Object o) {
37+
if (this == o) return true;
38+
if (o == null || getClass() != o.getClass()) return false;
39+
Player player = (Player) o;
40+
return rating == player.rating &&
41+
Objects.equals(name, player.name);
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hash(name, rating);
47+
}
48+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.shekhargulati.tadm.ch04.excercises;
2+
3+
import com.shekhargulati.IntPair;
4+
5+
import java.util.OptionalInt;
6+
import java.util.stream.IntStream;
7+
8+
public class Problem4_2 {
9+
10+
public static IntPair maximize_unsorted(int[] numbers) {
11+
OptionalInt min = IntStream.of(numbers).min();
12+
OptionalInt max = IntStream.of(numbers).max();
13+
return new IntPair(min.getAsInt(), max.getAsInt());
14+
}
15+
16+
public static IntPair maximize_sorted(int[] numbers) {
17+
return new IntPair(numbers[0], numbers[numbers.length - 1]);
18+
}
19+
20+
public static IntPair minimize_unsorted(int[] numbers) {
21+
int[] sorted = IntStream.of(numbers).sorted().toArray();
22+
return minimize_sorted(sorted);
23+
}
24+
25+
public static IntPair minimize_sorted(int[] sorted) {
26+
int diff = sorted[1] - sorted[0];
27+
IntPair pair = null;
28+
for (int i = 1; i < sorted.length - 1; i++) {
29+
int first = sorted[i];
30+
int second = sorted[i + 1];
31+
if (diff > (second - first)) {
32+
diff = second - first;
33+
pair = new IntPair(first, second);
34+
}
35+
}
36+
return pair;
37+
}
38+
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.shekhargulati.tadm.ch04.excercises;
2+
3+
import org.hamcrest.CoreMatchers;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertThat;
10+
11+
public class Problem4_1Test {
12+
13+
@Test
14+
public void shouldDistributePlayersIntoTwoUnfairTeams() throws Exception {
15+
List<Player> players = Arrays.asList(new Player("a", 10), new Player("b", 7), new Player("c", 11), new Player("d", 2), new Player("e", 4), new Player("f", 15));
16+
17+
List<List<Player>> teams = Problem4_1.teams(players);
18+
assertThat(teams.get(0), CoreMatchers.equalTo(Arrays.asList(new Player("d", 2), new Player("e", 4), new Player("b", 7))));
19+
assertThat(teams.get(1), CoreMatchers.equalTo(Arrays.asList(new Player("a", 10), new Player("c", 11), new Player("f", 15))));
20+
21+
}
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.shekhargulati.tadm.ch04.excercises;
2+
3+
import com.shekhargulati.IntPair;
4+
import org.junit.Test;
5+
6+
import static org.hamcrest.CoreMatchers.equalTo;
7+
import static org.junit.Assert.assertThat;
8+
9+
public class Problem4_2Test {
10+
11+
@Test
12+
public void shouldFindPairThatMaximizeWithinUnsortedArray() throws Exception {
13+
int[] numbers = {6, 13, 19, 3, 8};
14+
15+
IntPair p = Problem4_2.maximize_unsorted(numbers);
16+
assertThat(p, equalTo(new IntPair(3, 19)));
17+
}
18+
19+
@Test
20+
public void shouldFindPairThatMaximizeWithinSortedArray() throws Exception {
21+
int[] numbers = {3, 6, 8, 13, 19};
22+
23+
IntPair p = Problem4_2.maximize_sorted(numbers);
24+
assertThat(p, equalTo(new IntPair(3, 19)));
25+
}
26+
27+
@Test
28+
public void shouldFindPairThatMinimizeWithinUnsortedArray() throws Exception {
29+
int[] numbers = {6, 13, 19, 3, 8, 14};
30+
31+
IntPair p = Problem4_2.minimize_unsorted(numbers);
32+
assertThat(p, equalTo(new IntPair(13, 14)));
33+
}
34+
35+
@Test
36+
public void shouldFindPairThatMinimiseWithinSortedArray() throws Exception {
37+
int[] numbers = {3, 6, 8, 13, 19};
38+
39+
IntPair p = Problem4_2.minimize_sorted(numbers);
40+
assertThat(p, equalTo(new IntPair(6, 8)));
41+
}
42+
}

0 commit comments

Comments
 (0)