4 kyu
Smallest possible sum
259 of 8,092dkhaburdzania
Loading description...
Algorithms
Mathematics
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.
This comment has been hidden.
Scala Sample Test /
Type Mismatch Error
If the numbers within the Seq() do not contain the L to state the numbers are long. Then the compiler will throw a
Type Mismatch Error
. Therefore you have to can bypass the sample tests by submitting. Or you can change sample test by adding theL
Example: (Seq(9L), 9L), (Seq(6L, 9L, 21L), 9L), (Seq(1L, 21L, 55L), 3L)
I have the problem with basic tests. In results actual value is 4, but 4 is not in tests.
actual
refers to the value your code returns to the testing suiteso, your code is returning
4
for that test, whereas it should return12
for that testI am not even sure this is possible, but it appears that, during a particular test, using
arr[i+1]
within a loop based oni < arr.size()
you end up causing UB by altering the elements of the following test'sarr1
?It seems strange, because on my VS Code with compiler gcc 8.3.0 i have correct answer for {4,16,24} and {30,12}
as rowcased said, your code contains undefined behavior. you are accessing the vector past its last element. one property of undefined behavior is that it can cause code to work in one setting (your computer) and not in another (Codewars). this is not a kata issue, but a problem with your code.
Lua translation!
Brilliant coding challenge...
Hhahaha what the evil author, the description is completely misleading
I do agree, like WTF.
Python random test generation can crash due to overflow.
Fixed, tests also refactored to be maintainable (seriously they were a catastrophe before).
This comment has been hidden.
not sure if it's the right answer, but you are assuming that the Codewars' servers are as fast as your computer (perhaps more accurately, that the computing power they allocate to running your code is on par with the one you have on your own machine). 3.5s vs 12s is not that big of a difference, it's roughly the same order of magnitude. usually, when you start wishing you had more computing power, it's time to optimize your algorithms and data structures first
If you aren't passing the non performance tests essentially instantly you have the wrong approach.
(javascript),same timeout.LoL
This comment has been hidden.
this kata is broken :) even i used numpy and wrote a function with 4 lines but it is actually not accepting my solution what the fuck guys
no, it's not. thousands of coders have solved in in your language alone. your code is the problem, guy
My solution still works, and passes tests in around 5 seconds. Your solution with numpy and 4 lines is too slow. You are hereby advised to git gud, improve performance of your solution, and be more mindful about the language used here. Good luck.
This comment has been hidden.
My python code is passing all tests, but times out in the performance section after about 190 tests. I have tried different approaches and on my laptop the code executes in less than 0.1 seconds for arrays as large as 50000 and numbers in the array as large as 1000000. I am struggling to find a more efficient solution. Any one can give me a pointer? Thanks.
don't go through the array completely, instead split it into 2 parts, and process them separately, both for reading, copying and adding. i hope i helped
There is only one working approach in Python and it isn't that.
If C lets that through then the tests need to be hardened significantly.
+1
Most fun kata I've seen so far but the description really needs to be improved.
Someone should rewrite the description of this kata. I see many complaints below pointing out that the algorithm is incompletely specified.
This is what the description should have said:
what a saint
Thanks @geoffp - after attempting to read and reread this kata and going slightly mad thinking I couldn't understand it, you've explained it in such a nicer way.
Thanks! I was interpreting it that way, but I needed confirmation and your formulation is much clearer.
Scala translation
This comment has been hidden.
Your code's time complexity is exceesibly high and a more optimized solution can be deduced
Before to even be a programming challenge, this kata is a total riddle as for directives. Reading
if X[i] > X[j] then X[i] = X[i] - X[j]
, one expects a left to right parsing, and only for immediate neighbour. But then one has to decrypt the example. Let's try with arrow in other direction:It seems we first substract 2 from 3, then 1 from 3, then 1 from 2, then 2 from 3, but lastly we substract 2 from 1. Not left to right, not immediate neighbours, so what? Deep right to left parsing? Greater from next greater? Greater from lower? Strictly none of those. Let's assume it's a deep right to left as far as possible then ending as possible. But what if
[21, 9, 6]
? Plus three poor test cases, can't help a lot.I really like puzzles, and this one could be great as a puzzle if it was clearly stated. If it's not a puzzle, then some things should be clarified, as a lot of people already noticed in discussion. Personnaly, I feel that tagging it as puzzle and replacing misleading instructions with some more examples/tests, would been more interesting… Then one can try it only when in "puzzle solving" state of mind.
In the example the two highest elements are selected each time, likely because it meant the author had to write less lines. You can select any two non-equal elements at random and reach the same answer.
The most difficult parts of such katas is to determine what the blood it`s about. A did not get it (((
TypeScript translation based on existing JavaScript translation.
Approved
This is one of those problems, where solving is possible only with one specific algorithm
And has nothing to do with coding skills.
This comment has been hidden.
I dont understand this question, where the numbers in the examples are coming from
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I am coding in golang and I pass every test case other than the "Input Mutation Test". I don't understand what this test is looking for/doing? The instructions don't say anything about this test case? Here is what the console spits out: Input mutation test Solution changed input Expected <[]int | len:7, cap:7>: [1, 1, 1, 1, 1, 1, 1] to equal <[]int | len:7, cap:7>: [1, 1, 2, 2, 2, 1, 1]
You should say in the instructions for golang that the input array cannot be directly modified.
The test case in golang is wired. looks like expect []int{1, 1, 1, 1, 2, 2, 2} = []int{1, 1, 2, 2, 2, 1, 1} not Solution([]int{1, 1, 1, 1, 2, 2, 2}) = Solution([]int{1, 1, 2, 2, 2, 1, 1})
The input is not supposed to be directly modified, it doesn't mention that in the instructions though.
This comment has been hidden.
This comment has been hidden.
What wrong with codewars? I write py code all works correct code working 150.40 ms but codewars send info to me more 12000 ms. I am disappointed
Nothing is wrong with codewars. Your code may work fast for smaller inputs, but it does fail performance tests, because your code isn't efficient enough (it passes ~100 tests out of 200).
But under completed code i can see 150 ms and codewars write that my code longer than 12000 ms. I don't understand it's logic
The logic is that in this kata, tests are divided into three groups: fixed tests, random tests, and performance tests. Your solution passes fixed tests below 1 ms, and it passes random tests in 150ms, but it's too slow to pass performance tests. Your solution completes only some portion of performance tests and then runs out of time.
Is this clearer now?
I'm not sure where exactly you see that, but I believe it's the tests before performance tests. Since you don't pass them in time, you don't see how long those tests took.
You can print the input and scroll down to see the last test that times out. I ran one of those tests and your code took more than 12s on that one test. So it's either: infinite loop, or code that is too slow.
Ok I understand thank you
Sorry but i don't understand why in the first transformation 9 is used and not 6 and in the second it's 6 ! why not 9 again??!
The array is iterated from right to left and while the index you're on is greater than the index to the left, the index you're on has the one to the left subtracted from it. You then begin again from the right-most index.
So if you have [1, 2, 3], you would check 3>2=True, then do 3-2=1, giving you [1, 2, 1], you would then check starting from the right again, 1>2=False, then 2>1=True, do 2-1=1 to give [1, 1, 1], then check 1>1=False and 1>1=False and that means it's at a solved state.
"you would then check starting from the right again" this does not fit an example.
Then you check an element even if its fit x[i] > x[j], you keep going witout reseting the line, so after make(x[2]) = 3(x[2]) - 2(x[1]) , you next step is check if 1(x[2]) > 1(x[0]). Am i wrong?
This comment has been hidden.
(6, 9, 9 - 9) = (6, 9, 0) step can't be done.
The description is too vague to be useful and needs to be clarified.
It would help to even mention what things determine the selection of
i
andj
duringif X[i] > X[j] then X[i] = X[i] - X[j]
. Is it merely 'whichever pair leads to the most significant influence on the smallest end result'? (Don't tell me, but on the side, I wonder: Is that mathematically equivalent to 'whichever pair makes the largest subtraction available immediately', or is it more involved, as in Pyramid Slide Down?) I looked at this a while back and it didn't make enough sense to even begin. Maybe doing that other kata in the meantime helped somewhat.This comment has been hidden.
Great fun! Please improve the description though, it's not immediately clear what you want us to do.
broken ass shit on python it has limit of 109 tests passed timed out for no reason, I did fastest I can
The fact that your solution times out does not necessarily mean the kata is broken. Maybe your solution is not good enough? "I did fastest i can" does not necessarily mean "sufficiently fast".
It's not broken.
I'm looking at your code in which you are trying to solve it all by brute force. This approach won't work, look at the fixed tests and try to find a pattern (there is one). Cheers!
This comment has been hidden.
Has anyone been able to solve this problem in Kotlin? Always getting "Execution Timed Out"
You need a faster approach here. Iterating the array/list over and over again is too slow. You should try to find a more "mathy" approach.
I can't either after spending all day with 3 different algorithms...
This comment has been hidden.
You pass all tests successfully until you run out of time. This means that your solution returns correct answers, but is too slow and does not manage to process all tests.
Your solution being too slow is not a kata issue.
This comment has been hidden.
Lol, I just tried to divied by zero...
Why is my code running good in visual studio and get all tests, but in codewars I have: UndefinedBehaviorSanitizer:DEADLYSIGNAL ==1==ERROR: UndefinedBehaviorSanitizer: FPE on unknown address 0x000000425719 (pc 0x000000425719 bp 0x7ffc046ee768 sp 0x7ffc046ee290 T1) ==1==WARNING: invalid path to external symbolizer! ==1==WARNING: Failed to use and restart external symbolizer!
Is anybody know?
This one is really throwing me for a loop. Ive managed to write a solution that passes 108 of tests, and in the test example is actually more perfomant than the example only needing 3 steps instead of 5, but i still time out every time. Guess its back to the drawing board again.
Not complaining by the way, I like a good puzzle, just stating this one is more challengin than usual for me.
Still not there yet.... The first 108 tests are executing is 74ms but the huge number tests always time out. Im not sure i can make this thing any more efficient than it already is.
Sure you can. More than 4000 persons did it before.
i think im going to need to attack it in a fundamentally different way. those huge tests must be absolutely massive numbers.
This comment has been hidden.
With go, 208 tests pass and 1 fails. The failure is: "Input mutation test":
Solution changed input Expected <[]int | len:7, cap:7>: [1, 1, 1, 1, 1, 1, 1] to equal <[]int | len:7, cap:7>: [1, 1, 2, 2, 2, 1, 1]
Unclear what this test is testing for.
This test verifies whether your solution destroyed contents of the input array, or maybe left it untouched.
How is that unclear?
Thanks for the context; this is my first encounter with "mutation testing" https://en.wikipedia.org/wiki/Mutation_testing
It's unclear which requirements, in the kata's description, the test is covering.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Cannot reproduce.
why it keeps timing out in C++? even though i didint use recursion and it runs fine when i run it in my terminal without with less than a second
What times out is your code and this is not a kata issue. Your code is too slow or it never ends. An issue is a bug in the kata. I your terminal you cannot run the whole bunch of random tests. Please read this and don't raise issues so lightly in the future: https://docs.codewars.com/training/troubleshooting/
Perhaps a better approach would be to give the user a framework to run all the tests locally, so that they can debug failed tests using something better than printing to stdout.
D translation
approved
Could someone give a more clear description of this kata please? I might just be dumb but the one the author gave is really confusing me and I'm having a hard time getting started on the kata.
I'll agree with this, I believe the author needs to give a much better explanation
You have an array :
If one element is strictly greater than another, let's say
a < b
, then you subtract the smaller from the greater :And you repeat this again and again until it's not possible anymore. At this stage, you sum all the numbers left in the array and return that.
The thing is (look at the
Additional notes
section), this method is really ineficient. The real task is to find a way to do the same calculation but much faster.carry on repeating the operation in the description until all the numbers are equal.
I'm also confused by the description. Even the example is helping me. I'd love to take a crack at this one but I don't even know what's being asked.
This thread was enough to clarify the description. The description has a bug in it in the example where it has '21' it should have '12'.
NASM translation
python fork:
https://www.codewars.com/kumite/62c54e0b5568e4000f587d99?sel=62c54e0b5568e4000f587d99
Approved by someone
Tests not running in go. Though, with the same solution it worked in python.
Tests are fine in Go, and this is not the proper way to raise an issue. Please read this: https://docs.codewars.com/training/troubleshooting/
Rust translation (author is inactive).
approved
C++: tests can generate inputs for which result overflows
unsigned long long
.Frustration. Every test passes in 1.5 seconds except for the last one. I hit max recursion error. No idea how everyone gets around this...
Without recursion.
Bump: in Python it takes around 6 seconds even if one just writes "return None" in the body of the function. From the comments below, it seems related to Python 3 and this kata.
You need iterative version for the important function to pass this kata.
What does that mean? Do I need to change any setting somewhere?
This doesn't occur anymore. Maybe there was a flaw in some old version, but with Python 3.10 it's fine.
This comment has been hidden.
Who said it's not?
COBOL translation (author inactive).
approved
This was a fun kata, thank you. I have to say however that I was almost turned away by the description, which in my view needs improving.
Indexes i, j are used without being defined. Also the part where the transformation example is presented could be more clear. I suggest to add X_0 [6, 9, 21] at the top of the sequence roll out, otherwise people will continue to think 12 is a typo instead of the result of 21 - 9.
Agreed! I also think the order of the arrows should be changed. Saying "X1 -> ... " means, to me, "with the input X1 you get the following". But the author actually means "X1 is dervied from...".
This should be changed to match the common mathematical usage of "->".
You will get the same final answer regardless of which i and j you choose for each iteration, so long as
X[i] > X[j]
.Biggest facepalm of my life when I figured out the easy solution lmao great kata!
Sorry, but this Kata is just weird. I do not understand the description. This is so unlogical and not correct. You can't just come with i and j without saying anthing about them... What is i? What is j?
i and j are any two indices in the array. If one element in the array is larger than another one, change it to the difference between the two.
Correction: upon further reading I see that there are no transposed digits but the whole text is somewhat misleading
I'd expect to have the
and so on.
Python: The description and example contain two pairs of transposed digits (
21 <-> 12
)X = [6, 9, 21]
is detailed below:" but the code starts withX_1 = [6, 9, 12]
solution([6, 9, 21]) #-> 9
but the detailed steps start with[6, 9, 12]
The code shown assumes the correct values
This comment has been hidden.
From
X
,X_1
is the array after the first transformation.I don't understand it seriously.
nvm i tried to explain and i dont know how to delete the comment so im editing this.
Nice kata! Just a small tip: compare the result array and the original array and try to find a pattern. Good luck to future challengers!
There's a pattern indeed but I can't figure it out
Timed OutPassed: 106Failed: ?Exit Code: 1 Test Results: Fixed Tests Should pass example tests (9 of 9 Assertions) Random Tests 100 random tests (97 of 97 Assertions)
what is the problem it passed all exames
It passed all the tests until it timed out. There are 209 tests in Python.
ok thanks
Incorrect test structure in Crystal: "Basic tests" are not in an
it
block.Not fixed. The random tests in Crystal sometimes crash with
I've reduced them drastically, can you still reproduce them?
No overflows now, but
Look at this...
same for me in Javascript i get a max buffer size reached. the algorithm works fine until it gets tot he huge arrays. I am not sure how the help system works here, i guess we're not allowed to leave our code for others to help...
im pretty sure that the buffer size error is caused my you printing stuff
Cannot pass the attempt, my python code works fine for the test at 520ms, even for the random but it gets timed out. i dont think its an issue of the efficiency, its something else.
To pass the test, the solution should have about linear time complexity and less 600 ms 1 million values.
I've been trying to fix the Python version and found out that something is actually different from something else and is an order of magnitude faster. The difference is much bigger that between different ways of generating random input, so I'm wondering if the size of performance tests should be descreased to compensate for slow random input generation (I have a faster version, but it still is not too fast) or increased because solutions like this just use the slow thing when there's a faster alternative. At the same time, random test case generation seems to be taken from the original Ruby kata and I'm not entirely sure why it's done like that, it causes solutions like this to be the fastest, although they have a horrible worst case performance. So I'm trying to understand if it's OK to replace the test generation code entirely or the current random tests generate the worst case data for a certain type of solutions. So what solution are there that are faster then a naive implementation, but not good enough to be acepted? (Don't forget about the spoiler flag when necessary.)
I'm passing 98 random tests out of 100. My algorithm can't handle an array of 42,000 numbers. Trouble :(
It's impossible to meet deadlines, my solution is the most effective I know and still not enough for the deadlines (of course there are no errors, and all my test passed in less 2500 ms) language : C++
Please help! :)
Can someone please explain why the first transformation in the example [6,9,21] is 21 - 9 and not 9 - 6? Are you supposed to always take the maximum?
You can do it in any order you want, the final result, will be the same. The amount of steps will change, surely, and not all algorithms will work.
Thank you!
Tried testing with only returning a number, resulted in 4000+ ms, add 2 more lines, resulted in 7000+ ms, is this normal?
It's not really desired situation, but possible. This time is a sum of the test suite startup time, generation of random inputs, and running the reference solution. If there's many cases and long arrays, test suite can take some significant time even with empty solution.
This is normal if the two lines you added are terribly slow. I added like 10 lines and time increased for about 0.5 second.
What is not clear in the explanation are the indices
i
andj
. They refer to algarisms in the same number or indices in a vector? In the first example an array of[6, 9, 12]
is given, then there is a21
that appears.21 is in the origial list. 12 = 21 - 9.......the order of i and j doesn't really matter at all.
This comment has been hidden.
This is my 3rd Kata 4 attempt. I have submitted 4 versions, including one by framing the data to n*2 shape, so far all tests passed but failing on execution timeout. I am guessing that I need to utilise python standard library or other ones to optimise further. Are importing standard and non standard libraries allowed in codewars? I haven't imported any but survived to kata 4 tasks so far. Alternatively can someone give me some guidance on whether this task can go through without any library import, but with cleverer algorithms.....If so I will try harder on algorithm design.
for [4, 8, 8] expected 24 why not 12? kata with mistakes
Please state which language you're talking about.
golang
It looks like you changed the input data. I added/fixed the tests here: https://www.codewars.com/kumite/5d7a94b270a6f60015a422c3?sel=6024413da50089000973dc6e
This comment has been hidden.
(Python)On this kata, for the first time I get an error message trying to use lru_cache decorator: File "main.py", line 7, in test.assert_equals(solution ([9]), 9) TypeError: unhashable type: 'list'. Does anyone know the cause?
Yes. Lists, as the error states, are unhashable (because they can be mutated and thus their hash would change and that is bad).
lru_cache
's docs state:Humm, ok. I knew about unhashable lists, I also knew, at least theoertically, that lru_cache stored data in a dictionary, but I never faced that problem before. Thanks for the link and the explanation!
Meh... I knew it there was something fishy here :) The description though is out of this world!
I've read the description 10 times and still I don't know what is asked here.
Observe the list given and ask yourself: "How are these numbers connected?"
This comment has been hidden.
Indexes.
This comment has been hidden.
Testing for: numbers = Array too big to be displayed ( don't understand
That is just an artifact of how the test results are printed, it's not an error in your solution.
OP solved it, closing
Time: 2058ms Passed: 116 Failed: 0 Exit Code: 137 Test Results: Fixed Tests Should pass example tests (9 of 9 Assertions) Random Tests 100 random tests (100 of 100 Assertions) Performance Tests 100 random, huge tests (7 of 7 Assertions) STDERR Max Buffer Size Reached (1.5 MiB)
WTF? What to do about it? Python.
https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#i-get-max-buffer-size-reached-15-mib
Not a kata issue.
Thank you
lol
This comment has been hidden.
Fixed - added explicit require in tests
Python, I've managed to get
RuntimeWarning: overflow encountered in long scalars
on the line witharr =
in performance tests, whererandint
fromnumpy
is used. I don't know if this can affect tests (mine passed), but this could be completely avoided by tweaking the numbers.Same. I feel like the runner should not accept a solution if there is an overflow? Or is it just a warning of a possible overflow?
ok i dont get it, whats up with this test?
Testing for: numbers = [12 12]
Expected : 24 to equal : 12
every other test is passed
im on golang
The description states that the condition for performing changes to the array is
for any i, j pair.
12 > 12
is false, so no changes are made to the array, which is then summed: 12 + 12 = 24 as expected.In the example, in the first step, the first transformation is derived by taking 21-9 and getting [6,9,12] from [6,9,21].
Why isn't the second transformation derived by taking 12-9 to get [6,9,3] from [6,9,12]?
If X[i] > X[j] is the condition, then why does the direction of the condition go from X[2] - X[1] (21-9) in the first transformation to X[2] - X[0] (12-6) in the second transformation when X[2] - X[1] (12-9) in the second transformation satisfies the condition and would mirror the first transformation?
Either way you'll reach the same result.
Yeah I got that too, just wondering if the order was intentional, as in, you should prioritize the subtraction that will result in the greatest difference for each transformation step.
the final result won't be any different
I finally wrote a solution for Python 3.6 that comes close enough to passing all of the random tests within the time limit, that I was able to succeed after attempting the same solution about a dozen times. Does this kata really need hundreds of random tests?
Also the description is STILL wrong, the 12 in the example list should be 21. That made this kata really difficult to understand at first.
It's a 4kyu kata and performance is tested, so I'd say yes.
The highest rated solutions don't even work any more. I guess they're bad?
Read this: https://github.com/Codewars/codewars-runner-cli/issues/757 I think it's related.
X_1 is the first transformation, so
X_1 = [6, 9, 12]
is ok. The comment is how you reached that result, not what you do after reaching it.It should have been like this "X_5 = [3, 3, 3] # -> X_5[0] = X_4[0] - X_4[1] = 6 - 3" instead of this "X_5 = [3, 3, 3] # -> X_5[1] = X_4[0] - X_4[1] = 6 - 3" in the example which is given in instructions. Second element of array is assigned according to the example: "X_5[1] = X_4[0] - X_4[1]". Then, if you look at the result, first element is the one which is assigned. "X_5 = [3, 3, 3]". Am I missing something?
No, it should be like you say:
X_5 = [3, 3, 3] # -> X_5[0] = X_4[0] - X_4[1] = 6 - 3
This comment has been hidden.
I think part of the problem with timeout is that the tester is printing out the whole array to the console when it says testing for... This slows down testing quite a bit when arrays are tens of thousands of elements long.
I noticed most of the katas that I'm solving these few days have Time Out issues. Creating an excellent algorithm and minimal codes are extreme.
it's most of what it takes to be a good coder for sure
Problems like these have nothing to do with coding skills. If you know the algo, you can do it, otherwise you'll keep staring at it for a week and still get nowhere.
This comment has been hidden.
Completed in all languages except Crystal and Racket without any problems.
I submitted a second solution, but instead of my solution, the provided template was accepted as a solution. All further submission yield the same result. https://www.codewars.com/kata/reviews/5bc56a4d7bf31df2f0000319/groups/5e7f5abbbe09b60001b2a92e
Not anymore ~~
That's a nice one! I don't see any issues here, my solution made it in less than 4000ms (Python version).
what was your O?
Is it possible to do at Kotlin? Performance tests are real killers
I am not happy. I spent a lot of time on writing a solution that is O(log(n)) because the description said it was necessary to write an efficient algorithm (and it should be); then I find out that everyone else wrote a simplistic O(n) "solution" and got away with it. :/ That is not right.
How the hell do you write an O(log n) algorithm for a function with n integer inputs...
You didn't write a O(log n) solution.
I meant O(n log(n)). And considering the number of people complaining about timeouts, they wrote a O(n^2) one.
Random test sometimes generate negative integer values, although description says there are only positive ints in the array.
Language?
Go
Same happens in crystal...
Same in Python.
Cannot reproduce in Python.
Oh boy, If only Euclid could live these days.
This comment has been hidden.
True the description is misleading right of the bat !
Java translation
While solving the Go version I calculate 10, it wants 3022.
How on Earth is it supposed to be 3022? Aren't there any more transformations possible?
you got it the other way around, it tells you it expects the int 3022 to equal 10 , it should equal 10
Not an issue, read the error message properly.
I have same problem, keep getting "STDERR: Execution Timed Out (12000 ms)" during the 100 random huge test. Passed all Assertation..
use python 2. if you are already using it, just use a better algorithm.
Spent 15 minutes coding, and another 15 testing. Not sure if my solution in is correct and the tests are broken or if my solution is incorrect and the tests are broken. Either way it passed and failed randomly. I couldn't find any pattern, so I just gave up and submitted lol
What is the size of the random testdata in the Python version ? I was curious about this when I was looking at performance in C (which seems to have sizes up to 1M).
100 tests each ranging from 30K to 50K elements
While solving the C-version I find rare 0 values in the array - maybe sometimes the last array-entry. I see also one other solution stating and handling this the same way I do.
fixed, thanks
C# and Dart translations.
This comment has been hidden.
O(n) works.
Damn, the current way of thinking can't really be optimized to that extent. Guess I'll have to find a different way somehow.
Python Version Times out all the time with random tests
Have you clicked reset?
Reset doesn't affect random tests. Lightened performance tests more. Good now?
Works fine with another online compiler times out all the time here. Big issue with Python !
Read one of the posts below stating it doesn't work in Python for the time being.
Try again, it should be fixed now.
Thanks @steffan153. :)
This comment has been hidden.
Javascript still works, as for Python, read the posts below, there are already issues open. Closing this one.
Try again, it should be fixed now.
C translation kumited
This is wrong kata. My solution on Python3 passes 92-189 tests. Code: def solution (a): return 3 get "STDERR: Execution Timed Out (12000 ms)"
My solution on Python 2 passes 209 tests and get "STDERR: Execution Timed Out (12000 ms)"
Try again, it should be fixed now.
Could the Kata description at least be updated to show that, as of October 2019, no solution is possible with Python 3 so that others do not spend time on a Sisyphean task?
Fixed Python version instead.
Python version still timing out and working well with another online compiler
I'll second that: Python version still timing out
Something wrong with this kata. My solution passes 149 tests (100 of them "random, huge tests") and then stderr with timeout.
there is an issue with python, see below
Try again, it should be fixed now.
Max Buffer Size Reached (1.5 MiB) Everything is correct but this happend. Can anyone tell me why?
That's okay now I know the problem because i printed too much. but still timeout :(
If it is Python you're talking about, even the most performant solutions won't pass, see many posts below saying that, if it's javascript it works, but not with the trivial solution.
Try again, it should be fixed now.
In python, even with return 0, timeout is received :/
Try again, it should be fixed now.
Go translation awaiting approval.
This comment has been hidden.
It's been already mentioned below, and it's been discussed here: https://github.com/Codewars/codewars-runner-cli/issues/757
thank you for your reply, so there is no fix yet ?
Sadly, not yet. You could try asking there as it affects more than only this kata.
ok I will ask there, have a good day !
Try again, it should be fixed now.
thank you it works !
This comment has been hidden.
Try again, it should be fixed now.
Racket translation https://www.codewars.com/kumite/5d599dd552e1b10015919b6d?sel=5d599dd552e1b10015919b6d
This comment has been hidden.
This comment has been hidden.
The problem is https://github.com/Codewars/codewars-runner-cli/issues/757 The number of tests can always be adjusted, but until we know the end of the story, there's no way to know what it should be...
Try again, it should be fixed now.
Using python, I keep getting "STDERR: Execution Timed Out (12000 ms)" during the 100 random huge tests, even with
def solution(a): return 0
which I'd assume should just tell me all the tests have failed.
I am having the same problem. Just read the first term and returned firstterm * length and got a timed out.
I had the same problem using python 3.x. But it's ok with python 2.x.
for it does not work whatever the python version I choose !
Try again, it should be fixed now.
solved
It has a typo in the example: X_1 = [6, 9, 12] # -> X_1[2] = X[2] - X[1] = 21 - 9 (12 should be 21) And 'i' and 'j' have no defined order (where do they start? Is 'j' always larger than 'i'? etc.), so I can chose any order I want. But if I can chose any, the first step in the iteration could be as well: X_1 = [6, 9, 21] # -> X_1[1] = X[1] - X[0] = 9 - 6, because all the written rules apply to it. I didn't check if the result would be the same, but this specification is ambigous.
This confused me to at first, but I don't think its a typo; rather, they are saying 12 is the result of 21-9.
Well, I definitely made a meal of that one - having seen the solutions I need to kick myself in the ar*e big time!
This comment has been hidden.
Try again, it should be fixed now.
This comment has been hidden.
It's been already reported below by cocoderrr, but it was marked as having spoiler content (dunno why). I've unmarked it and I'll close yours (there is no need for multiple reports about the same issue). Cheers.
P.D.: I have no idea why, but even when I unmark it and reload the page, it's marked as having spoiler content again.
Try again, it should be fixed now.
There is something fishy with this exercise, or better put, with the test cases. I get it, the first couple of attempts may not be optimal but when I ran my solution on large arrays (in fact much larger than stated in the instructions e.g.:
l = np.random.randint(np.iinfo(np.int64).max - (1<<62) , np.iinfo(np.int64).max, size=1000000)
it still completes in about 5 sec rather than taking longer than 12000 milsec timeout claimed by the test platform.
The 12000 miliseconds timeout is for the whole test suite, in Python there are 9 fixed tests, 100 random tests and 100 random huge tests.
I get that, but still... the timeout is erratic and you can see others complaining about that too
Yes, I've tried a previously working solution and it failed, so maybe someone changed the test suite and now it doesn't work.
Try again, it should be fixed now.
That's crazy: when in Python I type a function that does nothing
it times out!
The test harness is so computationally expensive that it times out without the code even having the chance to execute.
Try again, it should be fixed now.
Try again, it should be fixed now.
unresolvable... :/
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Fixed
How many tests are there for Python? I've got: Test Results: Fixed Tests Should pass example tests (9 of 9 Assertions) Random Tests 100 random tests (100 of 100 Assertions) Performance Tests 100 random, huge tests (100 of 100 Assertions)
and still error because of execution time
It seems there are exactly 209 tests in Python.
changed to Python 2.x and with the same result the test was accepted
Poorely stated problem with typos in the example. Skip!
This comment has been hidden.
This comment has been hidden.
What does "When no more transformations are possible" meant? Is it when all numbers in the array are the same? Or is it when the difference to any pair of numbers is 0 or negative?
Yes ;)
This comment has been hidden.
It might be a language thing because I am not a native English speaker, but IMO description is horrible and for life of me I could not figure out what
i
andj
are supposed to mean and how should they be picked.This comment has been hidden.
I'm not sure I follow the example given, or how it fits the description. We're told to basically repeatedly compare each value to all of the others and take action depending on this comparison. But then then the example seems to be foccusing on comparing the largest value with the largest of all other values instead.
For instance :
X_1 = [6, 9, 12] # -> X_1[2] = X[2] - X[1] = 21 - 9
No care is given to x[1] being bigger than x[0]
If I follow the "i,j" instruction, I would expect the process to look more like this :
[6, 9, 21]
(edit - note : I initialy misread the example so here I used 1 as first index instead of 0) First pass : x[1] < x[2] => . x[1] < x[3] => . x[2] > x[1] => x[2] = x[2]-x[1] = 9 - 6 = 3 x[2] < x[3] => . x[3] > x[1] => x[3] = x[3]-x[1] = 21 - 6 = 15 x[3] > x[2] => x[3] = x[3]-x[2] = 18 - 3 = 12 => [6,3,12]
Second pass : x1 > x2 => x1 = x1-x2 = 6 - 3 = 3 x1 < x3 x2 == x1 x2 < x3 x3 > x1 => x3 = x3-x1 = 12-3 = 9 x3 > x2 => x3 = x3-x2 = 9-3 = 6 => [3,3,6]
Third x1==x2 x1 < x3 x2==x1 x2 < x3 x3 > x1 => x3 = x3-x1 = 6-3 = 3 x3 == x2 => [3,3,3] => 9
The result is the same, but the process isn't (probably longer) => and my problem here is I don't see how to demonstrate that the method used in the example will always give that same result.
I think something is wrong with Python tests. My solution times out and everytime on different step... One time it even passed... But i didn't publish it, and now i can't get it to pass again. I did the same kata in JavaScript and that was so much easier to pass with similar code. I even tried to take a solution from somebody in "Solutions" and run it and it didn't pass LOL Edit: It got 100/100 in performance test and still didn't pass, Codewars says timed out 12000 ms... I switched to Python 2 and it passed...
Same issue as mine but mine is done JS. I got it 100/100 in everything except for the timeout :-)
i have a problem with C++ 14: why the test case has the type of vector the error show like this: main.cpp:161:20: error: no matching function for call to 'solution' Assert::That(solution(arr), Equals(sol(arr))); ^~~~~~~~ main.cpp:54:15: note: candidate function not viable: no known conversion from 'vector' to 'const vector' for 1st argument unsigned long solution(const std::vector& arr) ^ main.cpp:171:20: error: no matching function for call to 'solution' Assert::That(solution(arr), Equals(sol(arr))); ^~~~~~~~ main.cpp:54:15: note: candidate function not viable: no known conversion from 'vector' to 'const vector' for 1st argument unsigned long solution(const std::vector& arr) ^ 2 errors generated.
This comment has been hidden.
Not a question. It's impossible to tell what's wrong with your solution if nobody can see it.
Why wrong? I tested the same C++11 source code, however the results are not same???
Meanwhile, I unlock C++ solutions, and pass the same C++ test on my local PC with a a ready made unlocked solution and the result is 95 again, i.e. it is not 97 as a test states preliminary?!
More over I passed a JS solution with 106 green test (during attempt) of total 100 huge tests and the result is timeout, please write an eficient algorithm??
Either there're problems with the reference solution or with your tests. I haven't solved this one, so I can't investigate.
Then your solution is not efficient enough.
The test is not mine. It is a random generated test during attempt test phase and test numbers are reported by codewars. I simply copy/paste that test and both solutions, mine and ready made one from unlocked solutions report the 95 as a result?? At the same time the test says that the result must be 97?
For those numbers the control function expects 65441025342 (at least now), have you clicked reset and tried again?
The C++ kata description says unsigned long. The max unsigned long number in C++ is about 10 digits, i.e. tests and control functions differ from problem description and it is a bug. Maybe the correct C++ description is unsigned long long, however the assert statements have to be updated in that case.
I propose to add to the test a variant of the array that contains zeros ...
This comment has been hidden.
If you update "Smallest possible sum" description
from "if X[i] > X[j] then X[i] = X[i] - X[j]"
to "if X[j] > X[i] then X[j] = X[j] - X[i]"
then you can pass the first of BasicTests as follows:
"Assert::That(solution({1,21,55}), Equals(3));"
Please fix that bug.
if
i
andj
are any integer between0
andarray's length - 1
how is one expression different than the other?Yes, you are true. The bug is mine, however the bigest C++ unsigned long is 4 294 967 295, i.e about ten decimal digits, and at the same time random test (during attempt) passes me 12 digit numbers
such as assert(solution({340445127495, 60921759657, 293857899522, 204267076497, 347612393337, 258021570312, 25085430447, 14334531684, 168430747287, 129010785156, 336861494574, 304608798285, 322526962890, 21501797526, 146928949761, 272356101996, 28669063368, 136178050998, 150512582682, 229352506944, 318943329969, 7167265842, 161263481445, 236519772786, 351196026258, 118259886393, 172014380208, 39419962131, 236519772786, 218601608181, 43003595052, 344028760416, 250854304470, 3583632921, 297441532443, 57338126736, 175598013129, 293857899522, 211434342339, 351196026258, 247270671549, 315359697048, 154096215603, 311776064127, 139761683919, 222185241102, 143345316840, 351196026258, 351196026258, 222185241102, 347612393337, 143345316840, 175598013129, 283107000759, 225768874023, 336861494574, 10750898763, 186348911892, 240103405707, 265188836154, 258021570312, 107508987630, 354779659179, 204267076497, 46587227973, 193516177734, 75256291341, 254437937391, 25085430447, 275939734917, 204267076497, 64505392578, 17918164605, 154096215603, 193516177734, 326110595811, 243687038628, 68089025499, 225768874023, 240103405707, 265188836154, 326110595811, 261605203233, 215017975260, 286690633680, 78839924262, 53754493815, 100341721788, 89590823025, 222185241102, 71672658420, 344028760416, 240103405707, 333277861653, 311776064127}) == 340445127495) ;
I'm gonna close this issue, because it has changed to something else (unrelated). Below @GiacomoSorbi solved some C++ kata issues, so, click reset a few times and try again. If you still see that behaviour, report it in a separate post.
In the given example given in the instruction as shown below:
X_1 = [6, 9, 12] # -> X_1[2] = X[2] - X[1] = 21 - 9 X_2 = [6, 9, 6] # -> X_2[2] = X_1[2] - X_1[0] = 12 - 6 X_3 = [6, 3, 6] # -> X_3[1] = X_2[1] - X_2[0] = 9 - 6 X_4 = [6, 3, 3] # -> X_4[2] = X_3[2] - X_3[1] = 6 - 3 X_5 = [3, 3, 3] # -> X_5[1] = X_4[0] - X_4[1] = 6 - 3
Shouldn'tX_5 = [3, 3, 3] # -> X_5[1] = X_4[0] - X_4[1] = 6 - 3
'sX_5[1]
beX_5[0]
?-edit-
ah, no, I misread
This comment has been hidden.
This comment has been hidden.
I'm not sure if that's the cause of this issue too or not, but there are negative numbers generated because of overflows.
What about now?
It's still the same.
Can you please verify and close the issue?
Not fixed yet, it just crashes with Arithmetic overflow in newer versions of Crystal instead.
Now?
I've checked your solution and my solution around 10 times, I didn't faced any such issue anymore.
Kotlin Translation
Please review, and comment or approve.
This comment has been hidden.
I was struck with the same problem so i have to complete in other languages
Fixed, my bad for leaving the code template in an older state.
Thank you! Everything works now.
Not exactly 4 kyu or Maybe the idea struck my mind too quickly.
Crystal and c++ translations kumited :)
Notification message. There's a bunch of issues with both above.
Fixed one and waiting for your feed on Crystal.
up about the crystal translation, guys.
Fixed, I presume.
Adding a small line to define
i
andj
in the description would help this kata be more quickly understandable. Just something like "X[i] = X[i] - X[j]
for alli
,j
inrange(len(X))
" Maybe adapt to be less Pythonic and more general.The kata is interesting, but the description is very confusing!
I'd replace with something like below:
Basically you pick two non-equal numbers from the array, and replace the largest of them with their subtraction. You repeat this till all numbers in array are same.
Method returns sum of all elements in the final array.
I don't uderstand the solution step.
X_4 = [6, 3, 3] # -> X_4[2] = X_3[2] - X_3[1] = 6 - 3 X_5 = [3, 3, 3] # -> X_5[1] = X_4[0] - X_4[1] = 6 - 3
Here how x_4[0] is converting from 6 to 3. I don't see any x_#[0] = something operations.
Is i not coming before j?
Who can not remember the basis of geometry must solve it with brute force (I was in last group :( ). I hope this is not spoiler. Tanks for great kata.
This comment has been hidden.
This comment has been hidden.
I guess this Kata has to import some modules. With only the bulit-in module, it will be impossible to finish in time.
There aren't any modules you need to import. However, you need to find a common property shared by all the numbers in order to use something other than subtraction to get your answer.
Or you can be really stupid and do what I did which isn't clever at all but still works.
Hello,
I think some of the first 100 random tests in python are having some problems. I got this one for example:
[9303875, 695795, 26566380, 23696555, 11215955, 32798000, 9456395, 28526355, 27082220, 2269355, 24552620, 2539520, 22499955, 12238955, 10478000, 30970395, 3772080, 2194955, 15477680, 17084720, 4322795, 33227195, 28926720, 4426955, 576755, 23939595, 15282380, 6705920, 4017755, 7777280, 26952795, 1227755, 4692780, 14418875, 3487500, 6966320, 10721195, 26438195, 28792955, 22264355, 19533875, 37519920, 23094380, 35119280, 6705920, 5954480, 403155, 1458395, 14230395, 25048620, 2700720, 3869420, 4426955, 15971355, 36010220, 17291180, 6138155, 5247680, 13580480, 35119280, 2307020]
Where the proposed solution was 12, which it can not be, as the smallest solution for any test is the length, as that would be the resault in case all the numbers were reduced to 1. For this case then it would have to be 61.
The solution I was getting is 9455, I now that is hard to check, because they are random test, but I have been having problems with some of them, when I copy them and try them in my python console I get the same result with different aproaches to solving the problem (older slower versions).
Is there any way to check if the generation of the test is being done properly?
Running this set of input with the reference solution produces 9455 as expected, so I don't think there are problems there.
Maybe you're looking at the wrong test? :)
"TypeError: Return value of solution() must be of the type integer, none returned".
I checked and the type of the variable I was returning was "integer".
I suspected the tests for php might be broken so I changed the last line of my function.
The last line of my function now simply reads: "return 9".
I'm still getting the exact same error.
Am I correct in assuming the error is not all on me? I just started learning php last week and haven't been coding for too long so forgive me if I'm not seeing something obvious.
Can anyone tell me the test inputs of the speed test.
I've just made a huge revamp on the random tests and performance tests.
PHP Translation Kumited - please carefully review and approve :D
I've just implemented performance tests in every language except PHP (I don't know PHP enough to do it).
Please see my implementation of random tests in other languages and update the PHP version to do the same thing except maybe adjusting the array sizes for performance tests as necessary (no need to kumite, just edit the kata yourself since you're already a contributor).
Thanks for the notification, I have increased the number of random tests in PHP such that my (efficient?) solution completes all tests just in time.
Well, when I ask you to implement random tests I mean "implement it like the other language does" because just dumping a bunch of random number for this kata wouldn't end up with good random test sets.
Anyways, I just took the effort to actually implement the random tests in the PHP version that is the same as the other languages.
Codewars Forums - Kata Best Practices - Have Full Code Coverage
JavaScript and Python versions seem fine, but Ruby version is lacking in terms of code coverage (only
< 10
fixed assertions in the entire test suite) which makes it prone to allowing logically flawed and/or hardcoded solutions to pass. Please either (1) increase the number of distinct, unique fixed assertions in Ruby to at least 20 and including edge cases, or (2) write 100+ random tests as per standard Codewars practice (and generally preferred over only 20+ fixed assertions)Fixed
This comment has been hidden.
I just did a massive edit to implement performance tests in every language except PHP (which you'll have to do it since I don't know PHP well to do this).
This comment has been hidden.
This comment has been hidden.
Oh, my bad then. Doesn't make for a very interesting kata once you figure that out. :P
I guess there is a typo in the description in the first test case, the last element in the initial array should be 21 and not 12 for it to work the way it's written.
You're reading it wrong. But it was not described as clearly as possible.
You have to read the operation first, then the array, which is the resultant array from that operation.
Ok thanks My Bad
This comment has been hidden.
I'm having the same issue
Fixed
Incomplete description: Explain that
X[i]
is the largest element of X andX[j]
is the smallest one.It's not required that
X[i]
must be the largest orX[j]
must be the smallest one.Why? Well, people familiar with competitive coding will instantly recognize what this kata is doing...
Whilst I have no experience on what an issue is considered here, the problem can be solved without assuming that. It will take more iterations, though...
PS: To clarify and not saturate the comments section, the above means that that is not required to solve the problem and thus not an issue.
.. but that's not an issue. :D
Haskell translation kumited.
Please review, and comment or approve.
It doesn't let me it approve your translation says
Description cannot be approved, recent changes from related record must be merged first.
Please try again.
If it still doesn't work, there are more things I can try. I'm not sure how I should resolve this problem, so I'll be trying the possibilities one by one.
It means that the descriptions have changed during the time.
Unfortunately I just changed the descriptions yet again (fixing some issues in the Python translation), so I'm afraid you'll have to copy the new descriptions again.
Well, I hope it works. :S
( What's magical about
52000
in Python anyway? )I don't know, it was translated like that.
(But then, my JS translation went even more hardcore with the numbers.)
Can this be approved, or is that still giving an error message? Or is there something else wrong with it?
The author doesn't seem to be active.
I just performed a massive edit which make the tests in all languages actually performance-heavy. Hopefully it's actually a
4kyu
now :). It also means the descriptions are also edited.You should adjust your translation to be at least the same as my random tests. (Typical way of generating random tests easily gives tests that has an answer of 1).
The instructions are way more complicated than the problem itself. In case it sheds some light to prospective trainees:
Thanks for better detailed description, just updated kata
I have updated the proposed Haskell translation with this as well.
Aha, this is where the typo in the description comes from ;) It should be
X_1 = [6, 9, 21] # -> X_1[2] = X[2] - X[1] = 21 - 9
@notliam That also confused me, indeed. In the description (original & current), each row shows the current stage: on the left, the modified array; on the right, the processing and its relation with the above array.
You propose to show the original array on the left and the modified one on the right. I think your suggestion would require changing the steps as well like this (plus some verbosity added):
Maybe it is more clear that way, I do not know.
I see
X_5[1] = X_4[0] - X_4[1]
here as well as in the description, but it isX_5[0]
being changed fromX_4[0]
.Hello - small typo on python sample tests :
""" test.assert_equals(solution ([6, 9, 21]), 3) """ <- should be 9
Cheers!
Fixed
This is way too simple for 4k.
That is your opinion, not a kata issue.
Also, good luck getting it changed.
Closing.
Dear Johan,
I'm afraid you're wrong, because I just implemented the performance tests ;-)
The performance tests are killer (well for me at least)
~edit: you guys posted an issue w/ some python code on github that is still open, so nvm for now...
JS translation
This looks like fun, but I don't really understand the directions.
Python translation added :)
Maybe the author is AFK. So sad QAQ
Good kata. We should improve the instructions as it seems that the author isn't active at the moment.
Nice kata. Thanks. But the description is really more difficult than the kata itself.
Your example is a little unclear. In the first step [6,9,21] you subtract the second-largest number from the largest (21-9). In the next step, you subtract the smallest number from the largest (12-6 instead of 12-9). This made it difficult to understand what you wanted.
I am confused about that too. Does it matter? I keep finding the largest and the second largest. I passed all tests but it takes a lot of time computing it. Wonder if thats the reason
This is a good exercise, but I thought the instructions were difficult to understand and I think they need to be more explicit.