Ad
  • Custom User Avatar
  • Custom User Avatar

    There should be fixed test cases for multiple plateaus I think

  • Custom User Avatar
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Ok bud. Don't delete this kata, but can you add a better description please? Add examples when the data array comes with zeros and negative numbers. It will save time for many people. Thanks

  • Custom User Avatar

    For those wondering what the definition of a peak is in this question (which could be more specifically notes):
    The values immediately left and right of the "peak" have to be less than it.
    The plateau description is fine.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I did this in Java.

    In my opinion, the sample tests for this kata should be rewritten.

    Some of the test failure messages are the same even though what is being tested is different, making it difficult to tell which test you have failed. Critically, the tests do not test for arrays where values are repeated but are not plateaus; this isn't mentioned in the description either, so it's not clear that this is a problem until you go to attempt the kata and get failures there. At the very least, the description should be updated to account for this.

    I can understand why the tests have been conducted in a single @Test block, but I don't think it results in a well written test. Sure, it makes the code shorter, but shorter isn't always better if the result is confusing to anyone who has to look at the code later. Splitting them into seperate tests means the logs are also split, making debugging problems a whole lot easier, especially with the Codewars UI we are using here.

    I have included the tests I wrote below, and you can judge for yourself.

    import static org.junit.Assert.assertEquals;
    import org.junit.runners.JUnit4;
    import org.junit.FixMethodOrder;
    import org.junit.runners.MethodSorters;
    import java.util.*;
    import java.util.stream.Collectors;
    
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class SolutionTest {
    
      @Test
      public void a_shouldHandleSimplePeak(){
        int[] input = {1, 2, 1};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
    
      @Test
      public void b_shouldIgnorePeakAtBeginning(){
        int[] input = {2, 1, 2, 1};
        int[] pos = {2};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
    
      @Test
      public void c_shouldIgnorePeakAtEnd(){
        int[] input = {1, 2, 1, 2};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
    
      @Test
      public void d_shouldHandleMultiplePeaks(){
        int[] input = {1, 2, 1, 2, 1};
        int[] pos = {1, 3};
        int[] peaks = {2, 2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
      
      @Test
      public void e_shouldHandleSimplePlateau(){
        int[] input = {1, 2, 2, 2, 1};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
    
      @Test
      public void f_shouldIgnorePlateauAtBeginning(){
        int[] input = {2, 2, 1, 2, 1};
        int[] pos = {3};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void g_shouldIgnorePlateauAtEnd(){
        int[] input = {1, 2, 1, 2, 2};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void h_shouldHandleRepeatingValuesGoingUp(){
        int[] input = {1, 2, 2, 3, 2};
        int[] pos = {3};
        int[] peaks = {3};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void i_shouldHandleRepeatingValuesGoingDown(){
        int[] input = {1, 2, 2, 3, 2};
        int[] pos = {3};
        int[] peaks = {3};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void j_shouldHandleComplexPeaksAndPlateaus(){
        int[] input = {1, 2, 2, 3, 2, 2, 1, 0, 0};
        int[] pos = {3};
        int[] peaks = {3};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
    
      public Map<String, List<Integer>> getExpected (int[] pos, int[] peaks){
        return Map.of("pos", arrToList(pos), "peaks", arrToList(peaks));
      }
      public List<Integer> arrToList(int[] arr){
        return Arrays.stream(arr).boxed().collect(Collectors.toList());
      }
    
    }
    
  • Custom User Avatar

    Nice kata. Maybe the description should mention that the returned object needs to have the peaks sorted into the same order in which they occurred in the original array?

  • Custom User Avatar

    COBOL translation (author is inactive).

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    There might be something wrong with the random tests in php.

    I pass all tests except random, where I get a timeout (over 12000 ms), using my code elsewhere (online php sandbox) on a random array with 95 peaks, looped 100 times, results in: "Total execution time in seconds: 0.085011005401611". I have a for loop which contains two very short foreach loops for the before and after arrays. Both have breaks and the second does not get used if the first fails. There doesn't seem to be anything to optimise out.

    Logging to STDOUT, it seems that the random test fails at 293 arrays being generated, but without any of them actually passing through the function (I get 293 '1's and no arrays using fwrite(STDOUT, print_r($arr)); before my timeout, while passed tests give me one '1' per array, then the actual arrays are printed, as expected).

  • Custom User Avatar

    I am having a problem with the use of global variables, out the box my code works correctly, but here in the tests, it fails flat out in all the tests. I have tested with different versions of python and in all of them, it works correctly.

    [spellmell@nexus codewars]$ python3.8 15_pick_peaks.py
    [7, 9, 5, 0, 7, 9, 9, 8, 6]
    pos:[1, 5]
    peaks:[9, 9]
    python 3.8.12 (default, Aug 30 2021, 00:00:00)
    [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)]

    [spellmell@nexus codewars]$ python3.9 15_pick_peaks.py
    [7, 8, 9, 5, 4, 0, 4, 2, 3]
    pos:[2, 6]
    peaks:[9, 4]
    python 3.9.9 (main, Nov 19 2021, 00:00:00)
    [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)]

    [spellmell@nexus codewars]$ python3.10 15_pick_peaks.py
    [6, 1, 0, 3, 4, 8, 7, 2, 8]
    pos:[5]
    peaks:[8]
    python 3.10.1 (main, Dec 7 2021, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)]

    Here: Time: 500ms Passed: 0 Failed: 10 Exit Code: 1

    It is confirmed that the problem is the global variables, when removing them the code works.
    I was reading the "troubleshooting" page, and I see that there is some reference to the problem, but
    I can't understand. Any suggestions on how I can solve it?

  • Custom User Avatar
  • Loading more items...