Ad
  • Custom User Avatar

    answered above

  • Custom User Avatar

    Thats awesome, knew about first but second was just gamechanger. Thank you

  • Custom User Avatar

    Depends. With simple exercises like this one, compact solutions are used in production. However, with more complex algorithms, code golfing is more of a past time. Self documenting and legible code is more desireable for professinal production code, as it's easier to read and maintain.

  • Custom User Avatar

    @Tobleron44 What language are you on? That sounds like an error in the description, since the correct output should be 1, 3, 5, 7, 9, 2, 6, 10, 8, 4.

    Also, when you start your second run through, you forgot to switch from yes to no. You end on 10 (no), so when you go back to 2, it should be a yes.

            Y N Y N Y N Y N Y N
    Input:  1 2 3 4 5 6 7 8 9 10
            |   |   |   |   |
    Output: 1,  3,  5,  7,  9
    
                           Y N Y N Y
    Input:                 2 4 6 8 10
                           |   |  _|
                           |  |  |
    Output: 1, 3, 5, 7, 9, 2, 6, 10
    
                                  N Y
                                  4 8
                                    |
    Output: 1, 3, 5, 7 9, 2, 6, 10, 8
    
    N
    4
    
    Output: (no change)
    
                                         Y
                                         4
                                         |
    Output:  1, 3, 5, 7, 9, 2, 6, 10, 8, 4
    
  • Custom User Avatar

    But that would mean:

    y n y n y n y n y n
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10

    1, 3, 5, 7, 9

    then we have

    N Y N Y N
    2, 4, 6, 8, 10

    1, 3, 5, 7, 9, 4, 8
    ...

    And in the description it says
    1, 3, 5, 7, 9, 2, 4, 6, 8, 4
    what am i missing?

  • Custom User Avatar
  • Custom User Avatar

    As I understand it, you need to write a function which takes an array, and returns another array. To populate the output array, go through the input, and select every other item. Once you reach the end, loop through the input array again with the same pattern, ignoring the items which were "selected" previously. However, when you restart the array, the decision to either include or exclude an item doesn't reset. So if you selected the last item, ignore the first item when you restart the array.

    For example, take the array [1, 2, 3, 4, 5]

    First, go through every other element, like so:

             Y  N  Y  N  Y
    input:  [1, 2, 3, 4, 5]
             |    _|  ___|
             |   |   | 
    output: [1,  3,  5]
    

    After "removing" these elements, we are left with [2, 4], so we repeate the same pattern. However, because our last runthrough ended with a "Yes", this new runthrough starts with a "No":

             N  Y
    input:  [2, 4]
                |_____
                      |
    output: [1, 3, 5, 4]
    

    Now, since only [2] is left in our input form, we add it to the end of the output, returning [1, 3, 5, 4, 2].

    Hopefully that makes a bit more sense.

  • Custom User Avatar

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

  • Custom User Avatar

    Are the most concise functions commmonly used in product code? Or is it generally more readable?

  • Custom User Avatar

    this makes a lot of sense... will definately take note of this.

  • Custom User Avatar

    You're not doing anything about the descending order.

  • Custom User Avatar

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

  • Custom User Avatar

    Find the sum of all multiples of n

  • Custom User Avatar

    This is a fantastic solution. Very well done!

  • Custom User Avatar

    False and 0 are different.

    Also, note that if you don't tell what's the input, what's the expected output and what's the result your code returned, nobody can help you.

  • Loading more items...