List of all available Websheets
Viewing java/12-structures/CME by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Define a method <tt>addReverses(Set<String> words)</tt> that takes a set of strings, and adds to that set the reverse of every string in the set. For example, if you call it on a set containing <tt>"HI"</tt> and <tt>"MOM"</tt> then after your method executes, it should contain <tt>"HI"</tt>, <tt>"IH"</tt> and <tt>"MOM"</tt> (only once, since that's all a <tt>Set</tt> can contain). <p> A partial solution is given below, but it throws a <tt>ConcurrentModificationException</tt>. You have to fix it. |
Public permissions | |
Engine | |
Template / Reference solution | public class CME { public static void addReverses(Set<String> words) { \[ // we will put the reverses in a new place for now // while we iterate through the main set java.util.HashSet<String> tmp = new java.util.HashSet<String>(); for (String s : words) tmp.add(new StringBuffer(s).reverse().toString()); // now that we're done iterating through 'words', add to it for (String s : tmp) words.add(s); \show: for (String s : words) { // for each string in the set // a one-liner to compute the reverse of a string String sReverse = new StringBuffer(s).reverse().toString(); // add it to our set words.add(sReverse); } ]\ } // test client: e.g. "java CME HI MOM" should print out on 3 lines HI IH MOM public static void main(String[] args) { Set<String> testSet = new TreeSet<String>(); for (int i=0; i < args.length; i++) testSet.add(args[i]); addReverses(testSet); for (String s : testSet) System.out.println(s); } } |
Java test suite See manual | testMain("HI", "MOM"); testMain("MADAM", "IM", "ADAM"); testMain("NOW", "SIR", "A", "WAR", "IS", "WON"); |
Solution visibility | |
Java imports json list of importables e.g. ["java.util.*"] | ["java.util.Set", "java.util.TreeSet"] |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.