7 kyu
Matrix creation
1,805 of 3,191Dmitry Kudla
Loading description...
Fundamentals
Arrays
Matrix
Linear Algebra
Mathematics
Language Features
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.
Haskell translation
C Translation (author inactive).
approved
JS fork updating the test cases and framework to Node.JS v18 (assisted with Katafix)
oh no, I already did it :(
well, let' pick your version, it has better error feedback
what a coincidence 😅 Thx for approval btw :D
For JS Node 18 should be enabled.
Pretty kata, congratulations.
python new test framework is required. updated in this fork
Approved
Nica kata!
This comment has been hidden.
OP left the building
But for anybody left wondering: if
o
is anObject
and not a primitive (Number, BigInt, Boolean, String, Symbol, null, undefined
),Array.fill(o)
will fill the array with all the same objectso
, ie. by reference and not by value.So poking a
1
into the first line would poke a1
into every line, because all lines would reference the same object, etcetera.nice kata.
Ruby 3.0 should be enabled.
Enabled in this fork
C++ tests generate warnings.
Fixed
This comment has been hidden.
By popular demand, previous vote and just mere logic, this kata has been now promoted to 7kyu.
And if even Voile agrees it was ranked too low, you can be sure it deserved this uprank ;)
nice
could you share how to change kata rank by hand?
Same editor to create or update one, but you cannot do it if you are the author.
JS, Ruby, Crystal and c++ translations kumited, cheers :)
great, thanks a lot)
This comment has been hidden.
You're creating an array with number subarrays, the problem is the subarrays are all the same, when you overwrite one, you overwrite all.
Thanks but where I am going wrong? Why is the overwriting happening?
You're overwriting output over and over, when you assign output to the nth element of superOutput n times, you'll end up with
[output, output, ... , output]
all those outputs are a reference to the same array (the last one). Try using push instead or create a new array each time.It can be hard to visualize, but remember objects/arrays are reference types.
Consider you make an array with 5 spots,
output
, and it's stored at address0x2000
. The variable itself is just data that points to that address. When you put an array into another array, you're not making a copy of the array. You're actually just storing that data which points to the array at0x2000
. In reality,superOutput
just contains the same pointer to the same array at0x2000
.This is why you're getting this
overwriting
issue. It's kind of hard to explain the difference between reference types and value types because JS abstracts all this away, but if you wanted to copy by value look into using theArray.prototype.slice
method onoutput
in the linesuperOutput[n] = output;
.Thanks everyone for the suggestions. Using push gave me the same overwriting problem. As far as Array.prototype.slice, I don't understand how to use it. I've only been learning javascript for about a week. As another commenter said, it seems this kata is too difficult the 8kyu level. If there's some other way of solving this that a beginner could grasp, I'd appreciate hearing it. Otherwise I might have to just look at the solution.
push
will still give the overwriting effect, because you're pushing in references tooutput
, rather than assigning them directly.You can use
Array.prototype.slice
to create a copy of an array and then put that copy into your return value.e.g.
8kyu
? This is at least7kyu
.There is no real reason to specify size > 0. You could just as easily specify size >= 0.
Not an issue, not even a suggestion, just a remark. But why overlook a perfectly good number?
I notice you've changed the description, but haven't changed the random tests or added a fixed test. I don't actually expect trouble if you do add it - most everybody's solution will correctly return
[]
when given0
.Note that existing solutions are not invalidated if they start failing new random tests, only if they fail a new fixed test. I don't know how this works exactly; it's just something I'm seeing happening in practice.
done
"identity", not "indentity" matrix.
done
Please use
Test.assertDeepEquals
instead ofTest.assertSimilar
.Done. Could you explain the difference?
Sure. (
assertSimilar
is documented, but AFAIKassertDeepEquals
isn't. )assertSimilar
runs its arguments throughTest.inspect
and compares them, so it compares stringified (Test.inspect
is a wrapper forJSON.stringify
).Test.assertDeepEquals
compares type and properties of its arguments, so it doesn't need to stringify them ( note the absence of quotes and escapes in its output ). Also, forObject
s, it doesn't fail tests when arguments have properties in differing order.If
assertDeepEquals
gets primitive values as arguments, it will compare them directly against each other, soassertDeepEquals
is really the only test you'll ever need; it replaces bothassertEquals
andassertSimilar
. (Test.expect
should never be used directly really. It's aTest
primitive that the rest wraps around, but it gives no useful feedback. You can use it in your own wrappers though. ) ( And there'sTest.expect[No]Error
of course; that was not relegated to legacy byassertDeepEquals
. )Test.assertDeepEquals
was introduced a while ago, without any fanfare, and without documentation ( there might be some in the Wiki now, but that's even newer thanassertDeepEquals
), but it did everything right that existing functionality did wrong sometimes. Its best feature is its output when comparingArray
s andObject
s - much cleaner thanassertSimilar
. Insensitivity to property order is nice as well, because JavaScript sometimes defines it, and sometimes doesn't. And it compares more directly, without stringifying everything. Stringifying things is not elegant.In short - see OP! :]
thanks a lot!
You're welcome!