Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.
Goals for this problem set:
List
: a sequence of elements with integer indexes
Set
: a collection of unique elements that is fast for testing membership
Map
: a collection of (key, value) pairs with fast lookup by key
stutter
Write a method called stutter
that accepts a List
of integers as its parameter and doubles the size of the list by replacing every integer in the list with two of that integer.
For example, if a list named nums
stores [1, 8, 19, 4, 17]
, the call of stutter(nums);
should modify it to store [1, 1, 8, 8, 19, 19, 4, 4, 17, 17]
.
removeInRange
Write a method removeInRange
that accepts four parameters: a List
of integers, an element value, a starting index, and an ending index.
The method's behavior is to remove all occurrences of the given element that appear in the list between the starting index (inclusive) and the ending index (exclusive).
Other values and occurrences of the given value that appear outside the given index range are not affected.
For example, for the "before" list below, a call of removeInRange(list, 0, 5, 13);
should produce the "after" list shown.
Notice that the zeros located at indices between 5 inclusive and 13 exclusive in the original list (before any modifications were made) have been removed.
before: [0, 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 0, 12, 0, 14, 0, 16] after : [0, 0, 2, 0, 4, 6, 8, 10, 12, 0, 14, 0, 16]
setMystery
What elements are contained in the following set after this code executes?
Set<Integer> set = new TreeSet<Integer>();
set.add(74);
set.add(12);
set.add(74);
set.add(74);
set.add(43);
set.remove(74);
set.remove(999);
set.remove(43);
set.add(32);
set.add(12);
set.add(9);
set.add(999); // [9, 12, 32, 999]
hasOdd
Write a method hasOdd
that takes a Set
of integers as a parameter and that returns true
if the set contains at least one odd integer, or false
otherwise.
For example, if the set is [14, 6, 37, 2, 19, 8]
, return true
.
removeEvens
Write a method removeEvens
that takes a Set
of integers as a parameter and that removes the even values from the set, returning those values as a new set.
The new set should be ordered in increasing numerical order.
For example, if a set s1
contains these values:
[0, 17, 16, 7, 10, 12, 13, 14]
and we make the following call:
Set<Integer> s2 = removeEvens(s1);
Then after the call, s1
and s2
would contain the following values:
s1: [17, 7, 13] s2: [0, 10, 12, 14, 16]
mapMystery
Write the set returned when passing each Map
to the following method:
public static Set<String> mystery(Map<String, String> data) { Set<String> result = new TreeSet<String>(); for (String s : data.keySet()) { result.add(data.get(s)); } return result; }
{baz=c, mumble=d, foo=a, bar=b} // [a, b, c, d] {f=z, d=x, e=y, b=y, c=z, a=x} // [x, y, z] {f=2, g=10, d=20, e=1, b=10, c=2, a=1, h=20} // [1, 10, 2, 20]
mapMystery2
Write the output produced when passing each Map
to the following method:
public static void mapMystery2(Map<String, String> m) { Set<String> s = new TreeSet<String>(); for (String key : m.keySet()) { if (!m.get(key).equals(key)) { s.add(m.get(key)); } else { s.remove(m.get(key)); } } System.out.println(s); }
{sheep=wool, house=brick, cast=plaster, wool=wool} // [brick, plaster] {munchkin=blue, winkie=yellow, corn=yellow, grass=green, emerald=green} // [blue, green, yellow] {pumpkin=peach, corn=apple, apple=apple, pie=fruit, peach=peach} // [fruit] {lab=ipl, lion=cat, terrier=dog, cat=cat, platypus=animal, nyan=cat} // [animal, cat, dog, ipl]
counts
Write a method counts
that accepts a List
of integers and a Set
of integers as parameters, and returns a map from each value in the set to the number of occurrences of that value in the list.
For example, if your method is passed the following list and set as parameters:
list: [4, -2, 3, 9, 4, 17, 5, 29, 14, 87, 4, -2, 100] set: [-2, 4, 29]
Then your method should return the map {-2=2, 4=3, 29=1}
, because there aretwo occurrences of -2, three occurrences of 4, and one occurrence of 29.
split
Write a method called split
that takes a set of strings as a parameter and that returns the result of splitting the strings into different sets based on the length of the strings.
In particular, your method should return a map whose keys are integers and whose values are sets of strings of that length.
For example, if a variable called words
contains the following set of strings:
[to, be, or, not, that, is, the, question]
Then your method should return the map {2=[be, is, or, to], 3=[not, the], 4=[that], 8=[question]}
.