Draft
Array transformations (rotate CW,CC,180,Flip Vertically, and Flip Horizonally)
Loading description...
Image Processing
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Also, the initial code has the wrong syntax.
Isn't this just a combination of many duplicates?
Submit tests are vulnerable to input modification.
Thanks for spotting this!
What can I do to mend this?
Where necessary, use deep copies for the arguments.
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, forArray
s; withObject
s (Array
s areObject
s too!) you can useObject.assign()
orJSON.parse(JSON.stringify())
.It's always something to be careful with if you're passing in
Array
s or otherObject
s (anything but literals"[ .. ]"
,"{ .. }"
and"/ .. /"
, literal or primitive values ofNumber
s,String
s andBoolean
s, and things likenull
,undefined
andInfinity
). Also be careful to deep copy all levels ofArray
s orObject
s.Done :)
Done wrong :P
The issue is not resolved, and my solution is still valid.
Oops, forgot to re-publish. I verified that your solution no longer works. Many thanks!!
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.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?)
const deepCopy = ta => Array.from( ta, v => v.slice() );
is a function definition. It's the equivalent offunction deepCopy(ta) { return Array.from( ta, v => v.slice() ); }
(with some differences. see hoisting, scope and multiple assignments forfunction
,var
,let
andconst
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.)1 - Indeed! :]
2 - The
deepCopy
protects against a function modifying its arguments. JavaScript passesObject
s (includingArray
s) 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
Array
s withconst
does not prevent modification of properties, such as array elements (which may beArray
s 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.)
But why does that mean the "ta" cannot be changed?
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?
Yes! Incredibly interesting and teaching!