7 kyu
Mirror, mirror, on the wall...
1,388 of 3,316David Gildour
Loading description...
Algorithms
Fundamentals
Arrays
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.
PLs help this is my firs Kata in my life my code works in VS Code but i dont know how to put the answer
the function declaration for mirror should have the input parameter data
you are instead using a global array called arr
get rid of that, put data in the declaration, and your function should work
take note data was already part if the set-up, but you took it out. avoid that in the future. your function is expected to operate on whatever input the testing suite sends it
thx that helped me. will be more attentive next time)
This comment has been hidden.
Read the error message that you get.
toSorted()
is not available in node 18 (which is the highest version of node available for this kata). You'll have to find a solution that does not usetoSorted()
.Thank you BobtheLantern for your quick reply! Truly appreciate :-).
The error msg (below) wan't obvious about node 18. Looking up further, I learnt that node 20 supports toSorted(), as mentioned on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted#browser_compatibility.
Thank you again !
TypeError: data.slice(...).toSorted is not a function at mirror (test.js:4:26) at makeTest (test.js:24:18) at Context. (test.js:34:7) at process.processImmediate (node:internal/timers:471:21)
This caught me out too. It would be good if the node version was updated but I don't know how to do this.
Node 18 is currently the latest available version across the entire website. It requires the admins to update any further than that, but no further updates will be made until all JS kata are updated to node 18. There are compatibility issues with some of the older kata, so those need to be fixed first. There is an effort to get all the JS kata updated, you can join the discord and ask for more info.
In the test cases where there are 1000 elements or more in the data array, the max number may not be unique. For example, if the max in the array is 1000, there can be multiple 1000s.
This raises a question about how we should deal with the existence of multiple max numbers: Should we take them as just one element? Should we place the group of max numbers in the middle, and then arrange the rest of numbers in the mirroring fashion?
Moreover, we may have an odd or even number of max numbers. Under the requirement of returning an array with an 'odd' number of elements in it, things can get more complicated.
This possibility of having multiple max numbers and how it should be handled should be mentioned in the description, and the test cases may need to be reexamined as well.
Hey, an interesting scenario, but this should be treated the same way as any other. Let's say we have an array
[-5, 10, 8, 10, 2, -3, 10]
; as you can see there are 3 occurences of10
, but still the algorithm works the same way: the greatest is10
, so it goes in the middle, the next greatest is10
, so it goes on both sides, then10
again on both sides, and after that the other numbers accordingly, resulting in[-5, -3, 2, 8, 10, 10, 10, 10, 10, 8, 2, -3, -5]
. I added this to the tests for clarity nevertheless.How i hate when you cant really see where is the problem.. My code just should work...(not mutating original input) P.S. nah my bad, turns out i was mutating the original... xD
Ruby 3.0 should be enabled, see this to learn how to do it
Please organize the structure of test fixture as following (although it has been mentioned in the attached link, I'm repeated here again)
Enabled in this fork
This comment has been hidden.
Without the input value it is hard to say, it could appear twice in the input (and because one of them was the middle value, only the other one got reflected) or maybe you've mutated it. After seeing the tests, it seems you would get an error message in that case, so it seems it's the first option.
Definitely,
692
was duplicated in the input list, and also the median, so it should a middle element in the output, as well as reflected on both sides of the middle element, just like Chrono79 stated.Ehm, Chrono .. you can work back from
expected
to get the input.I know, but if mutation was possible no, you can't.
C Translation kumited.
Approved, thanks.
Julia translation added thanks to stellartux.
Go (Golang) translation. My first translation, i hope it's okay xd
Thanks!
Prolog:
Woops, please check again.
This comment has been hidden.
you are trying to print local variable (
numbers
) outside of it's scope (mirror
function)Still when I print(numbers) within the function it returns blank. the array doesn't appear to be populating...
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
That seems self-explanatory :/ The errors are there to help you.
thank you. I need more practice with debugging.
Rust translation kumited!
Thanks a lot!
Thanks! Have a great weekend. :-)
why are the parameters in the function written (data:list)? I'm not used to seeing it this way....I'm used to seeing for example (list). Just wondering if it means if the argument is passed in differently?
It's a type hint.
thank you so much
I intentionally included them, so that new Python users can see the wonderful world of type hinting, which was added as long ago as 3.5, but since then getting more and more popular, mostly due to some great modules and libraries (like Pydantic) taking advantage of this feature.
thank you.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Hello David, I can't see your code as I did not complete the kata yet. Can you tell me why is my solution not working
Again, the error message you provided is far to vague, I don't know what's wrong. Post your code (with spoiler flag), so we can look into this.
This comment has been hidden.
Great, now I can work with this :D Your code is fine except for one important detail you oversaw. Think about how you remove the processed
arr
elements and visualize your algorithm on this example:[-3, 15, 8, -1, 7, -1]
^^ I think you will get this in no time. edit: let me know if you figured this out, I really want you to get something out of this kata!Oh! Two repeated numbers so both of them will be removed in the processed arr. I've changed my solution in the while loop and it worked fine now.
Thanks David for the help! Nice kata to practice on :)
This comment has been hidden.
You may want to look up how sorting integers works in JS.
Prolog translation kumited. Please reply after approving ruby translation, there will be merge conflict.
Indeed, leave the examples in the description simplified, as it is after Ruby translation, please.
Please check this fork to see if it's correct.
Looks good, thanks!
Ruby translation kumited -- please check and approve
Done.
I dont know if this is an issue or if im being ignorant but im getting "Max Buffer Reached 1.5 mib" All my attempts are passed but it's just that one stderr
this is not a kata issue. just remove print statements from the code and run it again.
Could be done at least a bit more interesting if disallowed O(n^2) solutions like this one.
Haskell translation
Note that input modification is not applicable to Haskell. You just can't.
As a Haskell ignorant myself, I'll have to stick to your words :P Thanks a lot, approved.
This is useless. Inputs should not be mutated, and this should be tested. Teach good habits!
Yea, you're right, I don't know why I didn't include it in the tests.
Done, thanks for pointing that out.
I still have this issue for Python. The test shows the values sorted but the attempt returns error when doing this (the cases that passed during testing are failling at the final attempt). "Your function should not mutate the input array, although this will not be tested."
The description was updated after adressing this suggestion to "Your function should not mutate the input array, and this will be tested". Your solution clearly mutates the input.
Correct! My mistake. Sorry for the spam. I didn't understand what "mutates the input" means, but I finally got it.
I notice quite a number of negative/neutral feedbacks, would you care to explain what's wrong? This was not meant to be especially hard or challenging, I wanted to make a quick, fun 8 kyu kata.
There're already hundreds of such small katas where you have to use 1-2 built-in features, including solely sorting or reversing a list. There's not much point in having more katas simply combining 2
8 kyu
tasks into another8-7 kyu
task, so, obviously, PU's will mostly downvote this.I don't think an 8 kyu kata has been published for over a year. Your kata isn't bad, it's just that people tend to vote down unless thay find the kata novel/interesting.
See here.
There's not much point in combining two existing
8 kyu
tasks into yet another easy kata.You kata is very well. Recently I also began to create katas and paid attention, that after the kata has been just published, the downwoters gather to sink it. However in a few days, the votes get better and better. I wish you good luck and hope your kata will be approved soon.