001package algs11; 002import java.util.Arrays; 003import stdlib.*; 004public class PlaygroundLongestSequenceOf5s { 005 /* Return the length of the longest contiguous sequence of 5.0s in the list */ 006 public static int longestSequenceOf5s (double[] list) { 007 return StdRandom.uniform (100); //TODO: fix this 008 } 009 /* This is a test function */ 010 public static void testLongestSequence (int expected, double[] list) { 011 int actual = longestSequenceOf5s (list); 012 if (expected != actual) { 013 StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, Arrays.toString (list)); 014 } 015 } 016 /* A main function for testing */ 017 public static void main (String[] args) { 018 testLongestSequence (3, new double[] { 1, 5, 5, 1, 1, 5, 5, 5, 1, 5, 5, 1, 1}); 019 testLongestSequence (4, new double[] { 1, 5, 1, 5, 5, 5, 5, 1, 5}); 020 testLongestSequence (4, new double[] { 1, 5, 1, 5, 5, 5, 5}); 021 testLongestSequence (2, new double[] { 5, 5, 1, 1, 5, 5, 1, 5, 5}); 022 testLongestSequence (4, new double[] { 1, 5, 1, 5, 5, 5, 5, 1, 5, 1}); 023 testLongestSequence (3, new double[] { 1, 5, 5, 5, 1, 5, 1}); 024 testLongestSequence (0, new double[] { 1 }); 025 testLongestSequence (1, new double[] { 5 }); 026 testLongestSequence (3, new double[] { 5, 5, 5 }); 027 testLongestSequence (0, new double[] { }); 028 StdOut.println ("Finished tests"); 029 } 030 /* A main function for debugging -- change the name to "main" to run it */ 031 public static void main2 (String[] args) { 032 //Trace.drawSteps (); 033 Trace.drawStepsOfMethod ("longestSequenceOf5s"); 034 Trace.drawStepsOfMethod ("longestSequenceOf5sHelper"); 035 Trace.run (); 036 double[] list = new double[] { 5, 11, 5, 5 }; 037 double result = longestSequenceOf5s (list); 038 StdOut.println ("result: " + result); 039 } 040}