Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 27 additions & 32 deletions LambdaLab/test/exercises/C_DefaultMethods.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package exercises;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -11,10 +14,6 @@
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* This set of exercises covers new default methods on
* the Collections and related APIs.
Expand All @@ -25,15 +24,15 @@ public class C_DefaultMethods {
* Given a list of StringBuilders, modify each StringBuilder
* in-place by appending the string "new" to each one.
*/
@Test @Ignore
@Test
public void c01_appendNew() {
List<StringBuilder> sbList = List.of(
new StringBuilder("alfa"),
new StringBuilder("bravo"),
new StringBuilder("charlie"));

// TODO write code to modify sbList

sbList.stream().forEach(x->x.append("new"));
assertEquals(List.of("alfanew", "bravonew", "charlienew"),
sbList.stream()
.map(StringBuilder::toString)
Expand All @@ -48,13 +47,13 @@ public void c01_appendNew() {
/**
* Remove the words that have odd lengths from the list.
*/
@Test @Ignore
@Test
public void c02_removeOddLengthWords() {
List<String> list = new ArrayList<>(Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot"));

// TODO write code to modify list

list.removeIf(x->x.length()%2!=0);
assertEquals(List.of("alfa", "echo"), list);
}
// Hint:
Expand All @@ -66,13 +65,12 @@ public void c02_removeOddLengthWords() {
/**
* Replace every word in the list with its upper case equivalent.
*/
@Test @Ignore
@Test
public void c03_upcaseAllWords() {
List<String> list = Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot");

list.replaceAll(String::toUpperCase);
// TODO code to modify list

assertEquals(List.of("ALFA", "BRAVO", "CHARLIE", "DELTA", "ECHO", "FOXTROT"),
list);
}
Expand All @@ -87,15 +85,15 @@ public void c03_upcaseAllWords() {
* append to each StringBuilder the string representation of its corresponding
* Integer key. This should mutate each StringBuilder value in-place.
*/
@Test @Ignore
@Test
public void c04_appendToMapValues() {
Map<Integer, StringBuilder> map = new TreeMap<>();
map.put(1, new StringBuilder("alfa"));
map.put(2, new StringBuilder("bravo"));
map.put(3, new StringBuilder("charlie"));

// TODO write code to modify map

map.forEach((k,v)->v.append(k));
assertEquals(3, map.size());
assertTrue(map.values().stream().allMatch(x -> x instanceof StringBuilder));
assertEquals("alfa1", map.get(1).toString());
Expand All @@ -113,15 +111,15 @@ public void c04_appendToMapValues() {
* append to each String the string representation of its corresponding
* Integer key.
*/
@Test @Ignore
@Test
public void c05_replaceMapValues() {
Map<Integer, String> map = new TreeMap<>();
map.put(1, "alfa");
map.put(2, "bravo");
map.put(3, "charlie");

// TODO write code to modify map

map.replaceAll((k,v)->v+k);
assertEquals(Map.of(1, "alfa1",
2, "bravo2",
3, "charlie3"),
Expand All @@ -137,16 +135,15 @@ public void c05_replaceMapValues() {
* Given a list of words, populate a map whose keys are the lengths of
* each word, and whose values are list of words with that length.
*/
@Test @Ignore
@Test
public void c06_mapOfListOfStringsByLength() {
List<String> list = List.of(
"aardvark", "bison", "capybara",
"alligator", "bushbaby", "chimpanzee",
"avocet", "bustard", "capuchin");
Map<Integer, List<String>> result = new TreeMap<>();

list.forEach(s->result.computeIfAbsent(s.length(), k -> new ArrayList<>()).add(s));
// TODO write code to populate result

assertEquals(Map.of( 5, List.of("bison"),
6, List.of("avocet"),
7, List.of("bustard"),
Expand All @@ -165,16 +162,15 @@ public void c06_mapOfListOfStringsByLength() {
* initial character. When concatenating the words, they should be
* separated by a colon (':').
*/
@Test @Ignore
@Test
public void c07_mapOfStringByInitialCharacter() {
List<String> list = List.of(
"aardvark", "bison", "capybara",
"alligator", "bushbaby", "chimpanzee",
"avocet", "bustard", "capuchin");
Map<Character, String> result = new TreeMap<>();

// TODO write code to populate result

list.forEach(x->result.merge(x.charAt(0), x, (y1,y2)->y1+":"+y2));
assertEquals(Map.of('a', "aardvark:alligator:avocet",
'b', "bison:bushbaby:bustard",
'c', "capybara:chimpanzee:capuchin"),
Expand All @@ -192,7 +188,7 @@ public void c07_mapOfStringByInitialCharacter() {
* null, and we need to add checks to protect against NullPointerException.
* Write code to ensure that all missing keys are mapped to the empty string.
*/
@Test @Ignore
@Test
public void c08_mapWithMissingValues() {
List<String> keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g");
Map<String, String> map = new HashMap<>(Map.of("a", "alfa",
Expand All @@ -201,7 +197,7 @@ public void c08_mapWithMissingValues() {
"d", "delta"));

// TODO write code to fix the map

keys.forEach(k->map.putIfAbsent(k, ""));
assertEquals(Map.of("a", "alfa",
"b", "bravo",
"c", "charlie",
Expand All @@ -222,7 +218,7 @@ public void c08_mapWithMissingValues() {
* We've now determined that's incorrect, and we want to undo that. This
* time, we want to remove the entry if the value is the empty string.
*/
@Test @Ignore
@Test
public void c09_mapRemoveEntriesWithEmptyValues() {
List<String> keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g");
Map<String, String> map = new HashMap<>(Map.of("a", "alfa",
Expand All @@ -234,7 +230,7 @@ public void c09_mapRemoveEntriesWithEmptyValues() {
"g", ""));

// TODO write code to fix the map

keys.forEach(k->map.remove(k,""));
assertEquals(Map.of("a", "alfa",
"b", "bravo",
"c", "charlie",
Expand All @@ -253,7 +249,7 @@ public void c09_mapRemoveEntriesWithEmptyValues() {
* to replace the empty-string values with a value that's the key itself.
* Write the code to do that.
*/
@Test @Ignore
@Test
public void c10_mapReplaceEmptyValues() {
List<String> keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g");
Map<String, String> map = new HashMap<>(Map.of("a", "alfa",
Expand All @@ -265,7 +261,7 @@ public void c10_mapReplaceEmptyValues() {
"g", ""));

// TODO write code to fix the map

keys.forEach(k->map.replace(k, "", k));
assertEquals(Map.of("a", "alfa",
"b", "bravo",
"c", "charlie",
Expand All @@ -287,16 +283,15 @@ public void c10_mapReplaceEmptyValues() {
* that are not present, we want to add an entry where the value is the
* same as the key.
*/
@Test @Ignore
@Test
public void c11_computeWithMissingEntries() {
List<String> keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g");
Map<String, String> map = new HashMap<>(Map.of("a", "alfa",
"b", "bravo",
"c", "charlie",
"d", "delta"));

// TODO write code transform the map

keys.forEach(k->map.compute(k, (key,value)->value==null?k:value.toUpperCase()));
assertEquals(Map.of("a", "ALFA",
"b", "BRAVO",
"c", "CHARLIE",
Expand All @@ -319,7 +314,7 @@ public void c11_computeWithMissingEntries() {
* the non-empty values to upper case, but we want to remove the entries
* for which the values are the empty string.
*/
@Test @Ignore
@Test
public void c12_computeAndRemoveSomeEntries() {
List<String> keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g");
Map<String, String> map = new HashMap<>(Map.of("a", "alfa",
Expand All @@ -331,7 +326,7 @@ public void c12_computeAndRemoveSomeEntries() {
"g", ""));

// TODO write code transform the map

keys.forEach(key->map.compute(key, (k,v)->v.isEmpty()?null:v.toUpperCase()));
assertEquals(Map.of("a", "ALFA",
"b", "BRAVO",
"c", "CHARLIE",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This is forked reposoitory from https://github.com/stuart-marks/LambdaHOLv2.git
# Solutions will be updated
# Oracle Code One Lambda Programming Laboratory

## Introduction
Expand Down