8 kyu
Multiple of index
10,804 of 23,680Dmitry Kudla
Loading description...
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.
Fun Kata but ought to be 7 Kyu
I would not say its 8kyu more like 7
The first index should never be counted. Because division by zero is an indeterminacy. Please correct the tests.
0 has a perfectly valid multiple, it's your code's job to handle that case, obviously, not using division. There is nothing to correct.
You are absolutely correct. I did not focus on the fact of multiplicity, but on the fact of division. I would like to delete my erroneous comment. I appreciate the response.
This was insane to figure out and the description was useless.
0 is not a multiple of 0. You can't divide by 0.
https://www.mathwarehouse.com/answered-questions/multiples/what-are-the-multiples-of-0-solved.php
Where did you get that? 0 is the only multiple of 0.
I'm misunderstanding the concept of multiplicity where you can't divide by zero or the autor didn't consider his example correctly ? assertArrayEquals(new int[]{0, 2, 6}, ZywOo.multipleOfIndex(new int[]{0, 2, 3, 6, 9}));
what do I need to do? the description is so vague
it's very simple you need to verifier if the index of the number is a multipule of the number or not if it's true add the number to the new array
Scala translation
Approved.
This comment has been hidden.
Description says to create a new array but you can pass the kata by manipulating the existing array.
Should mention language when reporting this.
A very enjoyable task setup for the given level
This comment has been hidden.
I agree. I think most people (including myself) approached the problem as if a number in the array had to be divisible by its index, but this doesn't work correctly for index zero as we cannot divide by zero. However, as you suggested, zero is a multiple of every number, including itself. I agree that if zero appears at the zeroth index it is a valid multiple and should probably be included.
Python fork with fix
Also closing as this issue is a duplicate of this one
Ive no idea what it wants me to do
This comment has been hidden.
Thanks, I got it because of your explanation
I'm working on this Kata in Java and the first test case I am getting a standard error because the size of the array is not defined. Is there any solution for this problem or is it a bug? I'm confused and I would appreciate all the help that I can get. Thank you.
I see no problems with the Java tests (apart from the general
i == 0
issue) and also your current code passes now so I'll close this issue.It works on my VS22 but I can't test my code here and I can't get why. It says: src/Solution.cs(12,22): error CS8652: The feature 'target-typed object creation' is currently in Preview and unsupported. To use Preview features, use the 'preview' language version.
Not a kata issue, you're trying to use a feature that's not available with the current C# version.
Alright, thanks. I changed it and it's good.
The user can mutate the input in random tests in at least javascript version.
Fixed for JS
Fixed for Python
Fixed for Java and C#. Working for TypeScript and PHP.
in JavaScript, the random test cases seem off?
see: Testing №4
for [5,79,81,-60,-83,85,-19]
index: 0 1 2 3 4 5 6
divisible by index are: 79 (÷1), -60 (÷3), 85(÷5) yet the expected is: '[79, -60]'
Shouldn't it be [79, -60, 85]
Other test cases also seem to have the similar issue
I just put in a basic JS solution and it runs fine. Please note that over 4K coders have solved it in JS. If you are not certain of an
issue
with the kata, please just post as aquestion
instead.Don't mutate the input.
Okay, that was it. It's fine now
This comment has been hidden.
It is about the number at index 0 because the tests assume that no number is divisible by zero (which is the wrong assumption because this kata is about multiples)
Why I can not us LinkedList?
I solved the problem. Still test fails because of using LikedList.
The examples arn't that obvious for a 8 kyu. I would really appreciate if you could explain [22, -6, 32, 82, 9, 25] => [-6, 32, 25], as since -6 is multiple by its index -1, 32 is multiple by its index 2, 25 is multiple by its index 5. Thank you
I had the same issue to understand too
The index of -6 is 1, not -1.
It isn't the index, rather the position of an element in the list. Consider for example the test case [68, -1, 1, -7, 10, 10] => [-1, 10] The index of 10 in the array is always 4. But in that case the result array shouldn't contain 10 since 10 is not divisible by 4. But if we consider the position of second 10 in the array, which is 5, 10 is indeed a multiple of 5, so we consider it in our result. Please make the necessary changes.
Your task description sounds better
I don't think that's a common interpretation of "index". The index of the 1st
10
is 4, the index of the 2nd10
is 5.Not a kata issue, the index is the position, if you're confused with what
index
method returns, read this: https://docs.python.org/3/tutorial/datastructures.htmlJulia: the example in the description uses 0-based indexing, which is different from the tests.
should be fixed
Coffeescript
Crystal
Julia
(author inactive)
Approved, thanks :)
Oops... look at Unnamed's issue above.
I think Go tests are a bit broken. I see some multiple values repeating, or last value duplicates. Some tests are OK. For example:
Testing №4 for [-10 -11 22 -45 2 60 -11] Expected <[]int | len:4, cap:7>: [-11, 22, -45, 60] to equal <[]int | len:5, cap:8>: [-11, 22, -45, 60, 60]
Testing №6 for [68 49 96 -44 -25 46 -2 -14 -53 17 -44 -99 -85 18 99 60 53 -31 -54] Expected <[]int | len:6, cap:31>: [49, 96, -14, -99, 60, -54] to equal <[]int | len:8, cap:8>: [49, 96, 60, -54, -14, -99, 60, -54]
Testing №7 for [-92 37 50 -26 -97 87 -4 59 -63 81 -63 89 -24 71 -48 72 6 -76] Expected <[]int | len:4, cap:31>: [37, 50, 81, -24] to equal <[]int | len:6, cap:8>: [37, 50, 81, -24, 81, -24]
I experienced a similar issue but it was because I was modifiying the input slice with an in-place filter. When I switched to a secondary slice for the return values the problem went away as you would expect.
See my answer below. No, tests are not broken in Go, and nothing prevents from having duplicate values in input or output array.
The example given in the description implies that the returned array should not contain duplicate values, however the Random test case seems to expect duplicate values to be included, as it will tell me that the length of my returned array is one less than the expected length. If I return an array with duplicate values, however, my code doesn't pass the other test cases. This means that my code will sometimes pass all of the tests, but fails the Random test case when the same number is included twice in the original array, and is a multiple of both indices where it is stored. It's possible that there is another issue, since the error message doesn't actually tell me the expected contents for the returned array, but this is the only explanation I have been able to come up with.
I only tried this Kata in Java, so I don't know if this is an issue in other languages as well.
You're doing something wrong. Nothing implies that there should not be duplicate values : elements which are multiple of their own index in input array, that's all.
Java Translation
Please approve it!)
I don't understand the instructions and the examples aren't helping me understand. Can someone clarify the instructions?
"Return a new array consisting of elements which are multiple of their own index in input array (length > 1)."
Example: [22, -6, 32, 82, 9, 25] => [-6, 32, 25]
In the resulting array [-6, 32, 25], how did those elements get chosen? For the firt one, element -6 * index 1 = -6? Then why is the second one 32, and the last one 25?
( Haskell, possible others - not JS though )
Description specifies
length > 1
. Haskell generates random test cases withlength >= 0
.Fixed for Haskell.
Other languages seemed to be OK, except possibly Ruby - I don't know exactly how ranges are handled.
Not closing yet.
Never mind, I wrote something stupid. That
> 1
constraint is weird in the first place though.Looks good, in Ruby
..
is inclusive,...
is exclusive.Bug in judge and missing test case: 0 is a multiple of 0, so a 0 at index 0 needs to be reported. Many (most?) "solutions" don't, including the judge's "solution". And there is no such test case. At least that's the situation in JavaScript, Python and Go.
Update: In Ruby there is such a test case, and my solution gets rejected because of that.
I agree, I have the same issue in python:
Please update tests and/or specify the expected behaviour in the description.
This comment has been hidden.
0 is a multiple of 0, so 0 at index 0 should pass the test. The only way you can pass the tests (at least in Java) is to ignore element 0, meaning that only the wrong solutions pass :(
Python fork with fix
(adds tests for arrays with leading zeroes and expects those zeroes to be included)
Fixed for JavaScript, TypeScript, Java, C# and PHP.
This comment has been hidden.
indexOf returns the first occurence of a number, your code won't work if the numbers are not unique. Why are you using indexOf at all? You already have the index in a var.
Oh of course! Thanks.
This comment has been hidden.
This comment has been hidden.
The Go translation doesn't correctly start with
package kata
and the sample tests don't work but the official submission doesIt's fixed
This comment has been hidden.
Division by 0.
start the first index at
1
instead of0
. I was getting a zero error while using modulo in PythonHaskell translation Kumited
Please review and approve.
Python translation Kumited.
Please review and approve.
Python not approved. All Ok?
Yeap, I've missed it, sorry) done and thanks
Thx. I'll take care of all issues in all my translations.
Ruby translation Kumited
Please review and approve.
0-indexed
should be mentioned in the descriptions (some languages use1-indexed
arrays instead).It will make sense when this kata is translated in other languages. That case it will be mentioned in the description. But now is only javascript so the description is actual.
When the kata is translated in the future you will have to go through this anyway, so why not do this in the first place?
You can clear the confusion and it takes literally less than a minute, but instead you chose to close the issue with a long reply which probably takes more time to type out then just adding a word to the descriptions.
And, after all, it's never an universally agreed fact that arrays are always
0-indexed
. If you're talking about JS, you didn't mention you're talking about indices in the context of JS either. So it's still your fault.thanks for your opinion. I have another.
When you point at any mistakes in katas, I always fixe it. Thanks a lot.
When you show this kata can have an additonal minor detail in the description - thanks again but only I'll make decision to accept this or not.