7 kyu
Pair Zeros
1,248 of 2,360Nianpeng
Loading description...
Algorithms
Arrays
Fundamentals
Functional Programming
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.
Please review: C++ Translation
LC translation
this translation modifies the description
Nice
I am attempting this kata in Ruby and passed all the test cases. However, in the random tests, if the input array has 3 + 0s in it I am failing.
When I check the expected compared to my solution, the expected seems to have always deleted the 3rd & 4th 0 of the input array, but surely it should only be deleting the 4th as that's the second 0 in the pair. Surely only every other occourance should be deleted? I can see people have passed the test on Ruby so am I just missing something here? Some examples below:
Examples: [5, 4, 3, 7, 4, 3, 2, 9, 2, 9, 7, 1, 2, 8, 0, 0, 3, 2, 7, 4, 5, 4, 9, 0, 4, 0, 5, 2, 5, 5, 4, 0, 2, 3, 5, 4, 6, 2, 5, 4, 9] input [5, 4, 3, 7, 4, 3, 2, 9, 2, 9, 7, 1, 2, 8, 0, 3, 2, 7, 4, 5, 4, 9, 4, 5, 2, 5, 5, 4, 0, 2, 3, 5, 4, 6, 2, 5, 4, 9] expected [5, 4, 3, 7, 4, 3, 2, 9, 2, 9, 7, 1, 2, 8, 0, 3, 2, 7, 4, 5, 4, 9, 0, 4, 5, 2, 5, 5, 4, 0, 2, 3, 5, 4, 6, 2, 5, 4, 9] my result
[6, 3, 9, 2, 9, 0, 7, 7, 5, 4, 9, 1, 7, 6, 4, 5, 0, 6, 0, 9, 6, 6, 3, 9, 0, 2, 4, 5, 7, 3, 8, 1, 2, 9, 4, 7, 4, 0, 0, 6, 5, 4, 4, 4, 4, 2, 7, 8, 6, 1] input [6, 3, 9, 2, 9, 0, 7, 7, 5, 4, 9, 1, 7, 6, 4, 5, 6, 9, 6, 6, 3, 9, 2, 4, 5, 7, 3, 8, 1, 2, 9, 4, 7, 4, 0, 6, 5, 4, 4, 4, 4, 2, 7, 8, 6, 1] expected [6, 3, 9, 2, 9, 0, 7, 7, 5, 4, 9, 1, 7, 6, 4, 5, 6, 0, 9, 6, 6, 3, 9, 2, 4, 5, 7, 3, 8, 1, 2, 9, 4, 7, 4, 0, 6, 5, 4, 4, 4, 4, 2, 7, 8, 6, 1] my result
[1, 4, 7, 5, 6, 0, 8, 1, 0, 6, 6, 5, 2, 2, 6, 2, 7, 4, 7, 0, 2, 3, 8, 4, 0, 9, 8] input [1, 4, 7, 5, 6, 0, 8, 1, 6, 6, 5, 2, 2, 6, 2, 7, 4, 7, 2, 3, 8, 4, 9, 8] expected [1, 4, 7, 5, 6, 0, 8, 1, 6, 6, 5, 2, 2, 6, 2, 7, 4, 7, 0, 2, 3, 8, 4, 9, 8] my result
Thanks
It would seem you are correct.
Does Ruby allow input modification? The translation seems vulnerable to that. Are you modifying your inputs?
( I don't know Ruby, so I'm poking in the dark a bit. )
Thansk for agreeing that I'm correct! I thought I was going mad. I'm not sure (I'm in training and have a lot to still learn about Ruby). However, I dind't modify any of the inputs for the random tests.
C Translation kumited (author inactive).
approved
Thanks!
COBOL translation (author inactive).
approved
Haskell translation
approved
Ruby 3.0 should be enabled.
Enabled in this fork
This comment has been hidden.
Wrong input in random tests: 2 more parameters (False and 1) are given.
corrected
The submission tests for the python version seem to be in error. The pair_zeros function takes one argument but one of the tests passes it 3 arguments.
I had exactly the same problem for one of the submission tests. My solution was to use the first argument and discard all the additional arguments that were given:
def pair_zeros(*arr):
arr = arr[0]
Thanks for the tip. I think we should still keep the issue open
This is still an issue. Works with ganzinottis solution.
still a problem even if it can be worked around.
def pair_zeros(arr, *args): # just ignore *args and solve
corrected
The random JavaScript tests seem to be flawed. All of the normal tests pass and all of the random tests fail with messages like the following, which seem to be replacing 0's with
NaN
rather than removing them:Testing for [0, 0, 7, 8, 9, 7, 6, 2, 6, 2, 0, 0, 0, 9, 4, 9]
It should work for random inputs too - Expected: '[0, NaN, 7, 8, 9, 7, 6, 2, 6, 2, NaN, 0, 9, 4, 9]', instead got: '[0, 7, 8, 9, 7, 6, 2, 6, 2, 0, 0, 9, 4, 9]'
Random test is reusing the same array object to perform its own computation to check if your solution is valid, so you shouldn't mutate the original array.
I have error: Testing for [8, 0, 0, 0, 7, 0, 4, 1, 5, 7, 0, 0, 6, 6, 1, 0, 0] It should work for random inputs too - Expected: [8,0,7,4,1,5,7,0,6,6,1], instead got: [8,0,0,7,4,1,5,7,0,6,6,1,0] But as I understand it should pairing in this way:
At least according to rule "0's generated by pairing can NOT be paired again" for 8 zeros in source we should have 4 zeros in result. What is wrong - test or my understanding?
As I understand the rules, this test: Testing for [0, 0, 0, 3, 0, 4, 2, 0]
should produce result: [0, 0, 3, 4, 2, 0] because arr[1] should collapsing with arr[0], arr[4] -> arr[2] and arr[7] stay at his place
But expected result was: [0,3,4,2,0]
How is that?
Hi. Did I do something out of the ordinary? I passed the first tests, but something's wrong with the random ones :
just add *args to pair_zeroes() ;)
That did the trick :D. Thanks
;)
This comment has been hidden.
corrected
Testing for [9, 0, 8, 0, 0, 0, 6, 6, 0, 0, 3, 9, 0, 0, 2, 6] ^-----^ ^--^ ^--^ ^--^ As I can see, output should be [9, 0, 8, 0, 6, 6, 0, 3, 9, 0, 2, 6] So why do you expect [9,0,8,6,6,0,3,9,2,6] ?!
I coulnd't reproduce the problem. The test solution expected
[9, 0, 8, 0, 6, 6, 0, 3, 9, 0, 2, 6]
when I tried.Are you modifying the input array? Don't do that.
Indeed doing it in-place causes the tests to fail for me.
The description should really mention this.
Instead of mentioning it in the description, the tests should only pass a copy of the array instead of the original one.
Translated into both Ruby and Python, if you wish to approve them; also, let me know if you might want me to add random test cases to the JS version :)
Thanks for the translation. Feel free to add more test cases as you see necessary.
Done, let me know if they are up to your standards :)
It looks great! I'm wondering if we should generate more
0
s in the arrays since now they are very sparse which may not cover some cases (like several0
s in a sequence). But it's already pretty good right now. How do I accept the change? Should I just re-publish it?I already saved them.
Give me one second to change the chance of generating 0s.
I see the change. Thanks a lot.
My pleasure :)
expect(equalArray(a,b))
should get replaced byassertSimilar(a,b)
.not anymore. => assert.deepEqual
.
Javascript version of Kata needs random test cases to prevent low-level cheats that exploit fixed tests/answers from passing.
random tests have been added..
This comment has been hidden.
What does "signal-digit" mean? I guess it should be "single".
This comment has been hidden.
As @Zozo mentioned, the function name has a typo, and should really be corrected. To save older solutions from being rejected, you could add:
pairZeros = pairZeors;
at the top of your testing code.Also, the description could use some work. A full demonstration of how things are checked and what should happen to the zeros would make it much easier to grasp. Took me a few re-reads and a half-written comment with questions before I finally figured it out. ;P
Here's an example you can use if you wish:
withit56, thanks a lot for this suggestion. I love your example which is very illustrative. I've updated my kata to 1. create alias between
pairZeros
andpairZeors
; 2. adopt your example in the description.Awesome. Happy to help ;P
A typo in function name :
pairZeors
. Should be modify soon !Thank you for pointing it out. It's now fixed.
resolved