Draft

Array transformations (rotate CW,CC,180,Flip Vertically, and Flip Horizonally)

Description
Loading description...
Image Processing
Algorithms
  • Please sign in or sign up to leave a comment.
  • Voile Avatar

    Also, the initial code has the wrong syntax.

  • Voile Avatar

    Isn't this just a combination of many duplicates?

  • JohanWiltink Avatar
    • MichaelSel Avatar

      Thanks for spotting this!

      What can I do to mend this?

    • JohanWiltink Avatar

      Where necessary, use deep copies for the arguments.

      var ta=ga();
      const deepCopy = ta => Array.from( ta, v => v.slice() );
      const actual   = flip ( deepCopy(ta), "h" );
      const expected = flipR( deepCopy(ta), "h" );
      Test.assertDeepEquals( actual, expected );
      

      You could of course (partially) inline this, and if you're very very certain your reference solution does not modify input you can skip it for that.

      You can use Array.from([]), [].map(), [].slice() or [...[]], and I'm certainly forgetting some more, for Arrays; with Objects (Arrays are Objects too!) you can use Object.assign() or JSON.parse(JSON.stringify()).

      It's always something to be careful with if you're passing in Arrays or other Objects (anything but literals "[ .. ]", "{ .. }" and "/ .. /", literal or primitive values of Numbers, Strings and Booleans, and things like null, undefined and Infinity). Also be careful to deep copy all levels of Arrays or Objects.

    • MichaelSel Avatar

      Done :)

      Issue marked resolved by MichaelSel 8 years ago
    • JohanWiltink Avatar

      Done wrong :P

      The issue is not resolved, and my solution is still valid.

    • MichaelSel Avatar

      Oops, forgot to re-publish. I verified that your solution no longer works. Many thanks!!

    • JohanWiltink Avatar

      You're still doing it wrong.

      Reason it now works is that you calculate and store expected value before you let user at the random input. But you're not using a deep copy.

      There is a failure mode for what you are doing. Imagine the task is sorting the input. Imagine the reference solution modifies the input to be sorted before returning it. The user solution will always get an already sorted input and won't have to do any work.

      Do you know what deepCopy = ta => in my snippet actually meant? Sorry for asking if you do, but that might be the disconnect if you don't.

    • MichaelSel Avatar

      In all honesty, I don't understand the point of the deepCopy. Nor can I run the code without the browsing raising an error. Could you explain what you are trying to achieve? (obviously copy the array to a different variable which is const, and therefore cannot be changed, but why is the deepCopy even required, and what is the code supposed to do exactly?)

    • JohanWiltink Avatar

      const deepCopy = ta => Array.from( ta, v => v.slice() ); is a function definition. It's the equivalent of function deepCopy(ta) { return Array.from( ta, v => v.slice() ); } (with some differences. see hoisting, scope and multiple assignments for function, var, let and const if you really want to know).

      Given that deepCopy is a function, does my snippet make more sense now? (You need that definition only once.)

    • MichaelSel Avatar
      1. I actually implemented it correctly this time.
      2. I still don't understand how the deepCopy helps. Why do I even need to make a copy? I understand the code, but not its purpose. This is a really great lesson by the way! Thank you!
    • JohanWiltink Avatar

      1 - Indeed! :]

      2 - The deepCopy protects against a function modifying its arguments. JavaScript passes Objects (including Arrays) by reference, so if the same variable is passed as an argument to multiple functions, it is possible for an earlier function to modify it and later function calls will see the modified variable as their argument. By making a deep copy of the master variable for every function call, every function call gets its own, guaranteed clean, arguments.

      Note that declaring and assigning Arrays with const does not prevent modification of properties, such as array elements (which may be Arrays themselves, and the problem repeats).

      Object literals cannot be modified when used as arguments, but that only really helps for fixed tests. Best you could do with that is randomise the order of fixed tests, which does defeat hardcoded solutions, so it's not useless.

      Primitive values (numbers, strings and booleans) can be assigned to variables without risk of modification when used as function arguments. (These are passed by value as function arguments.)

    • MichaelSel Avatar

      But why does that mean the "ta" cannot be changed?

    • JohanWiltink Avatar

      It can be changed, but that won't affect later function calls, because they get a clean copy. So every function call gets the same input, regardless of any modifying they do.

      Therefore, it doesn't help me to modify it (in order to cheat the test).

      Does that answer your question?

    • MichaelSel Avatar

      Yes! Incredibly interesting and teaching!