001package algs13;
002import java.text.DecimalFormat;
003import stdlib.*;
004public class PlaygroundNumUnique {
005        private Node first;
006        static class Node { 
007                public double item; 
008                public Node next; 
009                public Node (double item, Node next) { 
010                        this.item = item; 
011                        this.next = next; 
012                }
013        }
014        
015        /** Number of unique items, assuming the list is sorted */
016        public int numUnique() {
017                return StdRandom.uniform (100); //TODO: fix this
018        }
019
020        public static void main1 (String[] args) {
021                Trace.showBuiltInObjects(true);
022                Trace.drawStepsOfMethod ("numUnique");
023                Trace.run ();
024                PlaygroundNumUnique list1 = PlaygroundNumUnique.from ("11 21 21 21 31");
025                int result1 = list1.numUnique ();
026                Trace.draw();
027                StdOut.println ("result: " + result1);
028        }
029
030        public static void main (String[] args) {
031                testNumUnique(4, "11 21 21 21 31 41 41 41 41");
032                testNumUnique(1, "11 11 11 11");
033                testNumUnique(4, "11 21 31 41");
034                testNumUnique(4, "11 11 11 21 31 31 31 31 41");
035                testNumUnique(4, "11 11 21 21 21 31 31 41 41 41 41");
036                testNumUnique(8, "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71 81 81");
037                testNumUnique(8, "11 21 31 41 41 41 41 41 51 51 61 71 81");
038                testNumUnique(7, "11 11 11 11 21 31 41 41 41 41 41 51 51 61 71");
039                testNumUnique(7, "11 21 31 41 41 41 41 41 51 51 61 71");
040                testNumUnique(8, "-81 -81 -81 -81 -71 -61 -51 -51 -51 -51 -41 -41 -31 -21 -11 -11 -11");
041                testNumUnique(3, "-11 -11 -11 0 0 11 11 11");
042                testNumUnique(2, "0 11 11 11");
043                testNumUnique(2, "-Infinity 11 11 11");
044                testNumUnique(2, "11 11 11 Infinity");
045                testNumUnique(1, "11 11");
046                testNumUnique(1, "11");
047                testNumUnique(0, "");
048                
049                StdOut.println ("Finished tests");
050        }
051
052        private static void testNumUnique (int expected, String sList) {
053                PlaygroundNumUnique list = PlaygroundNumUnique.from (sList); 
054                String sStart = list.toString ();
055                int actual = list.numUnique ();
056                if (expected != actual) {
057                        StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, list);
058                }
059                String sEnd = list.toString ();
060                if (! sStart.equals (sEnd)) {
061                        StdOut.format ("Failed %s.numUnique(): List changed to %s\n", sStart, sEnd);
062                }
063        }
064
065        /* ToString method to print */
066        public String toString () { 
067                // Use DecimalFormat #.### rather than String.format 0.3f to leave off trailing zeroes
068                DecimalFormat format = new DecimalFormat ("#.###");
069                StringBuilder result = new StringBuilder ("[ ");
070                for (Node x = first; x != null; x = x.next) {
071                        result.append (format.format (x.item));
072                        result.append (" ");
073                }
074                result.append ("]");
075                return result.toString ();
076        }
077
078        /* Method to create lists */
079        public static PlaygroundNumUnique from(String s) {
080                PlaygroundNumUnique result = new PlaygroundNumUnique ();
081                if ("".equals (s)) return result;
082
083                Node first = null;
084                String[] nums = s.split (" ");
085                for (int i = nums.length-1; i >= 0; i--) {
086                        try { 
087                                double num = Double.parseDouble (nums[i]); 
088                                first = new Node (num, first);      
089                        } catch (NumberFormatException e) {
090                                throw new IllegalArgumentException (String.format ("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i]));
091                        }
092                }
093                result.first = first;
094                return result;
095        }
096}