Ad
  • Custom User Avatar

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

  • Default User Avatar

    It's a couple of operation carried out one after another. You know, same as all other code?

  • Custom User Avatar

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

  • Default User Avatar
  • Custom User Avatar

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

  • Default User Avatar

    And now I've read something new ... the asterisk (splat or unpack operator) will iterate over an iterable and serve one element at a time. Using a double-asterisk will iterate over a key-value iterable and serve up one key-value pair at a time.

    Point made earlier that this is clever but not necessarily the most readable.

  • Custom User Avatar

    Why this isn't already published?

  • Custom User Avatar

    I'm sorry if I sounded agro. But random tests are not optional => issue.

  • Custom User Avatar

    I think it already was stated on line one. Why so agro?

  • Custom User Avatar

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

  • Custom User Avatar

    Please mark it as an issue.

  • Custom User Avatar

    You need random test, a lot. But you already know it.
    In this link you can see how to propperly make some tests in JavaScript. Random tests are, as the name says, random. Generate 500 random numbers to test with.

  • Custom User Avatar

    Quick note for people seeing this on the main page: this code is not related to this kata and is therefore not a spoiler. The code provides zero help to someone trying to solve the kata.

    I see that the kata is retired, but I wanted to make good on my promise to provide the explanation for this issue.

    Let's pretend we're making a kata that gives the user an array and asks them to return the array with each element incremented by 1. Here's our simplified test code:

    def solution(data):
        return [x+1 for x in data]    
        
    data = [4, 8, 15, 16, 23, 42]
    print("Data is {}".format(data))
    actual = user(data)
    print("User answer is {}".format(actual))
    print("Data is {}".format(data))
    expected = solution(data)
    print("Solution answer is {}".format(expected))
    test.assert_equals(actual, expected)
    

    The user could submit code like this and it would work, because it's a valid answer:

    def user(data):
        return [x+1 for x in data]
    

    The print lines from the test code will show:

    Data is [4, 8, 15, 16, 23, 42]
    User answer is [5, 9, 16, 17, 24, 43]
    Data is [4, 8, 15, 16, 23, 42]
    Solution answer is [5, 9, 16, 17, 24, 43]
    

    All good.

    Except it isn't, because we are sending an array to the user's solution and then we use that same array in the kata's solution. This means the user could modify the array in their code and then the kata's solution would use that modified array.

    This is only possible because arrays are passed by reference, unlike most other objects (integers, strings, etc), which are passed by value. See this article for a more comprehensive explanation.

    For example, the user could write this:

    def user(data):
        for i, e in enumerate(data):
            data[i] = 0
        return [1 for x in data]
    

    That sets every item in the array to 0 and then returns an array of 1s.

    The print lines from the test code will show:

    Data is [4, 8, 15, 16, 23, 42]
    User answer is [1, 1, 1, 1, 1, 1]
    Data is [0, 0, 0, 0, 0, 0]
    Solution answer is [1, 1, 1, 1, 1, 1]
    

    This is problematic for several reasons:

    • It could be exploited by malicious users, because they can turn a difficult kata into an easy one.
    • We didn't intend the kata to be solved in this way.
    • This issue can lead to confusion for new users.

    There's two main ways to solve this problem:

    • Calculate the correct answer, then run the user's code. It doesn't matter if the user changes the array, because we've already determined the right answer.
    • Give the user a copy of the array. It doesn't matter if the user changes the array, because it's only used by the user's code.

    Steffan153 has solved the problem using the second solution.

    This type of issue can occur in lots of languages, such as Python, C#, JavaScript, etc. So it's always worth checking beta kata's in case they have this issue, because then it can be fed back and resolved before the kata is approved.

  • Custom User Avatar

    As I said in the description the program was made in order to help me and actually worked (was writting menus in different languages).
    Thanks for the help, maybe next time.

  • Custom User Avatar

    except that in this case, the constraint is rather on 2 different columns that are left justified, each with it's own width. While here, you put a global constraint (mx) which wasn't making sense considering the expected formatting.

  • Loading more items...