6 kyu
Persistent Bugger.
71,611 of 206,626joh_pot
Loading description...
Fundamentals
Mathematics
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.
It would be nice to show the input when the test fails.
the c environment is broken. same code passes everything with c++ compiler but crashed with c compiler. please fix it :(
please blame your own code before you blame the tests (there have been more than 3,000 completions in C as you can see at the top of the page, so such a glaring mistake is unlikely). debug your code locally. if you don't know what input causes your code to crash, add a
at the beginning of your function (flushing the standard output stream will ensure that you will see it even if the program crashes). i tried your code in a debugger and you do not allocate enough memory in
inst->num_array
. you allocate enough space for5
integers, while a 32-bit integer can have as many as10
digits.oh yea, you're right. I should debug more. really interesting that this worked under the cpp compiler and passed all the tests. Thank you for this commend :)
This comment has been hidden.
Please don't post solution in discourse. Do read the community's Code of Conduct for more info
This comment has been hidden.
Please don't post solution in discourse. Do read the community's Code of Conduct for more info
The tests for this task aren't written correctly.
I wrote my code for this in VS Code, called the function with the test data from the kata and some other inputs as well, and the return value was exactly what's expected.
This was not my first task in which it happened, by the way.
Write your tests a little better, please.
Thank you!
Ain't a great look to blame tests when your code is wrong xD
Have you actually read my entire comment, or have you just read the first few words and decided to be a wise guy?
I'll explain it slowly.
I. wrote. my. solution. first. in. VS. Code. The. results. were. all. correct.
So now explain to me please - how come the code works correctly in one place, but not in another? Does that even make sense? Maybe I'm missing something here.
Now's your chance to actually be helpful instead of being smart alec.
You asked for a glib remark when you blamed the tests before having understood what happened ;)
Your code doesn't pass the example tests, and you can find those below the editor.
"Works on my machine" just means you didn't test the thing that failed.
If you still fail to reproduce those tests locally, then debug it in the codewars environment. I did look at your code and it is your code that is the problem, not the tests. Write it a little better please.
Thank you!
I appreciate the heads up, and I'll try to fix the code.
Your solution is wrong, and tests are not at fault of your problem. If it's not the first time you encounter such issues, it would be a good idea to review your method, and not just blame tests. The "I. wrote. my. solution. first. in. VS. Code. The. results. were. all. correct." does not mean that your tests were correct, because, appparently, they were not. You did not tests whether your solution can be used more than once, and as it turns out, it does not and it breaks after a single use. You need to fix it, and reconsider the attitude of "EVERYTHING IS BROKEN!!1!1!".
I was initializing a static variable that was not bein reset between test runs. Running it outside seemed to work, but if I ran all together then I saw the issue. Maybe that is the issue you have?
This comment has been hidden.
Quite a lot.
Thank you so much!
Already I think I managed to fail this. I just started copying the same sequence of numbers and words from the Sample Area below to see what would happen.
Excellent practice for loops, infuriating to wrap my head around.
This comment has been hidden.
7081412 From the assignment, I understood that all numbers must be multiplied by numbers, but when multiplied by 0, it automatically turns out that the multiplicative stability becomes equal to 1, does it not matter the zeros?
so ez for 6 kyu
my below code passing test but not pass Attempt. Please help me to resolve the bug.
function persistence(num) { //code me //console.log(num); if (num > 9) { let valueOfMultiplication = num ; // console.log(num); let multiplicationCounter = 0; while(valueOfMultiplication>9){ const convertedToString = valueOfMultiplication.toString(); // console.log(convertedToString); let multiplicationVariable = 1; let stringToInt; for (i = 0; i < convertedToString.length; i++){ stringToInt = parseInt(convertedToString[i]); // console.log(stringToInt); if (stringToInt != 0) { multiplicationVariable = multiplicationVariable * stringToInt; }
else{ return 0; } } console.log(persistence(7647280));
You already passed the kata, read this for the next time you post code, please: https://docs.codewars.com/training/troubleshooting#post-discourse
issue resolved by removing if condition to ignore zero . Thanks for and quick reply
i used numpy, but its clear you dont need to, and the solutions that dont use packages are still clean and fast
Kata test is wrong pls fix this. Different outputs in description and UnitTest
I guess you're wrong about that, but, could you clarify what do you think is wrong? Have you read the posts below?
Yes, my bad.
This comment has been hidden.
You are storing
counter
as a global variable, which will accumulate previous function call's computation that are not necessary.this was challenging, I learned about how to use a while loop though!
In the explanation of the code it says persistence(39)->39->27->27->14->1*4-> Answer 4
In the test case the answer is 3 to the exact same question. This kata needs correction to test case.
No, you read it wrong:
Not a kata issue.
39 --> 39 = 27, 27 = 14, 1*4 = 4
we count how many time we had to multiple in order to get to a single digit and this intence, we multiplied 3 times, we did (39) and (27) and (1*4) thats 3 times, hence the answer is 3.
Tests, basically, are wrong.
Because 239477 -> 10584 -> 160 -> 6 (I think in this task we ignore 0's) But the test tells its should be equal to 2 somehow
You are, basically, wrong. In this task we do not ignore 0.
resolved it, do not ignoring zero.
Please label as #math
Done, but this should be a suggestion, not an issue.
I disagree with that label. Basic digit manipulation falls under fundamentals IMO.
Many Python learners like myself have limited math experience, which can make even minor math in katas challenging if unexpected.
While this kata's math is quite basic, labeling it as such would help learners like me choose it with the right expectations.
Small additions like this, without lowering standards, can really enhance the learning experience for many of us.
By that logic almost every kata should be labeled as math for involving multiplication or division of some numbers. Funnily enough, too many katas do this already.
But then what label should we apply to katas with geometry, polynomials, or involving simple/complex formulas? Hardcore math? :P
This comment has been hidden.
Why testing for num = 9989573: expected 5 to equal 2 , 9989573 -> 612360 -> 0 ?
You kind of answered it yourself, no? It takes 2 steps, but you returned 5. Don't use global variables.
my code returned 2
No, it didn't. The assertion message is: "expected actual to equal expected"
Try running the same test twice and see what happens.
thx for help
This comment has been hidden.
Please don't post solutions in Discourse.
Testing for num = 1435563: expected 5 to equal 2
Why run 5 times when the second output is 5400?
Your code returned 5 while the right answer is 2.
I guess there are some errors in this test. even the description is wrong.
39 --> 3 (because 39 = 27, 27 = 14, 1*4 = 4 and 4 has only one digit) how is 39 --> 3. it i should be 4 and it's even written up there
Or did i misunderstand the test?
As of writing this, 181000ish completions of this kata, at almost 9 years old and after ALL of that scrutiny, a lone code wolf FINALLY cracked the secret, that the tests are wrong.
That's only 3 multiplications
My code runs perfectly in browser console (all the test cases are passed), but not working here. Why?
You declare
count
as a global variable, so it only works for the 1st test case with the fact that other test cases use the precedence test case'scount
valuei did the same thing, but otherwise
count
gets reset every time it loops. I'm missing something for sure, and most likely overcomplicating it...test.assert_equals(persistence(39), 3) test.assert_equals(persistence(4), 0) test.assert_equals(persistence(25), 2) test.assert_equals(persistence(999), 4)
39 --> 3 (because 3 * 9 = 27, 2 * 7 = 14, 1 * 4 = 4 and 4 has only one digit) why is it in the test 3
999 --> 4 (because 9 * 9 * 9 = 729, 7 * 2 * 9 = 126, 1 * 2 * 6 = 12, and finally 1 * 2 = 2) why is it in the test 4
25 --> 2 (because 2 * 5 = 10, 1 * 0 = 0, ) why is it in the test 2
I understood. the tests are not correct!
what do I do with this?
are you trying to say something?
I think the bottom line here is that you need to read the instructions more carefully.
If your intention is to report a problem with the kata, then 1) you're including the information that answers your question and shows how it's not a problem 2) you'll need to spell out your thoughts so that others can understand what you mean. the rest of us aren't psychic.
You misunderstood what you have to count. Example 39 is 3 because you count the amount of multiplications until only 1 digit is left.
39 -> 3 * 9 = 27 (first multiplication)
27 -> 2 * 7 = 14 (second multiplication)
-> 14 -> 1 * 4 = 4 (third multiplication)
4 has only digit, so we found all multiplications.
I understood. it is necessary to count the number of steps. The result is the number of steps
i wrote the code in visual studio code and it returns the value correct in example (39) -> 3
but when i run it here , it gives me wrong answer expected : equal to 0 actual : 3
any idea ?
Your current code works, so your old code must have being wrong.
I'm having the exact same problem, any hints you can give? Does the browser just not like local static initialization?
same with my code. Passing test but not attempt.
I have problems with the attempt i think the test are wrong
I think the tests are not wrong.
I think that your solution is wrong.
The code I can see fails the first sample test, which is explained in the description, how can you think the tests are wrong? Please read the description again.
You should leave more clear you need to return the number of steps to get the persistence and not the persistance itself, I know is stated up there but it's easy to pass that part when reading and when you concentrate on the solution.
At least letting at the end something like "You must only return the amount of steps you took to get the persistance", so there's atleast less people asking the same thing over and over.
The instructions are not that long, read them again, what you should return is there, how can you pass that? Besides,you have sample tests to see what your function should return. People should read better.
Welp, fair enough honestly. I guess people just put all their attention on one step that they forget what result they were searching for in the first place.
I dont understand what im meant to do
Love this kata, I finally understood how the reducer function works!
you mean recursive function, do you?
Expected 14 to be 3 ???
WAT
That means your code returns 14 while it should return 3, and this is not a kata issue. Please refer to the documentation and don't raise issues without a real good and proved reason: https://docs.codewars.com/training/troubleshooting/
It's just the number of multiplication it takes until the number we get is one digit.
In Rust translation there are some warnings in tests:
Fixed
This comment has been hidden.
You've already been told that code working in another version of Swift and not in the one available is not a kata issue. Please don't open more issues about that. Your code has a problem too, it won't work when called several times in a row.
Guys Can i use standart library for solve this problem?
you have a test problem for 39 it's 4 not 3
you are testing with the false value
No, you're wrong. And it's been asked and answered already many times. Read the posts below.
the test does not pass what should I do??
Reread the description:
It's not the single digit value what your function should return, it's the number of steps to reach it.
Ok Ill try again
This comment has been hidden.
Please use appropriate formatting when posting code. See https://docs.codewars.com/training/troubleshooting/#post-discourse
Why is your first assumption "the kata that has been completed over 57000 times in JavaScript must be bugged", rather than "I must be misunderstanding something"?
Also, this has been wrongly pointed out as an issue multiple times. A quick look through the discourse and you would have saved yourself some time.
Not a kata issue.
Cool thanks I didn't knew that thanks :).
There is a problem with the description of the kata. We must return the number of operations to arrive at one digit number, not the one digit number
Isn't it literally in the description?
this is my code and in fixed_tests it says "Expected: equal to 0 Actual: 3" i just wonder because i did the cases on my machine and online compiler and it's the same result
Your code fails when called several times in a row. Not a kata issue.
This comment has been hidden.
issue
is reserved for problems with kata, instead post as aquestion
This comment has been hidden.
This comment has been hidden.
OP solved it, closing
This comment has been hidden.
That's what it says in the description, it doesn't say "return the single digit".
yeah but the examples end up overiding that thought because you end up thinking "oooh I must return the single digit",instead it should have said "than return how many times it took"
This comment has been hidden.
In test answer for 39 is 3 and for 999 is 4, but in example answers are correct
Sorry, I don't get what the problem is, both in the description and in the tests the expected answer for those input values is the same.
This comment has been hidden.
This comment has been hidden.
Please use spoiler flag when posting solutions.
Please use code formatting when posting code.
If you test this exact code locally and it works for you, you are not testing it correctly. What happens when you run your function locally more than once?
???????
Incorrect answer for n=39 ==> expected: <3> but was: <4>
3 * 9 = 27
2 * 7 = 14
1 * 4 = 4
Why expected 3?
You misunderstood the task, please reread the description.
The question is looking for the number of times you performed multiplication, not the end result of the multiplication.
This comment has been hidden.
so i got the code to work in a c++ online clang compiler, when i copy the code, i get an error that for one of the answers they expet 2, but my code gives 5. i can't duplicate that error in my compiler, and i checked against all the sample and test numbers in my compiler and they worked, so im not sure what else to do.
Read this: https://docs.codewars.com/training/troubleshooting/#works-but-no
Your problem is in one of those bullet points.
This kata will be placed on my list of places to never return for sure, skipped. On to the next one.
This comment has been hidden.
Ok, after a break and some tinkering I managed to get it all to pass. I had to make a weird if statement saying if the num coming in was 1 digit then it would return 0 even if it was for the test which had a num coming in which wasn't 1 digit...
how do i get the test complete? i finished but it doesn't shows completed in my profile
Did you press ATTEMPT, and later SUBMIT? Were tests all green when you did SUBMIT?
This comment has been hidden.
Using a global var like that won't work anywhere when called several times in a row, the global var will carry previous values and after the first call will give you wrong results.
Wow thanks for your help!
No problem, you can see another way of using recursion clicking in View Solution under my post.
This comment has been hidden.
I wrong read a description and give wrong reslts 😂👌
This comment has been hidden.
If it is already a single digit number you don't need to multiply it at all to get a single digit number, the tests are fine. Is the same as the last example in the description.
@Chrono79 Thank you, you help me to understand the challenge
This comment has been hidden.
Not a kata suggestion! You can start off from 8kyu up to 1kyu then ~~
There appears to be a problem with the test case. 39->4 not 3 999 -> 2 not 4 25-> 0 not 2 Kindly correct this
There is nothing to fix, because those values are fine. Read the description again. It's the number of steps, not the single digit number you get at the end what you should return.
tried global counter; it was working on my IDE or and online interpreter, but not in codewars, so had to implement them, some other way.
can any one explain why global count is giving unexpected count. even tho it's giving correct answer in otere web interpreter!
Only because you called your function once only. Call it a second time and you'll see why it doesn't work here.
can you please explain why it was giving wrong values!
Because the global var isn't reset between your function calls and keeps its value.
yea true, thank you. i was also trying this using a custom script, and the count wasn't resetting.
The test says that 4078952 should return 1, but that's not correct cause 4 * 0 * 7 * 8 * 9 * 5 * 2 = 5040
Any number multiplied by 0 gives 0...
Thus you only need 1 multiplication to reach 1 digit number.
Please check your math there. Not a kata issue.
Whatever you multiply by zero is always zero.
bruh
4 * 0 = 0 0 * 7 = ?????
Basic math lol
so the instructions says 39 should return 4; 39 = 27 27 = 14 1*4 = 4 but the unit test for 39 is saying 4 should equal 3
It is 3, its about how many times you do the operation, not what the actual result is. So for 39 you do 3 * 9 = 27, 2 * 7 = 14, 1 * 4 = 4, 4 is a single digit, and you had to perform 3 operations to get to a single digit answer, so your answer is 3 operations. It confused me too.
very true
Very new to JavaScript language and had watched some tutorials and wanted to try solving this as a 1 liner... did not work. I had to look up some things because I was unsure if some built-in methods could be used here but sure enough they can. Very unfortunate because my rabbit hole led me to finding the solution, but a positive takeaway is that I thought about the solution; I just did not know how to code it. I knew what I wanted to do.
Advice: take it step by step. Focus on getting the individual numbers and then decide what condition to check with that number (how many times aswell).
I have a similar solution. 1 liners are kind of harder to read.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting#works-but-no
contador
keeps its value between tests.I left my solution but don't know why the test2 has an error with log "100 Random Tests ****************".
OP solved it, closing
So, should this multiply zero digits as well or only non-zero digits? For example, if you input 909, you could go 9 * 0 * 9 = 0, and return value is 1, or you could go 9 * 9 = 18 -> 1 * 8 = 8, and the return value is 2.
Zero counts as every other digit, it should not be skipped.
Thanks. I thought so, but I was making sure since any number with a zero in it would then instantly end the sequence. Also, 9 * 9 = 81, not 18 in my original comment..
Multiplying by 0 will still bring num to single digit. Treat it like any other number.
This comment has been hidden.
Hey @Ground94 and welcome to Codewars - as this is your first kata, please note: when you post code you must use the Spoiler flag otherwise everyone can see it (thus spoiling the kata for other people).
To answer your question - you are using a
global
variable, so your code will fail on then 2nd, 3rd, 4th runs of your code (on the 2nd run of your code,count
will still contain the result from the 1st run, it will not reset to0
). Remove the global variable and just use a local variable in your code, and it should be OK.You can read the Troubleshooting guide here, it will explain the most common mistakes for new users on Codewars (and this is one of them!).
Hope that helps, and good luck on your Codewars journey - you can also consider joining the CW Discord (link on sidebar on the left) if you want help as you get started.
everything works in pycharm but not here( why?
Because it doesn't work there either if you call your function several times in a row, like here. Read this: https://docs.codewars.com/training/troubleshooting/#works-but-no and don't use global vars.
This comment has been hidden.
Not a kata suggestion!
there is problem while testing
No, there isn't. You're not returning what is asked, read the description again. Or the posts below:
https://www.codewars.com/kata/55bf01e5a717a0d57e0000ec/discuss#636d5a982aae8f99a2a349df https://www.codewars.com/kata/55bf01e5a717a0d57e0000ec/discuss#6322a492933786003e2da44a https://www.codewars.com/kata/55bf01e5a717a0d57e0000ec/discuss#628d0aa9fcbd190b8326a80c
And many more.
This comment has been hidden.
Please use Solutions section to discuss solutions. This is not a kata suggestion.
Passed all tests in c++.
the tester is broken... look in example and look with what are they comparing the results :))
The tests are fine, you misunderstood what to return, it's the number of steps, not the final single digit number.
oh, realy? upsi, my bad :), thanks!
done! i should pay attention when i read :)))
This comment has been hidden.
It says digits, why would it be 9x9x90 ? or 9x9x9 ignoring the 0? 0 is a digit like any other.
zero is neither positive nor negative, only the positive was mentioned in the kata
num = 0
is not tested afaik, numbers with 0 in them as the OP asked are. Like 10 for instance.having a problem where the code editor wont recognize a globally declared variable inside of the function. I have let persist= -1 function () { //codeblock persist += 1 //more code } but the editor won't recognize persist as a variable within the function!! It works on vscode, I'm not sure what the problem is here
I have the same issue where I used recurrence and tried to declare a global variable inside the function. It works on my IDE but the code editor seems to get a different output.
This comment has been hidden.
This comment has been hidden.
A problem with your code is not a kata issue, please read this first: https://docs.codewars.com/training/troubleshooting
This comment has been hidden.
This comment has been hidden.
Yes, it is possible, and the problem is in your code, read this: https://docs.codewars.com/training/troubleshooting#works-but-no
Thanks for this fun kata, feeling smarter than I should for solving my first Kyu 6 python, LOL.
Factor translation
Approved
Rust tests generate warnings ->
This comment has been hidden.
Guys, on the first part of the test, shouldn't Persistance(39) be 4 instead of 3 ? Because 39 = 27 -> 27=14 ->1*4 = 4 [end]. My test is computing as "wrong" because of that.
You need to count the steps to get to the final output, not the actual output
I am having the same issue, the tests are wrong. The instructions are clear that 39 = 27 -> 27=14 ->1*4 = 4. While the test asserts when result is equal to 3.
you're misunderstanding the description again
3 steps
Exactly. After executing the function for num = 39, the returned value is going to be 4 and not 3.
I left my solution but don't know why the test has an error I test the code on VScode and works really great even with huge numbers
Read this: https://docs.codewars.com/training/troubleshooting#works-but-no you're using a global var.
why doesnt the reduce() callback function doesnt work here?
Scratched my head for a few minutes there. Very fun kata; thank you!
I was able to make it in my own visual studio and ported the code here. Still error but the thank you for this wonderful challenge.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#post-discourse and this: https://docs.codewars.com/training/troubleshooting/#works-but-no
Your problem is you're using a global var.
Thank you very very much
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#works-but-no
It's a problem with your code, not a kata issue.
This comment has been hidden.
To all those that don’t understand, the “Test” button only tests the current scenarios, which are normally simple and basic scenarios. The Attempt button applies your code in a real-life scenario where many numbers of all sorts and sizes are tested. In other words, your code should be able to count the iterations of ANY size number that is randomly provided when the “Attempt” button is pressed. Happy Coding.
Update to Scala 3
I also refactored the test generation to be cleaner and always produce the same number of random tests, as it was there was a chance you didn't get a full 50 random tests.
.
Hello!
Sorry, there is something I don't get it, the
2818107412499218671
input number how come to reduce to1
as the assertion expect it?You have a
0
there, why did you ignore it?Thank You! Try not to, but did not succeed :) (ps.: that was super fast asnwer!)
This comment has been hidden.
A problem with your code is not a kata issue. See the values that make your code fail and fix your code.
I checked, is it possible to check how are generated the random tests? did u checked the code? where's the problem? :O
Only after you solved the kata, but the problem is in your code, it returns 0 for numbers with a zero in them.
This comment has been hidden.
Lua translation!
approved
There is an issue...
wrong tests. 39 -> 4 not 3
It's explained in the kata's description, please, read it. Also, it was asked and answered below too
good kata, thx for it!
I'm new in coding and solving this kind of task motivates me to keep going, since they are not very difficult but they make you think a bit, thanks for great kata!
When I click "test" it says that all the tests are passed, but when I click attempt it says that random_tests fails. I manually tested all of the test examples and they all work. Anyone have any idea of why this is happening? Thanks
A problem with your code is not a kata issue, please read this: https://docs.codewars.com/training/troubleshooting/
Print the input and debug your code.
what language, what tests fail?
there are 125,370 completions of this kata, so it's highly unlikely (but not impossible) that you have found a new issue.
either way, this is not a kata issue, rather a question. please read this:
https://docs.codewars.com/training/troubleshooting/
ah @Chrono79, i see you have beat me to it
This comment has been hidden.
Your solution not being correct is not a kata issue.
Please use code formatting when pasting fragments of code.
This comment has been hidden.
s=persistence(s)
Read this: https://docs.codewars.com/training/troubleshooting
problem is solved(I forgot to reset global variable)
This comment has been hidden.
No it doesn't. Read this: https://docs.codewars.com/training/troubleshooting/ Not a kata issue.
Thanks for great kata <3
tests on C++ is wrong, pls fix it
Wrong how? C++ version works for me. What do you think the problem is?
Your code is wrong, the tests are fine. You're returning the single digit number instead of the steps to reach it.
Why in the description the input 39 return 3 but the explanation is 4?Are the test cases wrong? Description:
Please read the description again or the many posts below asking the same. Not a kata issue. 4 is the single digit number and that's not what you should return, it takes 3 steps to reach it.
Okey, thank you .
It's the right thing?
assert.strictEqual(persistence(39),4); assert.strictEqual(persistence(4),0);
assert.strictEqual(persistence(25),0); assert.strictEqual(persistence(999),2);
No, they're not, read the description again, some of them are even there. It's not the single digit number what you should return.
I understood the question, thank you.
The test case is wrong?I use javeScript,input 39,output is 4, why tell me the result shoud be 3?
How test case is wrong? This example is in the description. You probably just didn't understand the task.
Oh!thanks,is my fault,the result is the number of calculations
im writing a code that basically works in vscode, returns all the correct answers, but here it does not work and i dont know why , could somebody help me ?
A problem with your code is not a kata issue, use
Question
label for that, read this too: https://docs.codewars.com/training/troubleshootingYeah the test cases for python are weird, my code might be wrong but looking at the random tests it can't be right.
The tests are fine, your code is wrong. Maybe you misunderstood what to return.
Go translation
Approved!
Result of test cases in python are wrong
Python tests are fine, please read this: https://docs.codewars.com/training/troubleshooting
Your code is returning that single digit instead.
This comment has been hidden.
This comment has been hidden.
Not kata issue, you're doing recursion wrong.
This comment has been hidden.
Your implementation was wrong = your code didn't work. So, discuss all you want, but that's not a kata issue,
If there were a problem with the kata
All those people couldn't have passed it, right?
No, your code returned
None
. It's a problem with your code, cheers.This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Have a look at the documentation: https://docs.codewars.com/training/troubleshooting/
I found the problem, but I do not know how to solve it. It is because I have a global variable for counting. The output the tests are getting is the addition of all the count.
You should declare a new variable inside your function on each call since otherwise the variable remains from previous calls.
I am doing recursion, so declaring a new variable means the count won't work. Would declaring a new variable work with recursion?
OP solved it, closing
This comment has been hidden.
Please use a spoiler flag when you post code, so that users who didn´t solve the kata cannot see it; I put the flag for you this time. Also use markdown tags to format your code or it's not usable. Refer to the documentation about this and more: https://docs.codewars.com/training/troubleshooting/#post-discourse
Here
undefined
means your function returns nothing.Because you're doing recursion wrong.
I can tell there is something wrong but I don't know what is
You could try an iterative solutions instead and then see how recursive solutions work when you can access Solutions page. Don't use global vars, they keep their value between tests.
This comment has been hidden.
it really helped. Thanks!
This comment has been hidden.
You're returning the single digit number instead. Please mark your post as having spoiler content next time.
So in the case of 39, the result is supposed to equal 3. I'm confused as to what I'm supposed to multiply the single digit number by (4 in this case) that'll equal that digit.
You're not supossed to multiply that last digit with anything. You should count how many times you need to multiply the number's digits to obtain a single digit number
The single digit value is not related to the expected value.
I get this when I try to test:
Traceback (most recent call last): File "main.py", line 2, in import codewars_test as test ModuleNotFoundError: No module named 'codewars_test'
I tried removing the first line (import codewars_test as test) and then I'm able to make the test (which I succeed). But then I can't attempt for the same reason.. am I doing something wrong? I tried resetting the kata
Save your code locally, make sure you have the latest language version selected (Python 3.8), push
reset
at the bottom page and it should fix the issue.It worked, thank you :)
1 should equal 2 Why? in xmpl we have: "4 --> 0 (because 4 is already a one-digit number)" hence "1 --> 0 (because 1 is already a one-digit number)" Why tests say: 1 should equal 2?
1 is what your function wrongly returned, not the input value.
For Java there should be tests for big numbers. Because all recursive functions (like mine) that have parameter long and return an int would fail ;)
EDIT: I'm wrong, my solution would fail for big numbers, but not because of this.
This comment has been hidden.
is there any problem with the results in C# ?
Thank you
OP solved it, closing
This comment has been hidden.
It fails when it's called twice in a row.
Your code is buggy, not a kata issue.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
it means your function is returning 0 when the correct answer is 3
oh I see, that s because I was return 2 values. one from the first fumction and the second from the other function.
Your
persistence
function takes no arguments, when it should be taking 1, and using it. Right now you are working only with 1 fixed test outside of function, that is not going to work for any other inputs.P-NBLT please read this: https://docs.codewars.com/training/troubleshooting#post-discourse and mark your post as having spoiler content next time.
yes I didn't think about saying spoil. will do if needed next time. And I can't seem to edit my post. I alos figured the solution in the meanime but thank you for your input, I appreciat it.
This comment has been hidden.
I did follow indentation rules
Please mark your message with a spoiler flag when you post code (I put the flag for you here). Also, use markdown tags to format your code or it's hardly usable, especially in Python: https://docs.codewars.com/training/troubleshooting/#post-discourse
This comment has been hidden.
First of all, you need to
return
the expected value to pass the kata.i try return too, its not working
Read this: https://docs.codewars.com/training/troubleshooting
You're doing recursion wrong and using global vars (these two problems are related, try doing it right and not using global vars).
if there is a number eg 16 ,result is 0;
i solved it in javascript and i don't think the description is very clear, author can explain a little bit more about the question so the solver can understand it better rather than looking at example solutions and getting confused.
it raises no module codewars_test
Select python version 3.8 at the top.
thanks
Attempt test are broken.
Broken how? What language are u using? Please use question label and read this https://docs.codewars.com/training/troubleshooting
I think TDD of this kata is wrong!
How? This kata has 111,406 completions.
I've got this error "expected 4 to equal 3" I thought maybe it's TDD error because 4 should be equal 0
4 isn't the input, it's your returned value, and 3 is the expected value. You can find which input caused it by logging the argument to console.
This comment has been hidden.
No.
it says no module codewars_test?
This comment has been hidden.
You need to return the number of steps, not the final value.
Thank you very much for the feedback, I was not careful when reading the instructions!
COBOL translation, with description updated.
Approved
This comment has been hidden.
First read this: https://docs.codewars.com/training/troubleshooting/ specially this part: https://docs.codewars.com/training/troubleshooting/#post-discourse
Instead of changing return for print, print the result of calling your function:
About your code's problem, print
nString
at the end of your while, the problem will become evident.It seems you solved it, remember to resolve the question.
This comment has been hidden.
Hi. I guess you're using JavaScript (please always state it clearly when asking for help). You can easily print the input and control what your code does. See there: https://docs.codewars.com/training/troubleshooting/
Many thanks :)
Guys. I tried to code this task using a recursion. But my variable, what counting a steps, always change because of recursion. Someone can help me with that?
You should ask on CW discord (check forum section on the left), faster response time there.
Mr you should run in debug mode I had the same problem Check type of value probably you need some pars methods
used global variable as counting step on my recursion...didnt work but it works on jupyter notebook. Any help would be apperciated.
It won't work there either if you call your function several times with different input values like in the tests.
I'm using javascript, recursion function still doesn't work. It's frustrating.
Probably you're doing it wrong, there are plenty solutions using recursion in javascript.
I found the solution, for my case, my counter was initialized outside of the function and I return the value of it when the condition in the question has been met. So when the tests runs the function the value of the counter stays the same after you called it for the first time, the test doesn't reset your counter. That's the reason why my kind of recursion doesn't work.
I'm getting this error when submitting Rust
These are all warnings. What's the actual error here?
Never mind, I saw now that was actually other error
I dont know why 7kyu katas are more harder than 6kyu katas... :(
This comment has been hidden.
Hi. Stack Error is a problem in your code and not a kata issue. Post a question, rather. Issues are things that need to be fixed in the kata itself.
Correction: Error is
Stack Trace
My solution works fine in my IDE. No logical error on my end. Pls advice.
Stack whatever, the problem comes from your code. And in your IDE you can only perform a few tests. Read this: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
Fixed here. Please check and approve.
It got approved o_O
Kotlin attempt tests throw warnings:
Tests need to be updated. not many codewarriors with 10K+ honor who also use kotlin :( any chance you might fix it? should be trivial and quick to do. (this will probably invalidate some current 1.3 solutions)
You can fork the existing translation, and publish it with fixes applied (and it can be approved by someone). I don't think you need 10k honor for that o_O
Didn't know that, good to know.
Fork ready for approval
Approved a while ago, you still get that warning?
Perfect, thanks :)
This comment has been hidden.
No it doesn't work. It only 'works' because you use console.log to ouput the value. Try changing the last line to
console.log(persistance(999))
and remove every singleconsole.log
inside your function. See if that changes the output.As as extra hint, get rid of the global variable (first line) and move it inside function, or you will get unpredictable results. Also, look up how recursion should be done correctly.
This comment has been hidden.
You still have
console.log
s in your solution. There shouldn't be any. And from what I see the problem is the same, your recursion is wrong.all right. having console.log shouldn't be a problem anyhow. if log show correct answer and followed with return statement then should ok. i guess u test for soluton which fits ur specific pattern. not a pleasure to deal with kata.i'll just skip it. have a nice day
That's because you don't understand the difference between console.log and return.
Please read this: https://docs.codewars.com/training/troubleshooting/#expected-the-same
Actually, that's the entire problem... And if you don't think it is, why is your solution not accepted?
for some other reason
Well, it's your choice to believe in some fairytales, and not the truth. However, don't be surprised to see the same thing when you do another kata involving recursion :P As a popular phrase goes: "you can lead a horse to water, but you can't make it drink"
i don't mean to be rude or say i can't be wrong. just no one points me a mistake and i can't see atm. i facing some issues in other katas mb once i realise my issue i will come b to fix code of this one.
It's not a good idea to just tell the answer, or you don't learn much from it. But Chrono79 told you what the problem is exactly, and I tried showing you small steps, so you could find the problem on your own ( but you didn't seem to try them :/ ).
If you do recursion, and use
console.log
in it, you will get the answer in console, but nothing will bereturned
to you from the initial function call, so you get undefined as the final answer. CodeWars tests work with returned values, and not output from console.It doesn't mean you can't use
console.log
, but you should understand that your code doesn't stop on that line. That's why I suggest looking at recursion online, compare with your code, and see if something is missing.that's the way i understand. it's just not easy sometimes to explain me things. Now i just one line and code i wrote worked. prolly i won't get points cuz skipped already but still and i learned.thank you!
JS Test is not correct. It expected 3 for 999, but it should be 2
No, you're misreading the logs, for 999 the expected value is 4 as in the kata's description.
Java basic tests are not correct, as you need to use a static method and declare a static variable which holds the value for the next call and can not use recursion..
The tests are fine, if you want to use recursion that's up to you, and it is possible.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#works-but-no
This comment has been hidden.
expected:<1> but was:<0>? random:120820 return 0; my code return:0. Because it does not work?
The expected value is 1, 0 is the expected value when the number is already a one-digit number, and 120820 clearly isn't.
This comment has been hidden.
I marked my question as spoiler and now I can't see my own question to re-edit it...
There is indentation error in what I pasted above (the last line). I just checked my 'real' code and that was indented correctly, I must have made a mistake when pasting my code into the question.
I can't, however, re-edit the above question as it's hidden from me, even though I wrote it :D :D
This comment has been hidden.
For anyone else struggling with formatting in these comments the markdown approach was shared by another user in another kata so sharing it here: https://github.com/Codewars/codewars.com/wiki/Markdown-Formatting
This comment has been hidden.
Your code being wrong is not a kata issue. Check how recursion works (your problem is there) or use an iterative solution.
The task is completed, but the tests are not running. Checked for IDEA. All conditions are met correctly.
Something's wrong with this test. My program runs properly in my VS but here I have only wrong tests.
The tests are fine:
Read this: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
Occam's razor suggests your outside environments to not have the same tests as on Codewars.
Upon displaying
digits
insplit_function
to the console, this is what I got:[9, 3.9, 0.39, 0.039, 0.0039, 0.00039, ...]
. The list goes on with floating point numbers up to 240 digits... The desired output should be a list of integers instead of floating point values.Side note:
c-x
is equivalent of doing nothingI'm just starting to learn how to code. The test for this challenge show me this at the end.
Test Passed: Value == 3 Expected: 0, instead got: 4 Test Passed: Value == 2 Expected: 4, instead got: 9
I don't understand what went wrong with my code. I want to ask for a hint, maybe I need to do an if statement to fix the first error and another loop inside of the loop I already have to fix the second error. I just trying to fix this but I don't understand exactly how. Thanks in advance for your help.
Maybe, but without seeing your code, nobody can tell for sure. Read this: https://docs.codewars.com/training/troubleshooting
If you print out messages about what you're doing in the code, you could then read those messages to get an idea of what happened. You'd probably want to start with what the input and output was, consider whether that's indeed wrong and then start looking what happened between those.
This comment has been hidden.
Your code doesn't do that. Also, returning inside the loop like that, the code inside it runs only once, because
return
exits the function.This comment has been hidden.
This task hasn't fixed the bug yet. Basic tests easy competed. But there is a problem with random tests. It seems I should skip the task.
The tests are fine:
Please fix your code. Read this: https://docs.codewars.com/training/troubleshooting
There is an error in the python tests, because the instructions have the example 39 which outputs 4, but one of their tests is 39 which they then say should output 3. Can someone please check this and see what is going on
Sorry but no. The description says it expects 3 for 39, which is correct (3 * 9 -> 27 (1) : 2 * 7 -> 14 (2) : 1 * 4 -> 4 (3)), and that's what tests expect too (test.assert_equals(persistence(999), 4)).
Someone fixed it.
errr... no, nothing changed. Only your code ;-)
This comment has been hidden.
Hi. Please use markdown tags to format your code or it is very discomfortable to read: how-to-format-code-in-markdown.
Don't post a solution like that, without any comment, it makes no sense. Do you have any problem?
Agreed
I solved it using recusive and when i saw other's solution i just want to bang my head with the wall !
I'm trying the recursive approach as well, so I'm wondering did you used static instance variable to keep the incremented value or? I cannot seem to pass the tests, even though for all of the numbers it seems to be working...
The instructions were a bit weird, essentialy its asking you how many times you can multiply the numbers together till you have one digit left, not the last digit.
That's what it says.
seems to be wrong 39 is 4 25 is 0, and shouldn't 4 return 4?
Read the description again, you should return the number of steps, not the last number.
This comment has been hidden.
Print the input, what does your code return for 25?
Input 1... Thank you for pushing me to a solution.
OP oslved it, closing
wrong test - in description persistence(39) # returns 3, because 39=27, 27=14, 1*4=4 # and 4 has only one digit but, in the test
test.assert_equals(persistence(39), 3) are all other tests wrong?
persistence(39) # returns 3
andtest.assert_equals(persistence(39), 3)
what is wrong there? The tests are ok.This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#works-but-no
OP solved it, closing
On random test in Ruby I get: Expected: 1, instead got: 4
Really no idea how to get help with this one. It works on the basic tests and on my machine. I see a lot of other people are also having similar problems, but I don't know if they've been fixed?
Read this: https://docs.codewars.com/training/troubleshooting
Print the input, see why your code fails. Also note:
The python attempt and test cases are wrong.
No, they're not.
Yes they are. The 999 problem states that the answer should be 4, while even in the description it says that the answer is actually 2.
2 is not the answer, it's the final number, you need 4 steps to reach that single digit number, and the answer is 4.
This comment has been hidden.
You're doing recursion wrong.
This comment has been hidden.
Don't use global vars, they keep their value between tests and calling your function more than once will give wrong results.
Thanks
something wrong with python version, says "25" should be zero, this is obviously wrong???
You are misreading the logs or the error messages:
There is nothing wrong with it. Please read this: https://docs.codewars.com/training/troubleshooting
you mind explaining to me how 2*5 doesn't create a 2 digit number?
That's because
2*5=0xA
, duh!But no, seriously: it's not what Chrono meant. He means that you might interpret logs incorrectly, for example associate log entries with another failure message than you are supposed to. Read this paragraph to see how matching log messages and assertion messages can be confusing: https://docs.codewars.com/training/troubleshooting#print-input
This comment has been hidden.
No, your code is wrong. It fails with inputs like this:
1492616576
. Please read this: https://docs.codewars.com/training/troubleshootingYou are right. I am wrong because of compiler. (int)(int/int) != (int)(float/float)
This comment has been hidden.
Not a kata suggestion, and please don't post solutions on Discourse.
This comment has been hidden.
This comment has been hidden.
agokhandemir that happens in any other place, not only here, if you call your functions several times. It's not a problem with the tests.
not accepting test cases persistence(39) == 3 // because 39 = 27, 27 = 14, 1*4=4 // and 4 has only one digit
if i run this my compiler it return 3 (correct) In this Excpected<0> Your output<3> i didn't understand!!!!!!!
You're confusing the logs. The next test expects 0. Maybe you're using a global var?
Thanks buddy, I got it.....
That was indeed a persistent bugger.
I have no idea what was the problem but somehow it was fixed. The test was running ok but the "attempt" didn't had enough feedback to guide me to the issue.
Now I'm curious about why it wasn't working before hahaha
I have passed all four basic test cases in my compiler but when i test my code here there is an error regarding expected output in basic test case. Further there is no details regarding which test case is failed. kindly guide in this regard. Thanks
i have the same issue. it works perfectly in vscode but here it gives me an error for some reason
Hi, how do I view the solution? The "View Solutions" tab is not available at the bottom of the screen, only Skip, Discuss, Reset.
https://www.codewars.com/kata/55bf01e5a717a0d57e0000ec/solutions/javascript
Thanks, that link says I can't view the solutions, that I need to rank up. How do I know what I need to rank up to?
I think it was at least one level below the kata's rank, try again when you're at 7kyu.
This comment has been hidden.
These are the Sample Tests for Ruby:
Click reset if you see other thing.
Nope those are the default test codes. It shows the same after reset. Even if I change them manually, everything test case pass for test but not attempt and submit.
It seems you're confused about what to return, it's not the number with a single digit, it's the number of steps it takes you to get it.
ohh okhay
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting/
Crystal translation
Approved :)
I've attempted a translation.
NASM Translation
Bumping this.
I am new to C and have created a recursive function that solves this problem when I run it in Code::Blocks. I am just curious why it does not work for this Kata. Could someone explain why? Thank you in advance!
it's generally possible to solve this kata recursively. if it does not work for you, you probably have some mistake in your solution. Maybe this would help https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution ?
I have figured out that the counter variable I am using is not being reset after recursion. Working on solving it now. Thank you for providing the resource as help.
Really enjoyed this one! broke the process down on paper but had to do a little googling on implementation.
Tried did it recursively. So I did little modification of the function, by adding a second variable for counts. And I didn't pass the tests, but my counts, what I return from function, is exactly in the same, as test result.
It is correct, but you are missing one keyword, currently your
else
statement outputsNone
regardless of the base case.This comment has been hidden.
This comment has been hidden.
Not a kata suggestion, please don't post solutions in Discourse.
Sorry man, I thought the answer with spoiler mark only shows up in sugestion, my bad
This comment has been hidden.
OP solved it, closing
Please read the description of this kata carefully - the tests are correct!!
Much easier to solve this iteratively than recursively. I personally had some testing issues when trying to import my recursive function (including a global count variable) into CW. I passed the 4 basic tests in my IDE, however when that same code inputted into CW the some of those basic tests failed. Maybe CW didn't like that I had declared a global variable within a function? Can anyone shine some light on this - Thanks
A global var keeps its value between tests, it's not a CW thing, when you call your function several times in a row, it'll fail everywhere too.
Yeah ok makes sense, thanks
What I did is I created a new function and pass on a local variable inside the persistence method. For some reasons, the code doesn't wanna exit as soon as it reaches a return line. wth.
Persistent bugger, aren't you?
This comment has been hidden.
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Why do you think the input number is less than 1000 for every test? It's not.
yeah I see that now. i am having trouble with adjusting my number if there is no 100s or 10s spot. what can i put in my ternary expression instead so that it just returns the number and dosnt replace it with a 1;
Rust: The following 2 unnecessary warnings appear in the main test:
Reraised as issue
There seems to be something wrong with the Rust random test cases. The code won't even compile when attempting (it does when testing).
I don't see any such issue. The
persistence
function there has the correct type signature (at least now).This comment has been hidden.
Not a kata suggestion!
This comment has been hidden.
This comment has been hidden.
Hello, please don't post solutions in kata's Discourse. There is a Solutions section for that matter, and even there, mark your post as having spoiler content when they do.
From Kata task: persistence(39) === 3; // because 3 * 9 = 27, 2 * 7 = 14, 1 * 4 = 4 and 4 has only one digit
Why so? (39) === 3 or 4 ?
It says
persistence(39) === 3;
This comment has been hidden.
Please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Thank you so much! I was able to make it work!
Have a nice day man
You're welcome, have a nice day too.
This comment has been hidden.
I think you misunderstood the problem. The one asked isn't the result on the last iteration but the
total number of iterations
before reaching the last one. In essence:Which gives you a total depth of 4.
Whoa! Funny enough I didn't see that. Thank you for this. I really appreciate
Please help : Either the tests are wrong or I have misunderstood. I am multiplying the digits until I arrive to 1 digit and the expected answer is 1 above or below what I got. So, I change instead of multiplying the digits, I did adding them and now the Expected is completely different.
Hi, read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Multiply the digits, as the examples in the description, don't add them:
Which language are we talking about here? We can't see your code either.
This comment has been hidden.
This comment has been hidden.
Because you're doing recursion wrong. See what your code does here: http://www.pythontutor.com/visualize.html#mode=edit
@Chrono79 Thanks again for the help and the great resource you linked. Recursion was happening however since I did not return the recursive function the value was being lost as the recursive stack frames unwind. This was solved by simply returning the recursive function or could have also been solved by using variables declared outside of the function that's state is changed by each recursive call.
Thanks again for not giving me the answer but pointing me in the right direction.
No problem, about using an external var, watch out because it'll keep its value when your function is called more than once, and that'll give you wrong results.
A little push in the right direction is the way to do it. At least for me.