Skip to content
Snippets Groups Projects
Commit 00fa411d authored by Stuart Marks's avatar Stuart Marks
Browse files

Rename Exercises into G_Challenges plus a bit of cleanup

parent 1b0dc17f
No related branches found
No related tags found
No related merge requests found
......@@ -19,25 +19,18 @@ package exercises;
* is located at the root of this NetBeans project.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.IntConsumer;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
......@@ -52,14 +45,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class Exercises {
// ========================================================
// ADVANCED STREAMS: POTPOURRI
// ========================================================
public class G_Challenges {
/**
* Denormalize this map. The input is a map whose keys are the number of legs of an animal
......@@ -312,7 +298,8 @@ public class Exercises {
IntStream.range(0, 100).mapToObj(String::valueOf).parallel();
Collection<String> result =
input.collect(Collector.of(null, null, null)); // TODO
input.collect(Collector.of(null, null, null));
// TODO fill in collector functions above
assertEquals(
......@@ -345,26 +332,4 @@ public class Exercises {
assertEquals(OptionalInt.of(4), result1);
assertFalse(result2.isPresent());
}
// ========================================================
// END OF EXERCISES -- CONGRATULATIONS!
// TEST INFRASTRUCTURE IS BELOW
// ========================================================
static final String REGEXP = "[- .:,]+"; // for splitting into words
private BufferedReader reader;
@Before
public void z_setUpBufferedReader() throws IOException {
reader = Files.newBufferedReader(
Paths.get("SonnetI.txt"), StandardCharsets.UTF_8);
}
@After
public void z_closeBufferedReader() throws IOException {
reader.close();
}
}
......@@ -19,25 +19,18 @@ package solutions;
* is located at the root of this NetBeans project.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.IntConsumer;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
......@@ -52,37 +45,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
//BEGINREMOVE
import java.util.ArrayDeque;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Function;
import java.util.stream.Collector;
import static java.util.Map.Entry;
import static java.util.AbstractMap.SimpleEntry;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.partitioningBy;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
//ENDREMOVE
public class Exercises {
// ========================================================
// ADVANCED STREAMS: POTPOURRI
// ========================================================
public class G_Challenges {
/**
* Denormalize this map. The input is a map whose keys are the number of legs of an animal
......@@ -116,7 +79,7 @@ public class Exercises {
input.put(10, Arrays.asList("crab", "lobster", "scorpion"));
input.put(750, Arrays.asList("millipede"));
//UNCOMMENT//List<String> result = null; // TODO
//TODO//List<String> result = null;
//BEGINREMOVE
// Simple solution: use Map.forEach to iterate over each entry,
......@@ -185,13 +148,15 @@ public class Exercises {
input.put("e", Arrays.asList(2, 4));
input.put("f", Arrays.asList(3, 4));
//UNCOMMENT//Map<Integer, List<String>> result = null; // TODO
//TODO//Map<Integer, List<String>> result = null;
//BEGINREMOVE
Map<Integer, Set<String>> result =
input.entrySet().stream()
.flatMap(e -> e.getValue().stream()
.map(v -> new SimpleEntry<>(e.getKey(), v)))
.collect(groupingBy(Entry::getValue, mapping(Entry::getKey, toSet())));
.map(v -> new AbstractMap.SimpleEntry<>(e.getKey(), v)))
.collect(Collectors.groupingBy(Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey,
Collectors.toSet())));
//ENDREMOVE
assertEquals(new HashSet<>(Arrays.asList("a", "c", "d")), result.get(1));
......@@ -214,13 +179,13 @@ public class Exercises {
List<String> input = Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel");
//UNCOMMENT//List<String> result = null; // TODO
//TODO//List<String> result = null;
//BEGINREMOVE
List<String> result =
IntStream.range(0, input.size())
.filter(pos -> pos == 0 || input.get(pos-1).length() < input.get(pos).length())
.mapToObj(pos -> input.get(pos))
.collect(toList());
.collect(Collectors.toList());
//ENDREMOVE
assertEquals("[alfa, bravo, charlie, foxtrot, hotel]", result.toString());
......@@ -245,12 +210,12 @@ public class Exercises {
List<String> input = Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot");
//UNCOMMENT//List<String> result = null; // TODO
//TODO//List<String> result = null;
//BEGINREMOVE
List<String> result =
IntStream.range(1, input.size())
.mapToObj(pos -> input.get(pos-1) + input.get(pos))
.collect(toList());
.collect(Collectors.toList());
//ENDREMOVE
assertEquals("[alfabravo, bravocharlie, charliedelta, deltaecho, echofoxtrot]",
......@@ -273,7 +238,7 @@ public class Exercises {
List<String> input = Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel");
//UNCOMMENT//List<String> result = null; // TODO
//TODO//List<String> result = null;
//BEGINREMOVE
int max = input.stream()
.mapToInt(String::length)
......@@ -282,7 +247,7 @@ public class Exercises {
List<String> result = input.stream()
.filter(s -> s.length() == max)
.collect(toList());
.collect(Collectors.toList());
//ENDREMOVE
assertEquals("[charlie, foxtrot]", result.toString());
......@@ -300,7 +265,7 @@ public class Exercises {
Stream<String> input = Stream.of(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel");
//UNCOMMENT//List<String> result = null; // TODO
//TODO//List<String> result = null;
//BEGINREMOVE
List<String> result = new ArrayList<>();
......@@ -336,19 +301,19 @@ public class Exercises {
List<String> input = Arrays.asList(
"alfa", "bravo", ":charlie", "delta", ":echo", ":foxtrot", "golf", "hotel");
//UNCOMMENT//List<List<String>> result = null; // TODO
//TODO//List<List<String>> result = null;
//BEGINREMOVE
List<Integer> bounds =
IntStream.rangeClosed(0, input.size())
.filter(i -> i == 0 || i == input.size() || input.get(i).startsWith(":"))
.boxed()
.collect(toList());
.collect(Collectors.toList());
List<List<String>> result =
IntStream.range(1, bounds.size())
.mapToObj(i -> input.subList(bounds.get(i-1), bounds.get(i)))
.collect(toList());
.collect(Collectors.toList());
//ENDREMOVE
......@@ -365,13 +330,14 @@ public class Exercises {
public void ex33_separateOddEvenSums() {
IntStream input = new Random(987523).ints(20, 0, 100);
//UNCOMMENT//int sumEvens = 0; // TODO
//UNCOMMENT//int sumOdds = 0; // TODO
//TODO//int sumEvens = 0;
//TODO//int sumOdds = 0;
//BEGINREMOVE
Map<Boolean, Integer> sums =
input.boxed()
.collect(partitioningBy(i -> (i & 1) == 1, Collectors.summingInt(i -> i)));
.collect(Collectors.partitioningBy(i -> (i & 1) == 1,
Collectors.summingInt(i -> i)));
int sumEvens = sums.get(false);
int sumOdds = sums.get(true);
//ENDREMOVE
......@@ -396,7 +362,7 @@ public class Exercises {
public void ex34_splitCharacterRuns() {
String input = "aaaaabbccccdeeeeeeaaafff";
//UNCOMMENT//List<String> result = null; // TODO
//TODO//List<String> result = null;
//BEGINREMOVE
List<Integer> bounds =
......@@ -404,12 +370,12 @@ public class Exercises {
.filter(i -> i == 0 || i == input.length() ||
input.charAt(i-1) != input.charAt(i))
.boxed()
.collect(toList());
.collect(Collectors.toList());
List<String> result =
IntStream.range(1, bounds.size())
.mapToObj(i -> input.substring(bounds.get(i-1), bounds.get(i)))
.collect(toList());
.collect(Collectors.toList());
//ENDREMOVE
......@@ -426,7 +392,7 @@ public class Exercises {
public void ex35_longestCharacterRuns() {
String input = "aaaaabbccccdeeeeeeaaafff";
//UNCOMMENT//String result = null; // TODO
//TODO//String result = null;
//BEGINREMOVE
List<Integer> bounds =
......@@ -434,7 +400,7 @@ public class Exercises {
.filter(i -> i == 0 || i == input.length() ||
input.charAt(i-1) != input.charAt(i))
.boxed()
.collect(toList());
.collect(Collectors.toList());
String result =
IntStream.range(1, bounds.size())
......@@ -460,7 +426,8 @@ public class Exercises {
IntStream.range(0, 100).mapToObj(String::valueOf).parallel();
//UNCOMMENT//Collection<String> result =
//UNCOMMENT// input.collect(Collector.of(null, null, null)); // TODO
//UNCOMMENT// input.collect(Collector.of(null, null, null));
//UNCOMMENT// // TODO fill in collector functions above
//BEGINREMOVE
......@@ -487,16 +454,17 @@ public class Exercises {
*/
OptionalInt majority(int[] array) {
//UNCOMMENT//return null; // TODO
//TODO//return null;
//BEGINREMOVE
Map<Integer, Long> map =
Arrays.stream(array)
.boxed()
.collect(groupingBy(x -> x, counting()));
.collect(Collectors.groupingBy(x -> x,
Collectors.counting()));
return map.entrySet().stream()
.filter(e -> e.getValue() > array.length / 2)
.mapToInt(Entry::getKey)
.mapToInt(Map.Entry::getKey)
.findAny();
//ENDREMOVE
}
......@@ -512,36 +480,4 @@ public class Exercises {
assertEquals(OptionalInt.of(4), result1);
assertFalse(result2.isPresent());
}
// ========================================================
// END OF EXERCISES -- CONGRATULATIONS!
// TEST INFRASTRUCTURE IS BELOW
// ========================================================
static final String REGEXP = "[- .:,]+"; // for splitting into words
private BufferedReader reader;
@Before
public void z_setUpBufferedReader() throws IOException {
reader = Files.newBufferedReader(
Paths.get("SonnetI.txt"), StandardCharsets.UTF_8);
}
@After
public void z_closeBufferedReader() throws IOException {
reader.close();
}
}
//BEGINREMOVE
/*
* Procedure for deriving exercise file from answers.
* - Open a shell and change do the LambdaLab/test/solutions directory.
* - Run the "cleanit" perl script from within this directory.
* - This should generate the LambdaLab/test/exercises/Exercises.java file automatically.
* - Make sure the only files open in the project are (unsolved!) Exercises.java and
* SonnetI.txt, then run clean, and close the NB project.
*/
//ENDREMOVE
\ No newline at end of file
}
\ No newline at end of file
......@@ -7,7 +7,11 @@ import org.junit.runners.Suite;
@Suite.SuiteClasses(value={
exercises.A_Lambdas.class,
exercises.B_Comparators.class,
exercises.Exercises.class
exercises.C_DefaultMethods.class,
exercises.D_SimpleStreams.class,
exercises.E_IntermediateStreams.class,
exercises.F_AdvancedStreams.class,
exercises.G_Challenges.class
})
public class JUnit4TestSuite {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment