7 kyu
Difference between biggest 2 numbers
1,492 of 1,861kyduke
Loading description...
Arrays
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.
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/176.
Please join the discussion to help us identify duplicate kata and retire them.
Those duplicates made this much harder. xD
JavaScript tests (both basic and attempt) crash:
Are you on a legacy version? Try selecting
Node v12.x
.You're right, my bad I should have checked that. I solved this kata quite a long ago and the test page remained on the old version. Thank you and sorry for then inconvenience.
No worries!
There seems to be an issue with JS version. Sample and all random tests are fine, but fixed test shows an error "expected 100 to equal 0". Can't wrap my head around how it is possible. Can anyone help?
I have problems too, but it seems different from you. I don't know if it's related. Edit: Well, I don't think it is. I think your code needs some debugging, check input, output, analyze log messages... See there: https://docs.codewars.com/training/troubleshooting
There's a bug in your code.
why array.sort() not working
It's in the description.
thanks
This comment has been hidden.
Hi. You're not far, but your code misses one edge case. Rather than give you a hint that would be a spoiler for you and others that may read the discourse page, I prefer to advice you to check the inputs and examine the cases where your code fails and figure out why. This may help: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
Also, I'm not sure your
filter
is very smart, think what it actually does, it seems you can replace it easily; though, if you catch the case you're missing you may need to write a very different code... Think and try to be logical, that's an easy problem once you figure out clearly what you have to do. Hope you'll solve that soon :)I passed the test with a completly different code :) thanks for pointing me away from my inital code =)
This comment has been hidden.
Hey, I made a Python translation of this kata, can you please check that?
When you do a translation and you want it to be reviewed, put the link with your message on the discourse page and flag it as a suggestion. This way it's easier to follow the translations pending.
Sorry sir, here it is, https://www.codewars.com/kumite/617e6762d243420032588763?sel=617e6762d243420032588763 Should I send a message again? Because I can not flag it as a suggestion.
Why my 3 solution always failed i try in codewars, but i have try it in my Viscode and my answer i get is always correct. i have followed the specified instructions by not using the sort method. Please help me sir!!
You must do something bad because tests are ok. Reading this can hopefully help: https://docs.codewars.com/training/troubleshooting/.
Ok sir, i will try again, thx for your sharring this information!!
The original JS version ( ie, the reference and example solution. same goes for the CS version ) depends on array elements being non-negative. This has been added to the description, and JS, CS and Ruby testing has been updated.
C testing has not been updated. Somebody please do that. TIA.
JS and CS versions have also been updated to guarantee inputs of at least two elements ( they didn't ), and JS version has been updated to
chai
and currentnode
.then the kata becomes uninteresting for C :/ it was a good kata to remind users about integer overflow when computing the difference between a very large signed number and a very small one; if both numbers are positive there is almost no point to it
I'm sorry about that. Using a kata that should have focussed on reimplementing
sort
anyway to make a different point comes with that risk though, I guess.Does C not have some unsigned type you could abuse to make the same point?
how would you abuse that ? if they are both positive, and the first is greater than the second, the difference will always fit in the underlying type, regardless of its signedness
i dont see what you mean by "reinplementing sort", it's a 7-kyu kata, and even bubble sort is harder than 7-kyu (on the other hand, if there is such a kata where
sort()
is disabled i'm interested in translating it to C)Yeah, the unsigned thing is not going to work. Did not think that through.
since you took the time to edit the description and other languages i'm not going to sabotage your work. i edited the C version, unheartedly.
edit : old solutions will not be invalidated, because i removed the tests with integer larger than
INT_MAX
. such tests are useless in the new version anyways, they are no longer edge casesWhat would keep you from passing signed 32 bits, as long as you don't actually pass negative numbers?
it serves as a type hint that the numbers will not be negative. anyway, i changed the test cases not to generate numbers larger than signed INT_MAX, this will keep binary compatibility
Johan I'm not sure to understand, have you updated JS tests lately? If so, they seem broken, see issue at the top of the page.
He probably did, but he doesn't understand that the correct structure is
describe -> it -> assertions
, so his edits broke everything - not sure how he managed to publish it at all this way.It was published with
Node v12.x
, which does not needdescribe
. This breaksNode v8.1.3
, which does require it. I do not consider that a problem. Get with the times.My
C
version passes all various and sample tests except last one withINT_MAX + 1ll
. Please, point me out to information I need to know to solve this problem. ForJS
I've managed for 1 minute, and it's a bit frustrating to immediately get stuck in another language. ))it sounds like you're not handling
int
overflow properly, but without seeing your code it's hard to tell. if you post your code (with markdown and a spoiler flag) i can help you find the problem@yar83: Consider we receive an array of
int
, but we have to return along
, and there's a reason for that ;)This comment has been hidden.
fmax
andsmax
have typeint
. therefore, the expressionfmax - smax
has typeint
also. In general, a signed integer typeT
cannot handle the difference betweenT_MAX
andT_MIN
. (e.g.2 ** 31 - 1 - -(2 ** 31) == 2 ** 32 - 1
which does not fit in anint
(it is equal toUINT_MAX
)).You seem to have understood that since you use
diff
with typeunsigned
; but it is too late because when the assignment is performed, the integer overflow has already taken place in the expression. You need to use larger types at the time you compute the difference.Ufff, I rememdered that earlier I had been doing casting from int to unsigned with
(unsigned)var
. Ant it worked out again! Thanks for you hint.This comment has been hidden.
Thanks :)
(silly question^^)
The JS original depends on array elements being non-negative. I have updated the description and other languages' tests, but my C is inadequate to do that.
Please update C to the new specs ( that should have been in there explicitly since the kata was published, but I can't help that. sorry ).
( see issue above )
Ruby 3.0 should be enabled.
done
This comment has been hidden.
Maybe because of this?
Wow. Reading is fundamental. lol Thanks
Some of the random test cases do not comport with the problem description; the problem description specifies an array of natural numbers, but the random test cases include negative integers. As the natural numbers are the non-negative integers, this is an inconsistency.
I changed 'natural numbers' to 'integers'.
Kata greenlit; kudos kyduke :)
Thanks.
This is the coding interview problem of my company. It is easy problem, but many applicants did not solve it.
Really? I should start sending CVs then, if your offer is good :D
I think there are problems with the random tests (Ruby). Below an example of the 42 failed random tests: arr = [-842, -166, 61, -291, 726, 736, -49, -35, 386, 96] The difference between the two biggest is 736 -726 = 10 but the test expects 340. Same thing with:
The 2 biggest are 459 and 7 hence a difference of 452 but not of 77, unless I don't understand the description...
PS1: the fixed tests are correct. PS2: it would be good to have some "Run Tests".
It worked fine to me, but I guess that he problem was arising from direct manipulation of the array: now the test cases work on a copy of the array, so it should be all good.
Sample test cases added, sorry I forgot them this time ^^'.
Resolved, ys there was a small manipulation of the array:-)
Random test cases would be great! If you need help with them I'm here for you :)
Thanks, I need your help.
I've added some random tests, hope it helps! :)
Ruby translation submitted, if you want to approve it :)
I could also translate it into Python, but while I can certainly disable the
sorted()
function, I know of no way to disable builtin methods like.sort()
: we could just write in the text not to be lame and use short-cuts and that should do as this is a beginner kata (so even if one "cheats", he ultimately gets to read good solutions), but I leave the last word to you.Thank a lot.
Is ther no way to disable sort() method in Python? It is so sad.
Ruby translation is so wonderful!
Regrettably Python is quite strict about built-in types and while I could disable a function, disabling a class method is something I never did; maybe I could disable the whole list class, then recreate it all but for the .sort method, but it would be quite messy.
If anyone has some advice to spare about it, I' surely listening :)