7 kyu
Integer Difference
1,284 of 3,102smepple
Loading description...
Fundamentals
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.
There is an issue in this kata the test cases run smoothly but when I click attempt it says should work for n = 0 or something above those lines. I tried using and if statment then it says +0 should equal 1 when I change it to return 1 it says 1 should equal 0. I don't know how to debug this. this is the only test cases that isn't working.
Maybe check this article: https://docs.codewars.com/training/troubleshooting/#print-input
python new test framework
Approved
For the final test data in Python Version, , when
n
is negtive, why the output should be zero?I think it's better to confirm this in description. -)
This comment has been hidden.
Your solution does not take into account cases like
arr = [8, 4, 0], n = 4
. The expected result should be2
, but your solution returns0
COBOL translation (author inactive).
approved
I don't understand how these are counted (yes, I saw @matt c's comment, it didn't help). The first example kinda makes sense: we always look to the right to see whether there are any elements with a difference of n (we don't iterate future values over past, right?). But why does the second example have 4 differences? It should have 2 as well: 1 >> 3 and 1 >> 3. I don't know how to make the description better because I don't understand the conditions.
P.S. Yes, I know that other people solved this kata in Python, this also isn't helpful.
Rather than difference, the description should say distance, ie, applied to single numbers, the absolute value of the difference.
If we consider the absolute value/distance, then the first example should include (5, 1) and (9, 5). But it doesn't. Am I so dense or what? One suggestion I can think of is to have only the
unique differences
counted (as set). That would make anyone's life easier. But that would be a different kata and a different solution, isn't it?You must do combinations if two elements in the array. Not permutations. In the second array, each 1 can combine with each 3 to give a difference of 2 so it makes 4 combinations with a (absolute) difference of 2. It's just what the description says: how many pairs have a difference of n within the array?
If there are duplicate elements in the array they make distinct combinations.
Now I understand. Thanks a lot, man.
should work for n of 0 [ 1, 6, 2, 3, 7, 8, 7 ] 0 [ 1, 2, 3, 4 ] 0
doesn't work! should it be equal 0? i tried @if (n === 0) return 0@ but this not works anyway ...
Not a kata issue.
The description does not state whether inverse inputs should be included (pointed out by myjinxin2015)
if n < 0: return 0
That would save you some minutes of frustration. The description is not worded properly. This is not a spoiler also.. because it spoils nothing (I wonder why negative
n
was even tested)There is a mistake in this kata. All the solutions we have provided will not work if n is a negative number
This comment has been hidden.
input arr = [1,1,5,6,8,16,27] n = -4
expectex output (1,5), (1, 5) -----> 2
given output 0
This comment has been hidden.
.
the (1,5) means 1-5 not 5-1
your mistake is that you always substract the smaller from the bigger
you got zero because your solution is wrong and not only your solution.
My explanation wasn't very good, but when the Kata says it wants the "the number of times where two integers in the array have a difference of n" it is ignoring the sign of the difference. Appreciate this could be clearer, but this can be inferred from the test cases.
thank you for making me to understand the problem
This comment has been hidden.
The description might as well be non-existent.
Could you elaborate? It doesn't look so bad to me.
I'm re-looking at solutions and I don't even know what the actual specs are, because my solution didn't work without sorting the list first and apparently this isn't even required. Edit: I'm talking about python and no my sort doesn't modify input.
Check my solution, there is no sorting involved. It seems it's a problem with your code.
It says more about tests rather than my solution because it passed. The note about look ahead should be added because examples in the description are ambiguous.
[1, 1, 3, 3], n=2 --> 4 # (1,3), (1,3), (1,3), (1,3)
How is it ambiguos? If you could look back then the expected result would be 8.Yeah, yeah, so the issue here is that I didn't bother to look twice at the description, put something together and still managed to pass the tests.
If the problem is some tests should be added so your solution shouldn't pass, write that, otherwise, complaining about the description is barking at the wrong tree.
Regarding what I wrote in previous comments I probably had too much coffee, so I apologize. Sorting isn't wrong, but superfluous because either way each pair is counted once no matter the order of its elements, other than that I improved the random tests (only in python, distribution of expected value isn't best, but enough to weed out wrong solutions).
Python Test results are strange: [1,2,3,4,5,6,7 ] Expected result = 0 ?
What's the value of n there?
The basic tests pass with no issue, but many of the attempt cases fail. The kata does not tell you what the input was on these so I have no idea how to debug this.
yes, i am also having this issue. i don't know to pass test. because i don't know the problem with my code
In this kata, a difference seems to be defined as non-negative integer. So, there are no pairs when a negative n is given. And also, think carefully for n==0 case (e.g. lst=[1,1,1],n=0). When you want to check the input values, try to add a temporary code like "print(lst,n)"
Your code will compare numbers against itself (which fails for cases when
n = 0
) since the diff between a number and itself is always0
languge C, don't modify input array. Why for n = 6 expected 3?
Array = {4, 8, 12, 12, 3, 6, 2}
Submitted: 0
Expected: 3
Log n = 2 1 1 3 3 res = 4 n = 4 1 1 5 6 9 16 27 res = 3 n = 6 4 8 12 12 3 6 2 res = 0
(12, 6), (12, 6), (8, 2) <- Three pairs as there are two 12 there.
JS:
i
andexpected
are not declared, therefore they spill into the global scope. 1, it's a bad practice, 2, https://www.codewars.com/kata/reviews/577420679b79813e6f000498/groups/5e84e4da077c3500013d6ac7I inserted a
let expected
line; is that enough? If not, please enlighten me and I'll fix itcheers
That fixes this problem, but it would be a good idea if you declared
i
too. Just change both occourences fori=0
tolet i = 0
.Maybe I should not... but I can, and nothing can stop me!
Fixed in all languages
The description still says that I'm not supposed to do that.
fixed
C translation kumited
please scrutinize for approval
domo!
C#-Translation kumited!
https://www.codewars.com/kumite/580bacc533c97abd9700000a
Please check and approve it! ;-)
Nice kata! Thanks!
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I added a note to the description about not modifying the input array and also added a few tests to cover that.
Thanks!
Looks great!
What is up with this "9 should equal 1" issue I'm having because of "n of 0". It doesn't make sense. I've checked my code inside and out.
Specifically looked at this array ([1,1,5,6,9,16,27] , 4).
The output should be 3 because we are testing for the difference with the value of "n" not thinking ahead and puting in a "what-if".
Hi - is your code modifying the provided array?
Not at all
Hmm, which version (language) of the kata are you trying to solve?
Must have been python. But 4 years later with 200+ python completions it's surely not an issue anymore...
I find the rule set is very much lacking and hard to figure out. For example why am I being failed for this one [0, 11, 14, 17, 4, 18, 2, 3, 5, 7] 9 2 should equal 0 [5,14] , [2,11]
I could hazard a guess that you only look forward but then my code would have failed for the following which it is marked as passing [4, 8, 12, 12, 3, 6, 2] 6 Test Passed
Are you modifying the input array? That can lead to the tests expecting a different output.
No random tests
I've added random tests. Thanks for checking the kata out.
Why
int_diff([1, 1, 5, 6, 9, 16, 27], 4)
should return 3([1, 5], [1, 5], [5, 9])
instead of 5([1, 5], [1, 5], [5 ,1 ], [5, 9], [9, 5])
?because you don't iterate future values over past.
rules is not clear enough...
Thanks or the feedback. Any suggestions on how to make the description more clear?
I think the fact that you can't iterate future values over past as user "matt c" mentioned 4 years ago, should be mentioned to make the description more clear.
Raised as issue
Love the kata.
If you would like to make it even more challenging you can add some test cases that test for n=0 when there are 2+ of the same items. For example [1,1,1,1] when n = 0, the answer should be 321=6 right?
Thanks! I added the suggested test case.
I meant 3+2+1 instead of 321.