001package algs11;
002
003import java.util.Arrays;
004import stdlib.*;
005
006/**
007 * This is a skeleton file for your homework. Edit the sections marked TODO. You
008 * may also edit the function "main" to test your code.
009 *
010 * You must not change the declaration of any method. This will be true of every
011 * skeleton file I give you.
012 *
013 * For example, you will get zero points if you change the line
014 * 
015 * <pre>
016 *     public static int minPosition (double[] list) {
017 * </pre>
018 * 
019 * to something like
020 * 
021 * <pre>
022 *     public static double minPosition (double[] list) {
023 * </pre>
024 * 
025 * or
026 * 
027 * <pre>
028 *     public static int minPosition (double[] list, int i) {
029 * </pre>
030 * 
031 * Each of the functions below is meant to be SELF CONTAINED. This means that
032 * you should use no other functions or classes. You should not use any HashSets
033 * or ArrayLists, or anything else! In addition, each of your functions should
034 * go through the argument array at most once. The only exception to this the
035 * java Math.abs. You can use the Math.abs function to compute the absolute
036 * value.
037 */
038public class MyFirstHomeworkFor300 {
039
040        /**
041         * minValue returns the minimum value in an array of doubles. You can assume the
042         * array is nonempty and has no duplicates. Your solution must go through the
043         * array exactly once. Your solution must not call any other functions. Here are
044         * some examples (using "==" informally):
045         *
046         * <pre>
047         *   -7  == minValue (new double[] { -7 })
048         *    1  == minValue (new double[] { 1, 7, 8, 11 })
049         *   -7  == minValue (new double[] { 1, -4, -7, 7, 8, 11 })
050         *   -13 == minValue (new double[] { -13, -4, -7, 7, 8, 11 })
051         *   -13 == minValue (new double[] { 1, -4, -7, 7, 8, 11, -13 })
052         * </pre>
053         */
054        public static double minValue(double[] list) {
055                return StdRandom.random(); // TODO: remove this line, add correct answer
056        }
057
058        /**
059         * minPosition returns the position of the minimum value in an array of doubles.
060         * The first position in an array is 0 and the last is the array.length-1.
061         *
062         * You can assume the array is nonempty and has no duplicates. Your solution
063         * must go through the array exactly once. Your solution must not call any other
064         * functions. Here are some examples (using "==" informally):
065         *
066         * <pre>
067         *   0 == minPosition(new double[] { -7 })
068         *   2 == minPosition(new double[] { 1, -4, -7, 7, 8, 11 })
069         *   0 == minPosition(new double[] { -13, -4, -7, 7, 8, 11 })
070         *   6 == minPosition(new double[] { 1, -4, -7, 7, 8, 11, -9 })
071         * </pre>
072         */
073        public static int minPosition(double[] list) {
074                return StdRandom.uniform(100); // TODO: remove this line, add correct answer
075        }
076
077        /**
078         * distanceBetweenMinAndMax returns difference between the minPosition and the
079         * maxPosition in an array of doubles.
080         *
081         * You can assume the array is nonempty and has no duplicates. Your solution
082         * must go through the array exactly once. Your solution must not call any other
083         * functions. Here are some examples (using "==" informally):
084         *
085         * <pre>
086         *   0 == distanceBetweenMinAndMax(new double[] { -7 })                      // -7,-7 are the min and max
087         *   3 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11 }),    // -7,11
088         *   5 == distanceBetweenMinAndMax(new double[] { -13, -4, -7, 7, 8, 11 })   // -13,11
089         *   1 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11, -9 }) // -9,11
090         * </pre>
091         */
092        public static int distanceBetweenMinAndMax(double[] list) {
093                return StdRandom.uniform(100); // TODO: remove this line, add correct answer
094        }
095
096        /**
097         * A test program, using private helper functions. See below. To make typing
098         * tests a little easier, I've written a function to convert strings to arrays.
099         * See below.
100         */
101        public static void main(String[] args) {
102                // for minValue: array must be nonempty with unique elements
103                testMinValue(-7, "1 -4 -7 7 8 11 9 -5");
104                testMinValue(-0.5, "0.2 -0.5 -0.1");
105                testMinValue(9, "11 9 21 31 41");
106                testMinValue(9, "11 21 9 31 41");
107                testMinValue(9, "11 21 31 9 41");
108                testMinValue(9, "11 21 31 41 9");
109                testMinValue(9, "9 11 21 31 41");
110                testMinValue(-99, "-11 -99 -21 -31 -41");
111                testMinValue(-99, "-11 -21 -99 -31 -41");
112                testMinValue(-99, "-11 -21 -31 -99 -41");
113                testMinValue(-99, "-11 -21 -31 -41 -99");
114                testMinValue(-99, "-99 -11 -21 -31 -41");
115                testMinValue(11, "11");
116                testMinValue(-11, "-11");
117
118                // for minPosition: array must be nonempty with unique elements
119                testMinPosition(2, "1 -4 -7 7 8 11 9 -5");
120                testMinPosition(1, "0.2 -0.5 -0.1");
121                testMinPosition(1, "11 9 21 31 41");
122                testMinPosition(2, "11 21 9 31 41");
123                testMinPosition(3, "11 21 31 9 41");
124                testMinPosition(4, "11 21 31 41 9");
125                testMinPosition(0, "9 11 21 31 41");
126                testMinPosition(1, "-11 -99 -21 -31 -41");
127                testMinPosition(2, "-11 -21 -99 -31 -41");
128                testMinPosition(3, "-11 -21 -31 -99 -41");
129                testMinPosition(4, "-11 -21 -31 -41 -99");
130                testMinPosition(0, "-99 -11 -21 -31 -41");
131                testMinPosition(0, "11");
132                testMinPosition(0, "-11");
133
134                // for distanceBetweenMinAndMax: array must be nonempty with unique elements
135                testDistanceBetweenMinAndMax(3, "1 -4 -7 7 8 11 9 -5");
136                testDistanceBetweenMinAndMax(3, "0.1 -0.4 -0.7 0.7 0.8 1.1 0.9 -0.5");
137                testDistanceBetweenMinAndMax(4, "9 11 21 31 41");
138                testDistanceBetweenMinAndMax(3, "11 9 21 31 41");
139                testDistanceBetweenMinAndMax(2, "11 21 9 31 41");
140                testDistanceBetweenMinAndMax(1, "11 21 31 9 41");
141                testDistanceBetweenMinAndMax(1, "11 21 31 41 9");
142                testDistanceBetweenMinAndMax(4, "9 -11 -21 -31 -41");
143                testDistanceBetweenMinAndMax(3, "-11 9 -21 -31 -41");
144                testDistanceBetweenMinAndMax(2, "-11 -21 9 -31 -41");
145                testDistanceBetweenMinAndMax(1, "-11 -21 -31 9 -41");
146                testDistanceBetweenMinAndMax(1, "-11 -21 -31 -41 9");
147                testDistanceBetweenMinAndMax(0, "11");
148                testDistanceBetweenMinAndMax(0, "-11");
149
150                StdOut.println("Finished tests");
151        }
152
153        /*
154         * A main function for debugging -- change the name to "main" to run it (and
155         * rename the existing main method to something else). Change the test as
156         * appropriate.
157         */
158        public static void main1(String[] args) {
159                Trace.drawStepsOfMethod("minValue");
160                Trace.run();
161                testMinValue(9, "11 21 9 31 41");
162        }
163
164        /* Test functions --- lot's of similar code! */
165        private static void testMinValue(double expected, String list) {
166                double[] aList = doublesFromString(list);
167                double actual = minValue(aList);
168                if (!Arrays.equals(aList, doublesFromString(list))) {
169                        StdOut.format("Failed minValue([%s]): Array modified\n", list);
170                }
171                if (expected != actual) {
172                        StdOut.format("Failed minValue([%s]): Expecting (%.1f) Actual (%.1f)\n", list, expected, actual);
173                }
174        }
175
176        private static void testMinPosition(int expected, String list) {
177                double[] aList = doublesFromString(list);
178                int actual = minPosition(aList);
179                if (!Arrays.equals(aList, doublesFromString(list))) {
180                        StdOut.format("Failed minPosition([%s]): Array modified\n", list);
181                }
182                if (expected != actual) {
183                        StdOut.format("Failed minPosition([%s]): Expecting (%d) Actual (%d)\n", list, expected, actual);
184                }
185        }
186
187        private static void testDistanceBetweenMinAndMax(int expected, String list) {
188                double[] aList = doublesFromString(list);
189                int actual = distanceBetweenMinAndMax(aList);
190                if (!Arrays.equals(aList, doublesFromString(list))) {
191                        StdOut.format("Failed distanceBetweenMinAndMax([%s]): Array modified\n", list);
192                }
193                if (expected != actual) {
194                        StdOut.format("Failed distanceBetweenMinAndMax([%s]): Expecting (%d) Actual (%d)\n", list, expected,
195                                        actual);
196                }
197        }
198
199        /* A utility function to create an array of doubles from a string. */
200        // The string should include a list of numbers, separated by single spaces.
201        private static double[] doublesFromString(String s) {
202                if ("".equals(s))
203                        return new double[0]; // empty array is a special case
204                String[] nums = s.split(" ");
205                double[] result = new double[nums.length];
206                for (int i = nums.length - 1; i >= 0; i--) {
207                        try {
208                                result[i] = Double.parseDouble(nums[i]);
209                        } catch (NumberFormatException e) {
210                                throw new IllegalArgumentException(
211                                                String.format("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i]));
212                        }
213                }
214                return result;
215        }
216}