001package algs13;
002import stdlib.*;
003
004@SuppressWarnings("all")
005public class XWhatGoesWrongInLoopsAndRecursion {
006        private Node first = null;
007        static class Node {
008                public Node (double item, Node next) { this.item = item; this.next = next; }
009                double item;
010                Node next;
011        }
012
013        /*
014         * numFives returns the number of fives in an array, written with a loop
015         */
016        public static int numFives0 (double[] a) {
017                int result = 0;
018                for (int i=0; i<a.length; i++)
019                        if (a[i] == 5.0)
020                                result++;               
021                return result;
022        }
023
024        public static int numFives1 (double[] a) {
025                double[] list = new double[] { 4, 5, 6, 5, 3 };
026                int result = 0;
027                for (int i=0; i<list.length; i++)
028                        if (list[i] == 5.0)
029                                result++;               
030                return result;
031        }
032
033        public static int numFives2 (double[] a) {
034                int result = 0;
035                for (int i=0; i<a.length; i++) {
036                        if (a[i] == 5.0)
037                                result++;       
038                        else
039                                return result;
040                        return result;
041                }
042                StdOut.print (result); 
043                return 0;//added to remove compiler error
044        }
045
046        public static int numFives3 (double[] a) {
047                int result = 0;
048                for (int i=0; i<a.length; i++)
049                        if (a[i] == 5.0)
050                                result++;               
051                        else
052                                i++;
053                return result;
054        }
055
056        public static int numFives4 (double[] a) {
057                int result = 0;
058                for (int i=0; i<a.length; i++)
059                        if (i == 5.0)
060                                result++;               
061                return result;
062        }
063
064        public static int numFives5 (double[] a) {
065                int result = 0;
066                for (int i=0; i<a.length; i++)
067                        //if (a == 5.0)
068                        result++;               
069                return result;
070        }
071
072        public static int numFives6 (double[] a) {
073                int result = 0;
074                for (int i=0; 0<a.length; i++)
075                        if (a[i] == 5.0)
076                                result++;               
077                return result;
078        }
079
080        public static int numFives7 (double[] a) {
081                int result = 0;
082                for (int i=0; i>0; i++)
083                        if (a[i] == 5.0)
084                                result++;               
085                return result;
086        }
087
088        /*
089         * numFours returns the number of fours in an array, written recursively
090         */
091        public static int numFours0 (double[] a) {
092                return numFours0 (a, 0, 0);
093        }
094        private static int numFours0 (double[] a, int i, int result) {
095                if (i>a.length) 
096                        return result;
097                if (a[i] == 4.0) 
098                        return numFours0 (a, i+1, result+1);
099                else
100                        return numFours0 (a, i+1, result);
101        }
102
103        public static int numFours1 (double[] a) {
104                return numFours1 (a, 0);
105        }
106        private static int numFours1 (double[] a, int i) {
107                if (i>a.length) 
108                        return 0;
109                if (a[i] == 4.0) 
110                        return 1 + numFours1 (a, i+1);
111                else
112                        return numFours1 (a, i+1);
113        }
114
115        public static int numFours00 (double[] a) {
116                return numFours00 (a, 0, 0);
117        }
118        private static int numFours00 (double[] a, int i, int result) {
119                if (i>a.length) 
120                        return result;
121                if (a[i] == 4.0) 
122                        result = result+1;
123                result = numFours00 (a, i+1, result);
124                return result;
125        }
126
127        public static int numFours11 (double[] a) {
128                return numFours11 (a, 0);
129        }
130        private static int numFours11 (double[] a, int i) {
131                if (i>a.length) 
132                        return 0;
133                int result = numFours11 (a, i+1);
134                if (a[i] == 4.0) 
135                        result = result+1;
136                return result;
137        }
138
139        /*
140         * numThrees returns the number of threes in a linked list
141         */
142        public int numThrees0 () {
143                int result = 0;
144                for (Node x = first; x != null; x = x.next) 
145                        if (x.item == 3.0)
146                                result = result + 1;
147                return result;
148        }
149        public int numThrees00 () {
150                Node x = first;
151                int result = 0;
152                while (x != null) {
153                        if (x.item == 3.0)
154                                result = result + 1;
155                        x = x.next; 
156                }
157                return result;
158        }
159
160        
161        public int numThrees1 () {
162                Node x = first;
163                int result = 0;
164                while (x != null) {
165                        if (x.item == 3.0)
166                                result = result + 1;
167                        x = x.next.next; 
168                }
169                return result;
170        }       
171        
172        public int numThrees3 () {
173                if (first == null) return 0;
174                int result = 0;
175                if (first.item == 3.0) 
176                        result = result + 1;
177                Node x = first;
178                while (x.next != null) {
179                        if (x.next.item == 3.0)
180                                result = result + 1;
181                        x.next = x.next.next; 
182                }
183                return result;
184        }       
185        public int numThrees2 () {
186                if (first == null) return 0;
187                int result = 0;
188                if (first.item == 3.0) 
189                        result = result + 1;
190                Node x = first;
191                while (x.next != null) {
192                        if (x.next.item == 3.0)
193                                result = result + 1;
194                        x = x.next; 
195                }
196                return result;
197        }
198        public int numThrees4 () {
199                if (first == null) return 0;
200                int result = 0;
201                if (first.item == 3.0) 
202                        result = result + 1;
203                Node x = first;
204                while (x.next != null) {
205                        if (x.item == 3.0)
206                                result = result + 1;
207                        x = x.next; 
208                }
209                return result;
210        }       
211
212        public int numThrees20 () {
213                if (first.next == null) return 0;
214                Node x = first;
215                int result = 0;
216                while (x.next != null) {
217                        if (x.item != 3.0) x = x.next;
218                        if (x.item == 3.0) {
219                                result = result + 1;
220                                x = x.next; 
221                        }
222                }
223                if (x.next == null && x.item == 3.0) {
224                        result = result + 1;
225                }
226                return result;
227        }
228
229
230
231        public static void main (String[] args) {
232        }
233
234}