Ad
  • Custom User Avatar

    Hi guys!

    I propose 1 more test to infeer if input parameters were changed (this way we get a more direct exposed information):

    describe("Solution", function(){
    let moves = ['up', 'left', 'right', 'left', 'left'];
    let position = [0,0]
    it("shouldn't change the input parameters", function(){
    streetFighterSelection(fighters, position, moves);
    assert.deepEqual(fighters, [["Ryu", "E.Honda", "Blanka", "Guile", "Balrog", "Vega"],
    ["Ken", "Chun Li", "Zangief", "Dhalsim", "Sagat", "M.Bison"]]);
    assert.deepEqual(position, [0,0]);
    assert.deepEqual(moves, ['up', 'left', 'right', 'left', 'left']);
    });
    });

    [Sorry, don't now how to preserve code formatation]

  • Custom User Avatar

    I figured out the issue and fixed it.

    The issue was if someone mutated the passed parameters the test would fail. So before calling the function I'm now cloning the parameters.

  • Custom User Avatar

    Hmm... I'm not exactly sure how to start this. Could I have a hint, please?

  • Custom User Avatar

    Interestingly, this is relevant to TypeScript version of the Kata and not relevant to its JavaScript version. Discovered it just now, after translating my JS solution to TS. In JS version I am mutating position. Mutating position in TS version results in random tests failure. After copying it to temp variable and using it, the test passed with the same logic.

  • Custom User Avatar

    Hmmm... Iterating through the list is better than popping off the list?
    Popping all the elements off a list takes O(cn) time if c = "lag time" for each popping.
    Iterating through all elements of a list takes O(n) time.

    So although iterating might be faster, in terms of asymptotic runtime, both take the same time.
    ...Unless c contains a term of n (i.e. log(n), n, n^2)?

  • Custom User Avatar

    There. Added a warning about not changing input data.
    Are you sure that popping off moves is the right/best approach?

  • Custom User Avatar

    Hello everyone,

    There is an issue with the code. If you mutate ANY of the parameters, such as popping off of moves, the random test case fails.
    I read the question again and it doesn't appear to tell that inputs should not be mutated. Please add this warning to the problem.

    Essentially, the moves list must not only be copied, but deep copied so the original moves list doesn't get mutated.
    Just so this post doesn't spoil the answer, I will leave it up to the reader to figure out how to deep copy.