An anagram of a collection of letters is a word that is a

 

CS 32

 

Programming Assignment 4
Anagrams

 

 

An anagram of a collection of letters is a word that is a rearrangement of all the letters in that collection. For example, anagrams of “idte” are “diet”, “edit”, and “tide”. An anagram of “importunate” is “permutation”. An anagram of “excitation” is “intoxicate”.

Here is the interface for a class that stores words and lets you find anagrams:

 

	class Dictionary
	{
	  public:
	    Dictionary();
	    ~Dictionary();
	    void insert(std::string word);
	    void lookup(std::string letters, void callback(std::string)) const;
	};

 

As you would expect, the constructor creates an empty dictionary, and the insert function adds a word (stripped of any non-letters) to the dictionary. The lookup function takes a string and a callback function, and for every word in the dictionary that is an anagram of the letters in the string, it calls the callback function with that word as an argument. For example,

 

	void printWord(string w)
	{
	    cout << w << endl;
	}

	vector<string> v;  // global variable

	void appendWord(string w)
	{
	    v.push_back(w);
	}

	int main()
	{
	    Dictionary d;
	    d.insert("cat");
	    d.insert("dog");
	    d.lookup("tca", printWord);  // writes  cat
	    assert(v.empty());
	    d.lookup("tca", appendWord);
	    assert(v.size() == 1  &&  v[0] == "cat");
	}

 

We have written a correct but horridly inefficient Dictionary implementation. Your assignment is to write a more efficient correct implementation. If you wish, you can do this by starting with our implementation in Dictionary.cpp (the only source file you will modify and turn in), and changing the data structures and algorithms that implement the class and its member functions. You are free to add, change, or even delete classes or functions in Dictionary.cpp if you want to.

Correctness will count for 40% of your score, although if you turn in a correct implementation that is no faster than the horridly inefficient one we gave you, you will get zero correctness points (since you could just as well have turned in that same horridly inefficient program).

Of course, we have to give you some assumptions about the way clients will use Dictionary so that you know what needs to be faster. The client may call insert tens of thousands of times. The collection of letters for which we want to find all the anagrams is typically about four to ten letters, but may well be more than that. Your program should be able to process tens of thousands of requests for anagrams quickly.

Performance will count for 50% of your score (and your report for the remaining 10%). To earn any performance points, your program must be correct. (Who cares how fast a program is if it produces incorrect results?) This is a critical requirement — you must be certain your program produces the correct results and does not do anything undefined. Given that you are starting from a correct program, this should be easier than if you had to start from scratch. The faster your program performs on the tests we make, the better your performance score.

When we do the performance tests on your program, we will create a dictionary, insert a bunch of words, start the clock, do a lot of lookups, stop the clock, and destroy the dictionary. This means that your insertions don’t have to be fast (within reason) as they build the data structures required for efficient lookups.

To give you a taste of how fast the program can be, we did this project and produced this Windows program, this Mac program, and this Linux program. Put the executable file in the same directory as the words.txt file we supply in anagrams.zip, and try the program on words.txt with an collection of letters like “intoxicate” or “Veronica Snot”.

When you believe your implementation is correct, and you’re ready to test its correctness and speed, transfer it to the SEASnet Linux server. On that server, run this command:

 

curl -s http://cs.ucla.edu/classes/spring15/cs32/Utilities/p4tester | bash

 

It will deposit several files in the current directory, then build and run correctness tests and a performance test. If your implementation does not pass the correctness tests, it will not earn any performance points. Our solution passes the basic and thorough correctness tests and runs in about 10 milliseconds. While the exact performance scoring has not yet been determined, we expect something approximately like the following: times below 20 ms would earn the full 50 performance points; below 50 ms, 45 points; below 100 ms, 40 points; below 200 ms, 35 points; below 400 ms, 30 points; below 8000 ms, 25 points. For implementations that take longer than about 8 seconds, we will have to run a limited performance test, for which it’s hard to say in advance what the scoring will be.

Here are some requirements your program must meet:

 

  • You must not change Dictionary.h in any way. (In fact, you will not turn that file in; when we test your program, we’ll use ours.) You can change Dictionary.cpp however you want, subject to this spec. (Notice that by giving Dictionary just one private data member, a pointer to a DictionaryImpl object (which you can define however you want in Dictionary.cpp), we have given you free rein for the implementation while still preventing you from changing the interface in any way. This is an example of what is known as the pimpl idiom (from “pointer-to-implementation”).

  • You may design interesting data structures of your own, but the only containers from the C++ standard library you may use are vector, list, stack, and queue (and string). If you want anything fancier, implement it yourself. (It’ll be a good exercise for you.) If you decide to use a hash table, it must not have more than 50000 buckets. Although we’re limiting your use of containers from the library, you are free to use library algorithms (e.g., sort).

  • The Dictionary::lookup function must call the provided callback function once for every word that is an anagram of the given collection of letters. If more than one word is an anagram of that collection, the order in which those words are passed to the successive calls to the callback function is up to you. For example,

    	Dictionary dict;
    	dict.insert("cat");
    	dict.insert("act");
    	dict.lookup("cat", f);  // f is the name of some function
    

    will call either f("cat") first and f("act") second, or f("act") first and f("cat") second, your choice.

  • Duplicate words are allowed in the input, and each instance of a duplicate counts as an anagram. For example,

    	Dictionary dict;
    	dict.insert("cow");
    	dict.insert("cow");
    	dict.lookup("woc", f);  // f is the name of some function
    

    will call f("cow") twice, since “cow” appears twice in the dictionary. This requirement makes things easier for you, since there’s nothing special for you to check.

  • During execution, your program must not perform any undefined actions, such as dereferencing a null or uninitialized pointer.

 

Turn it in

 

By Wednesday, June 3, there will be a link on the class webpage that will enable you to turn in your source files and report. You will turn in a zip file containing two files:

 

  • Dictionary.cpp. You will not turn in Dictionary.h or a main routine, since we’ll supply our own.

  • report.doc, report.docx, or report.txt, a report containing

    • a description of your algorithms and data structures (good diagrams may help reduce the amount you have to write), and why you made the choices you did. You can assume we know all the data structures and algorithms discussed in class and their names.

    • pseudocode for non-trivial algorithms.

    • a note about any known bugs, serious inefficiencies, or notable problems you had.

 

Your report will count for 10% of your score. We’ll be reading it with this in mind: If you were standing around a whiteboard talking about your design and gave this description, would a CS grad student know enough about what you had in mind to be able to code it up without asking you any further questions?

 

Performance Testing

 

By default under Visual C++, when you build an executable file, it builds it in the Debug configuration. This is nice when you’re not yet sure your program is correct, because the compiler inserts extra code to support giving you extra information if your program crashes. This extra code takes time to execute, however.

When you are sure your program is correct, and you’d like to see how fast it can run, you’ll want to build your program in the Release configuration. To do this in Visual C++:

 

  1. At the top of main.cpp (outside of the main function), add the lines

    	#if defined(_MSC_VER)
    	#include <iostream>
    	#include <windows.h>
    	#include <conio.h>
    
    	struct KeepWindowOpenUntilDismissed
    	{
    	    ~KeepWindowOpenUntilDismissed()
    	    {
    	        DWORD pids[1];
    	        if (GetConsoleProcessList(pids, 1) == 1)
    	        {
    	            std::cout << "Press any key to continue . . . ";
    	            _getch();
    	        }
    	    }
    	} keepWindowOpenUntilDismissed;
    	#endif
    

    (You can leave these lines in even if you go back to running in Debug mode or run under clang++ or g++.)

  2. In the BUILD menu, select Configuration Manager.

  3. In the drop-down list under Active Solution Configuration, select Release instead of Debug, and then close that dialog.

 

Under Xcode, select Product / Build For / Profiling to turn on optimization.

Under Linux, the -O2 to the g++ command turns on optimization; for example, to produce an optimized executable named proj4:

 

	g++ -O2 -o proj4 *.cpp

 

Get 20% Discount on This Paper
Pages (550 words)
Approximate price: -

Try it now!

Get 20% Discount on This Paper

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Custom Writings Help is a Quality-Oriented Company in Online Writing as a result of hiring exceptional professionals to execute clients' tasks.

Essays

Research Papers

At Custom Writings Help,We understand the struggle of research paper writing, and that is why at Custom WritingS Help, we are all out to help you. We pride ourselves on having a team of clinical writers. The stringent and rigorous vetting process ensures that only the 'BEST' Writers are chosen for the job. We have highly qualified Ph.D. and MA writers working with us; we equally offer these experienced writers specific bonuses and incentives to make them deliver highly original, unique, and informative content at reasonably low prices.

Admissions

Thesis Writing Service

Worlwide, Many Masters Students are struggling with Thesis Completion. A thesis is likely to be the longest and most challenging piece of work a student has ever completed. However, unlike essays and other assignments, a student can pick a particular interest topic and work on their initiative. Fortunately, we are there for you. At Custom Writings Help, you are assured of an authentic, imaginative, informative, linguistically great, and advantageous thesis that adheres to all your needs. So, why continue considering different writers when you have discovered the best in the field?

Editing

Custom Papers

Not a single student can avoid writing custom papers. However, a total lack of experience, skills, and time makes it very hard to produce a superb writing piece. Therefore, if you are seeking professional help, turn to us. Our specialized and experienced writers compose a variety of model papers, including custom essays, college term papers, research papers, book reports, MBA essays, executive summaries, dissertations, Ph.D. theses, admission essays, and research proposals for college and university students at any level.

Coursework

Essay Writing

Most of the students disregard the critical principles of essay writing and compose papers below sensible guidelines. Therefore, with Custom Writings Help, one should not worry about his/her essay. Our Writers compose informative and engaging content on all complexities and topics. We write meaningful and smart essays while prioritizing all aspects that bring about a good grade, such as impeccable grammar, proper structure, zero-plagiarism, and conformance to guidelines.

Coursework

Coourse Work Writing

Don't let the seemingly never-ending onslaught of writing assignments get you down. If you are looking where to get course work assistance online, the writers at Custom Writings Help are here to assist you with all of your writing needs. We undertake to unique delivery of papers that meet the professor's requirements. The content is proofread, edited, and checked plagiarism before submission to customers. No matter how big or small your work is, we will deliver on time. Try US Now! !

Coursework

Dissertation Writing Service

High-Quality Dissertation Writing Services are rare. They require Ph.D. academicians – not easily found. However, are an exception. The years, time, and resources we have invested in the dissertation world has given us a competitive advantage over others. Choose to come to Custom Writings Help; You will find perfect Ph.D. consultants who have written hundreds of dissertations theses ready to help you. Let our dissertation-writing services help you craft your dissertation, for you are assured we will give you the results.