2Exercise 1:FileUnionDifference
(10 pts)
This is a class that is initialized with a list of files, stores all the words that appear in them in sets, and then answers queries about them. IfMfiles were provided, then queries follow the syntax:
query::= +NUM
query| -NUM
query| iNUM
query| cquery|
NUM::= 1 | 2 | … |M
A query asks for the set of words that is obtained in the way prescribed by the query. That setSstarts empty, then:
- +NUMsetsSto be the union ofSand the set of words appearing in theNUM-th file.
- -NUMsetsSto be the set of elements inSthat don’t appear in theNUM-th file.
- iNUMsetsSto be the intersection ofSand the set of words appearing in theNUM-th file.
- c setsSto be the set of words that appear in some file, but not inS.
As an example, if the object is initialized with the filesa.txt, b.txt, c.txt
, then the query+1c-2i3
will return the set of words that is built by starting with the words ina.txt
, then, usingc
, taking all the words but those that appear ina.txt
, then removing the words that appear inb.txt
, and finally intersecting with the words inc.txt
.
Dos and don’ts.Thequery
function should be fast, and is not allowed to re-read the files (which may have been deleted at this point). The files may contain a huge amount of duplicates:be sure not to store the duplicates.
Easier to use In.java class from princeton algs4
package pa2.src; import edu.princeton.cs.algs4.*; import java.util.concurrent.Callable; import java.io.File; import java.util.List; import java.util.Queue; import java.util.ArrayList; import java.util.Arrays; import java.util.TreeSet; class Test { //////////////////////////////////////////////////////////// EXERCISE 1 TEST static private Void testEx1 () throws TestException { String[] files = {"tests/all.txt", "tests/even.txt", "tests/odd.txt"}; //create a String array of file names String[] tests = {"+1-2+3c", "+3-2+1c", "+3-2+1", "c-1-2+3c", "ci2i3c+3-3-2i2ci3-3+3"}; //create an array of type String called tests ProgrammingAssignment2 pa2 = new ProgrammingAssignment2 (); //creates a new PA2 ProgrammingAssignment2.FileUnionDifference f = pa2.new FileUnionDifference (Arrays.asList (files)); //creates a array for (String test : tests) { TreeSet
set = new TreeSet<> (f.query (test)); File file = new File ("tests/" + test + ".txt"); In in = new In (file); while (!in.isEmpty ()) { String s = in.readString (), s2 = set.pollFirst (); if (!s.equals (s2)) throw new TestException ("string " + s2 + " returned, expected " + s); } if (!set.isEmpty ()) throw new TestException ("more elements in returned set than expected"); } return null; } //////////////////////////////////////////////////////////// EXERCISE 2 TEST static private Void testEx2 () throws TestException { TwoThreeTree tree; for (int i = 0; i < testsuite2.length;="" ++i)="" {="" tree="TwoThreeTree.fromString" (testsuite2[i][0]);="" for="" (int="" j="1;" j="">< testsuite2[i].length="" -="" 1;="" ++j)="" {="" int="" n="Integer.parseInt" (testsuite2[i][j]);="" tree.delete="" (n);="" if="" (tree.contains="" (n))="" throw="" new="" testexception="" ("contains()="" failed="" on="" delete("="" +="" n="" +="" ")="" in="" tree="" "="" +="" i);="" if="" (!tree.is23="" ())="" throw="" new="" testexception="" ("tree="" is="" not="" 2-3="" after="" delete("="" +="" n="" +="" ")="" in="" tree="" "="" +="" i);="" }="" check="" that="" the="" tree="" obtained="" is="" correct.="" if="" (!="" (!tree.contains="" (-10)="" &&="" !tree.contains="" (1="">< 30)="" &&="" testsuite2[i][testsuite2[i].length="" -="" 1].equals="" (tree.tostring="" ())))="" throw="" new="" testexception="" ("deletion="" in="" tree="" "="" +="" i="" +="" "="" failed.\n"="" +="" "="" expected:="" "="" +="" testsuite2[i][testsuite2[i].length="" -="" 1]="" +="" "\n"="" +="" "="" produced:="" "="" +="" tree);="" }="" return="" null;="" }="" exercise="" 3="" test="" static="" class="" hashedchar="" {="" final="" char="" c;="" final="" int="" h;="" public="" hashedchar="" (char="" c,="" int="" h)="" {="" this.c="c;" this.h="h;" }="" public="" string="" tostring="" ()="" {="" return="" ""="" +="" this.c="" +="" "("="" +="" h="" +="" ")";="" }="" public="" int="" hashcode="" ()="" {="" return="" h;="" }="" public="" boolean="" equals="" (object="" o)="" {="" if="" (this="=" o)="" return="" true;="" if="" (!(o="" instanceof="" hashedchar))="" return="" false;="" hashedchar="" hc="(HashedChar)" o;="" return="" (hc.c="=" c="" &&="" hc.h="=" h);="" }="" }="" static="" private="" hashedchar="" hc="" (char="" c,="" int="" h)="" {="" return="" new="" hashedchar="" (c,="" h);="" }="" static="" private="" void="" testex3="" ()="" throws="" testexception="" {=""> st = new HashST<> (); Object o = new Object (); st.put (hc ('a', 0), o); st.put (hc ('b', 0), o); st.put (hc ('c', 2), o); st.put (hc ('d', 0), o); st.delete (hc ('b', 0)); //so 2nd bucket gets emptied out and look at each successor until find a replacement, in this case d can be moved because //first successor with same hash 0 //when finding a successor or new +1 position you don't go i=i+1, you supposed to do i = (i+1)% m, this how you get back to beginning, probably be j not i, while loop if (!st.toString ().equals ("a(0) d(0) c(2) null null null null null ")) //output should be this throw new TestException ("failed, got " + st.toString ()); return null; }//while loop should go back to beginning of array if all keys stored towards the end. Loop back to beginning if no successor etc... /////////////////////////////////////////////////// TESTING FRAMEWORK & MAIN static class TestException extends Exception { static final long serialVersionUID = 314; public TestException (String msg) { super (msg); } } static private void runTest (String name, Callable test) { StdOut.println ("TESTING " + name); try { test.call (); StdOut.println ("SUCCESS"); } catch (TestException e) { StdOut.println ("TEST FAILED: " + e.getMessage ()); } catch (Exception | Error e) { StdOut.print ("FATAL: "); e.printStackTrace (); } } static public void main(String[] args) { runTest ("EX 1", Test::testEx1); runTest ("EX 2", Test::testEx2); runTest ("EX 3", Test::testEx3); } ///////////////////////////////////////////////////////////// TEST DATABASES static String[][] testSuite2 = {{"51 84 -- {19 37 -- {1 14 -- {0, 9 11, 17 18}, 27 -- {21 24, 31 36}, 45 -- {39, 47 49}}, 71 -- {59 65 -- {53 58, 60, 70}, 74 80 -- {72, 79, 83}}, 95 -- {86 88 -- {85, 87, 94}, 97 -- {96, 98}}}", "72", "71", "70", "39", "74", "72", "86", "31", "88", "85", "37", "31", "59", "96", "65", "65", "60", "31", "9", "0", "45 -- {19 -- {14 -- {1 11, 17 18}, 27 -- {21 24, 36}}, 79 94 -- {51 -- {47 49, 53 58}, 84 -- {80 83, 87}, 97 -- {95, 98}}}"}, {"35 71 -- {7 20 -- {5 -- {2 4, 6}, 13 18 -- {10, 15, 19}, 33 -- {27, 34}}, 55 -- {42 49 -- {39 41, 44 47, 51}, 66 -- {61 62, 68 69}}, 86 -- {80 -- {77 78, 81}, 94 -- {89, 99}}}", "19", "34", "34", "44", "33", "20", "33", "44", "77", "77", "19", "10", "39", "20", "62", "49", "86", "18", "2", "39", "35 -- {7 -- {5 -- {4, 6}, 15 -- {13, 27}}, 55 71 -- {42 -- {41, 47 51}, 66 -- {61, 68 69}, 80 89 -- {78, 81, 94 99}}}"}, {"32 69 -- {17 -- {2 12 -- {0 1, 10, 13 14}, 25 -- {20 22, 30}}, 45 56 -- {35 40 -- {34, 39, 42}, 50 -- {47, 52}, 67 -- {57 63, 68}}, 86 -- {75 79 -- {72, 76, 83}, 89 93 -- {87, 91, 94}}}", "79", "56", "40", "57", "45", "32", "50", "69", "76", "20", "12", "87", "45", "32", "83", "56", "52", "25", "89", "2",