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 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

    Find the sum of all multiples of n

  • 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.

  • Custom User Avatar

    Not 100% sure since I'm just getting into swift myself but I'm pretty sure what is happening is that even though your float is showing as 126.0, it's actually something like 125.9999 and when you truncate to an Int it just chops off the .999

    I had a similar issue. I tried to cheat fix by just adding 1 to specific examples that were off by 1. That worked for all but one of the examples.

    Out put was telling me that 159 was the right answer when I got 158. When I added 1 to my answer it swapped and told me that 158 was the right answer now that I was returning 159.

    So bizarre. I didn't realize my output could affect the correct results.

  • Custom User Avatar

    Because in an application use, an extention can be used globally without any issues or headaches. Simply call the exention function on any Int and you're done.

  • Custom User Avatar

    Try defining result at the start of the function.
    Also, you need to say that the function return a string so you should have func switchItUp(_ number: Int) -> String when defining the function.

  • Custom User Avatar

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

  • Loading more items...