DOC PREVIEW
Duke CPS 100E - Anagrams/Jumbles

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Anagrams/JumblesBrute force? SillyAnagrams.javaQuantifying brute force for anagramsJohn von NeumannToward a faster anagram finderAnother anagram methodOO and JavaUnderstandable, extensible?Find all anagrams in dictionaryAnaword objects with optionsAnagram: Using NormalizersNormalizer hierarchyCPS 1003.1Anagrams/JumblesHow do humans solve puzzles like that at www.jumble.comIs it important to get computers to solve similar puzzles? Reasons?Should computers mimic humans in puzzle-solving, game playing, etc.? Lessons from chess?nelir,nelri, neilr, neirl, nerli, neril, nleir, nleri, nlier, nlire, nlrei, nlrie, nielr, nierl, niler, nilre, nirel, … lenir, lenri, leinr, leirn, lerni, lerin, linerWhat’s the problem here?CPS 1003.2Brute force? SillyAnagrams.javapublic String[] allAnagrams(String s) { int anaCount = factorial(s.length()); Set anagrams = new TreeSet(); ArrayList list = new ArrayList(); for(int k=0; k < s.length(); k++){ list.add(s.substring(k,k+1)); } while (anagrams.size() != anaCount){ Collections.shuffle(list); anagrams.add(listToString(list)); } return (String[]) anagrams.toArray(new String[0]);}CPS 1003.3Quantifying brute force for anagramsAll anagrams of "compute" takes average of 1 second over 20 trials. How long will "computer" take? Why?What is worst case time?What is best case time?We’re willing to do some pre-processing to make the time to find anagrams quickerOften find that some initialization/up-front time or cost saves in the long runWhat properties do words share that are anagrams?CPS 1003.4John von Neumann“Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin.”“There's no sense in being precise when you don't even know what you're talking about. ““There are two kinds of people in the world: Johnny von Neumann and the rest of us.”Eugene Wigner, Noble PhysicistCPS 1003.5Toward a faster anagram finderWords that are anagrams have the same letters; use a letter fingerpr int or sig nature/hi s togram to help find anagramsCount how many times each letter occurs:“teacher” 1 0 1 0 2 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0“cheater” 1 0 1 0 2 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0Store words, but use fingerprint for comparison when searching for an anagramHow to compare fingerprints using .equals()How to compare fingerprints using .compareTo()How do we make client programmers unaware of fingerprints? Should we do this?CPS 1003.6Another anagram methodInstead of fingerprint/histogram idea, use sorted form of word“gable” and “bagel” both yield “abegl”Anagrams share same sorted formSimilarities/differences to histogram/fingerprint idea?Both use canonical or normal/normalized formNormalized form used for comparison, not for printingWhen should this normal form be created?When is one method preferred over the other?Big words, little words? Different alphabets? DNA vs English?CPS 1003.7OO and JavaWe’ll use an adapter or wrapper class called Anaword instead of StringClients can treat Anaword objects like strings, but the objects are better suited for finding anagrams than stringsThe Anaword for “bear” prints as “bear” but compares to other Anaword objects as 11001000000000000100000000In Java change behavior with .toString() and .equals()No overloaded operators as in C++•Exception is +, this works for strings, but can't change itWhen string needed, automatically call toString()CPS 1003.8Understandable, extensible?The code does things simply, but isn't very OO. Why is simple sometimes better? Why is it worse?void printAll(Anaword[] list, Anaword target){ System.out.print("anagrams of "+target+": "); for(int k=0; k < list.length; k++){ if (target.equals(list[k])) { System.out.print(list[k]); } } System.out.println();}CPS 1003.9Find all anagrams in dictionaryIf we sort the dictionary what will happen to the anagrams?capitol optical topicaldanger gander garden rangedlameness maleness nameless salesmenHow can we overload .equals()?Look at "danger" or 1001101000000100010….How can we sort with Collections.sort or Arrays.sortElements sorted must be comparable/sortableMust implement the java.lang.Comparable interface•Return negative, zero, positive number depending on less than, equal to, or greater than•What is method signature?CPS 1003.10Anaword objects with optionsCan we use different canonical forms in different contexts?Could have Anaword, FingerPrintAnaword, SortAnawordWhat possible issues arise? What behavior is different in subclasses?•If there’s no difference in behavior, don’t have subclassesAlternative, make canonical/normalize method a classTurn a function/idea into a class, then let the class vary to encapsulate different methodsNormalization done at construction time or laterWhere is normalizer object created? When?CPS 1003.11Anagram: Using NormalizersHow can we normalize an Anaword object differently?Call normalize explicitly on all Anaword objectsHave Anaword objects normalize themselvesAdvantages? Disadvantages?If Anaword objects normalize themselves, how can we experiment with different normalization techniques?Gut and paste. Problems? Versions? Saved code?What about using save-as and several .java files?What about deciding at runtime on normalization?We need inheritance!CPS 1003.12Normalizer hierarchyAnaword objects normalize themselvesWhere does the normalizer come from?•Passed in at construction time•Obtained from normalizer factory•Other approaches?How is Normalizer used?Normalizer is conceptually an interfaceDifferent implementations of the interface have different behavior (guts) but same skin (sort


View Full Document

Duke CPS 100E - Anagrams/Jumbles

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download Anagrams/Jumbles
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Anagrams/Jumbles and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Anagrams/Jumbles 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?