5 kyu
Luck check
692 of 4,691SundaySalsa
Loading description...
Strings
Mathematics
Puzzles
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.
For some reason, I was getting an error message that my Ruby code was not throwing an error when I pushed "Test" although my code had this line: raise ArgumentError, "string with illegal characters" if str.empty? || str.match?(/\D/). By commenting out this line, the test passed. However, it wouldn't pass when I pushed "Attempt". So, I removed the # to uncomment the error line before pushing "Attempt", and the code was accepted.
is obviously wrong - can't catch an exception that has already happened. that needs to be in a block. still strange that it doesn't complain when it doesn't raise when that's what it says it tests for. doesn't really matter though, unless this also happens for MRI 3.0.0
No random tests in Ruby
Ruby 3.0 should be enabled
The overall idea is quite interesting and unique. I have to admit, though, that the requirement of an exception as a possible output seems to me unjust and unnecessary, especially in context of a FP oriented language.
python new test framework is required. updated in this fork
Approved
Ruby Tests are broken.
Proof?
No proof, no issue.
Well that is one way to handle issues. Is it a good way? Judge for yourself.
It's not enough to just say something is broken; very often it is in fact the user's code, not the tests.
So, the appropriate way to handle issues is to provide details, explaination, examples, or evidence of the problem, as can be seen in the post just before this thread.
But there is no issue presented. Issues like "tests are broken" or "it works on my PC" are simply useless, because you're giving no tangible information to track or fix.
At least you mentioned the language, which is a start, but what next? Should I (or anyone else) go and debug the entire tests suite, trying to find a bug that probably doesn't exist? Keep in mind, I don't even know what I'm looking for, because there is no mention of a test that causes the failure, an error message that could help narrow it down, or... anything?
There are a few issues already raised below, so you should check them first to see if it's already reported, or provide as much information as possible. The kata is 10 years old (originally published in Ruby), so I expect there to be some issues like poor feedback or lack of tests... but that has to be specified in issue report.
There should be more random tests for odd number of digits to avoid solutions like this.
I really remember this fun tradission from my childhood. But author did not tell the the most interesting part of the belief: when you have such ticket you should eat that ticket for luck to work !!! )))
It would have been tougher to figure out without reading the comments, though nice kata, I do not think it sucks.
The invalid tests are not working properly. I tested the same string that it was saying should return False and it does in my IDE.
This comment has been hidden.
That's a problem with your code, not a kata issue. Print the input like this:
console.log("*"+ticketStr+"*");
Having a little trouble with a particular test case. Test case "124X212", In this test case the middle character is not apart of the arithmetic required to return if the lucky string is true or false, and therefore does not throw a python error. Does anyone know how I should handle a case like this?
Thanks
Hey @KiplandDynamite - basically the kata has some input validation tests; at the bottom of the description it's saying "It should throw errors for empty strings or strings which don't represent a decimal number.".
So it's actually up to you and your code to raise a Python error when the input is "invalid" i.e. when the input string is empty, or does not represent a decimal number (which is the case here).
If you are new to Python, or haven't encountered this yet in any case, the way this is done is with the
raise
keyword - you can Google something like "raise exception Python" if you want more details and some examples but basically the syntax is:if (bad condition is True): raise
The Python official docs aren't super helpful with examples, but this site is quite nice https://realpython.com/python-exceptions/ - disclaimer: I'm not paid by/ or endorsing them ;)
Awesome Thank you! I wasn't sure if the interpreter needed to fail on its own or if I could throw my own exception but throwing my own exception worked! Thanks Benjaminz!
This comment has been hidden.
This comment has been hidden.
I leveled up to 3 kyu with this one!
something is off with the invalid tests,they are not properly working.
Your code is wrong, it's not throwing (or raising in Python) errors. The tests are fine:
Such a trash kata which doesn't work correct in Python 3.10 here while in PyCharm it works correct
Hi @marblemar61e - the kata works fine (as does your solution in fact); you're just missing one small detail from the kata:
"It should throw errors for empty strings or strings which don't represent a decimal number."
Your solution works for all the inputs, except ones like
'6F43E8'
for which your solution must throw (raise
) an error.thanks it worked but the kata is trash anyway.
This kata would've taken half as much time if the description explained that the middle digit of an odd-length string needs to be ignored. I spent ages trying to figure out if I was supposed to include the middle digit on the left or right side and finally realised you're supposed to ignore it completely. Instructions could do with being a lot clearer
It's already in the description:
Read it again.
Ahh this happens to me all time lol, thanks for pointing it out
Java translation https://www.codewars.com/kumite/6377c8af0ab4f00057e3580e?sel=6377c8af0ab4f00057e3580e
Please approve it. Thank you. I also clarified and improved the instructions, as many people here have wished.
Rejected, see comments under the translation for details. Feel free to fork the translation, fix the issues, and republish.
"124X212" the only test case i'm having a problem with, i tried everything but it's not working, what's the catch? btw on Py, cheers.
I think I've run into an issue. The "6F43E8" test case passes when I "test" my code, but if I "attempt" the problem, the "6F43E8" attempt case does not pass. Why would it pass on the "test" but not on the "attempt"? Every other attempt and test case pass.
Select Node v8.1,3, that's not the input your code is failing. Not a kata issue.
It seems to persist when i select Node v8.1,3. Can you explian how it is my code that is failing, rather than a kata issue, if the "test" case passes, but then the same argument failed when "attempted"?
As I said, it's not the same test that fails, they are the next two. Changing Node's version makes that clearer, it won't make your code pass.
That's all green, the next two fail.
Ah, I understand. Thank you for your assistance and re-explanation! I can now see this is not a Kata issue now, and was able to fix my code and solve the kata.
return false/False
or
throw "Error"
string "6F43E8" must throw "Error" like instructions say at the end.
Scala translation
that's fun!
I ate a lot of lucky tickets.
some of them were pretty tasty (:
The usual ways of error handling for integers, etc.. will not work here. So do not use them unless you want to waste a lot of time like I did. Great kata!
Yes, I made the same experience. I have translated it to Java. I proposed to clarify the instructions.
kata hint != kata suggestion
Oops. Sorry.
what's sneaky about this kata is that: it throws an invalid character at the middle of an odd len string e.g. "54L36", so that limits what you are doing... Thanks for the great kata.
Swift translation kumited
Javascript:
Couple of conditions mentioned in the description are not checked in the test cases
JS: I have a problem with '6F43E8'. The test doesn't work right at attempt stage, please fix the test. I throw error of false - it doensn't pass.
That's not the input of the test your code fails. Not a kata issue.
I have the same problem. How do I solve it?
same problemo
it works with every other case , even this case works when i console log it . just doesnt work when testing .
To clarify: Please print the input. '6F43E8' is not the input of the test where your code is failing. Read this: https://docs.codewars.com/training/troubleshooting/#print-input Your code fails another test case. Also if you select Node v8.1.3 you may get clearer error messages.
This comment has been hidden.
The reason why your code fails apparently only on one test is because tests are very weak. This code should fail on several cases. It is clear according to the description that this is not a valid ticket, isn't it? You should try to print how you actually split the string...
Fixed it, Thanks
This comment has been hidden.
Print the input with this:
console.log(`"${ticket}"`);
Damn dude, thanks for the tip man. that was so sneaky
This comment has been hidden.
This comment has been hidden.
Once again: USE A SPOILER WHEN YOU POST CODE.
I put the flag on for you twice.
I also gave you a link to format properly your code.
Sorry for that, I haven't seen your reply the moment I post the previous message. Thanks for telling me this!
Type 'Number' doesn't exist, only 'number' (not that it's the solution, but something you should know)
Yeah, sure! I just googled some info bout typeof, therefore tried Number, but initially I had number. If you don't mind answering, do I really need to use try and catch or throw is okay in that case and the only mistake I made is the logical one?
Why my kata is wrong if function return 'Error" for test example '64BC53?
You should throw or raise an error, not return a string.
this case doesn't work in js as well. I do this
throw new Error();
the test case still does not passbukenoff, print the input, that's not the input of the test your code is failing. Read this: https://docs.codewars.com/training/troubleshooting/#print-input
This comment has been hidden.
You should use the
raise
keyword.This comment has been hidden.
You don't need to
try
/except
the error, justraise
it.Thnak for your help
now it work
hello, in prerequisites basic test i have a error "It should give an error for invalid input: True should equal False" but code is fine, all another tests passed. Work with Python, what do i do wrong?
Print the input, see why your code fails that test.
You should raise an Exception rather than
exit()
.How 181 supposed to be true?
Read the description again, 1 = 1
This comment has been hidden.
This comment has been hidden.
As long as the given input is not a string or any characters within it are not digits, you should raise an Exception. Currently, you are only checking whether the string is a single-spaced character and any of the characters in it is
NaN
, which is not feasible because strings can only store characters.Empty strings aren't tested in Rust.
Added.
In what cases it should throw error? I throw error when ticket is '', or it contains one digit, or it contains only spaces, or it contains any symbols that are not Number, but I still cant get pass check. expected [Function] to throw an error
Print the input: https://docs.codewars.com/training/troubleshooting#print-input
Extra help: Use
console.log(`"${ticket}"`);
and select Node 8 for a cleaner log.I’ve tried this kata with Python, and the main difficulty to me was that slice indices must be integers not float (in Python). And what a surprise I got, when I looked through the Best Practices and saw that some Solutions used float as slice indices, then of course the solution can be short and nice. The problem is, that I tried to solve this kata once again using the copy of Hacker Sakana’s solution, and of course it doesn’t work, while slice indices must be integers not float. Could anybody explain this to me? Thank you!
Probably it's a solution in Python 2.7 where
/
was integer division.Go translation
Ok, so if anyone is wondering why your JavaScript code is failing the tests for non-decimal strings it's because you're not actually supposed to throw an error (despite what the description says). In the event of an empty or non-decimal string simply return false.
Fixed. Now the tests expect you to throw an error, as per description.
Thanks, that one took me a while. Thought it was looking for a particular error message!
yeah not an 5. But very nice kata anyway.
Rust translation
approved
JS: Node v14 should be used along with its appropiate assertion tools (Mocha + Chai). Refer to this and this
.
Updated
I know old katas are mostly overranked, but this one is way past the line.
This comment has been hidden.
(Python) My first solution sometimes passes and sometimes don't: it doesn't check the middle character. It should be checked if solution handles wrong middle character correctly (raises exception) on something like "123X204" and "353f429". Add this (or similar) to test cases please.
Added a test case for '124X212', waiting for approval (author inactive): https://www.codewars.com/kumite/62cfe4a1e73c9a002a4a5422?sel=62cfe4a1e73c9a002a4a5422
Hello, i used Try/except of Python
My program give "False" for testing 2544470768F9
but the test says :
Testing for 2544470768F9 It should give an error for invalid input: True should equal False
Is it forbiden to use Try/except ??
OK I don't see : "It should give an error for invalid input",lambda: luck_check('6F43E8')) Curious....
The description explicitly states:
You can use try/except if you like, but if you catch an error you need to raise it again to pass the test.
Might be the easiest 5kyu I have seen so far. I guess it is too old right?
Python 3 should be enabled.
I don't understand problem description, how this number is lucky: 17935 # 1 + 7 = 3 + 5, you are not adding 9 anywhere and do not mention nothing about this. Can anybody explain, please.
Pretty self explanatory given the example, if the length is odd, you should ignore the middle number when adding the halves. You should test the string represents a valid number tho (the middle char included.)
That is not as explanatory as you think it is, for example, if I would have this string 12341, I can think that obviating the three numbers in the middle, the answer is True, however as you previosly explain is just the number at the very middle. This kind of things most be explicit explain in the problem description.
Obviating the 3 middle numbers seems rather odd to me (and that's not what the example shows), but if it helps, I've added a comment in the example you found not clear.
Please fix this error in test cases:
Not sure what you're talking about. The tests are working fine.
Old Katas are overranked damn
JS: The error checking tests were completely bonkers and hence doesn't actually check for errors at all:
The correct way to use expectError is to wrap it into another function, which the test fixture then execute things inside to catch possible errors:
Is it fixed right now? I throw a Error for random tests with letters but failed. Letters should not be allowed right?
Fixed
Hey, @SandaySalsa! Description and Ruby test cases aren't clear! Or this kata like puzzle, guess what should be:
luck_check('6F43E8') == false
orluck_check('6F43E8') == 'Error'
or end of program :) Andluck_check('')
in test cases will be an error.Yours sincerely, FridayBachata
Ruby: on the test's if I had raise "Error" if (wrong type,etc)...then the tests would fail. But if I put return "Error" if (wrong type,etc) they passed. The opposite is true for final submission.
When I detect a string containing a non-numeric character, I'm using "throw" in JavaScript, but the testing framework doesn't seem to think I'm raising an exception. What's going on?
This comment has been hidden.
You should raise an Exception instead of returning false for invalid inputs.
(Ruby) cannot pass the invalid string input tests (only for submit, test button works). If I return 'error' or false, I still get: "The function somehow worked with wrong input"
"6F43E8" "Error" The function somehow worked with wrong input "1234 " "Error" The function somehow worked with wrong input "" "Error" The function somehow worked with wrong input
Same.
Same. The author did not fix it. 7 years passed.
Pretty sure the test cases are broken. Once you get to random testing, the tests expects you to return false on invalid input instead of throwing an error. Also the description should probably be more detailed about dropping the center character on input with odd length.
This comment has been hidden.
If you're given an array of odd length, you won't call
int
on the middle element.This comment has been hidden.
This kata is misleading. It doensn't require you to have an even string, it requires that you split the string in half evenly, for example '12345' --> '12' and '45' omitting the center number, and then checking to see if the sums are equal.
I agree. The kata itself does not tell, but the random tests do.
what shuold i return for invalid input.
That depends on whether you trust the problem description or the example test cases. The problem description specifically says to return False (actually, it says to return 'false', but that's the kind of sloppy error I've come to expect on this site). The example test cases, however, check for an error to be thrown. You have to throw an error for the tests to pass, making your code explicitly not correspond to the problem statement.
Kata description edited; on a side note, proposing edits it is a bit more constructive than talking about general "sloppiness" (now both capitalized and uncapitalized name for boolean appears, but I could not change that part of the text according to the specific language in use); just sayin', no polemical intentions here.
And when you ask for question, also always mention the language: it helps us to fix things :)
And translated into JS too ;)
Translated it into Python: once approved, answer me on this message and I'll fix the issue with the example test cases and then translate it in JS too (if I do it now, having slightly modified the description, I fear it will cause some problem).
IMPORTANT TO PEOPLE TRYING THIS KATA: the sample test cases are broken, so the third won't work; try hitting only the "submit" button and don't waste time wondering what's wrong with your code because of it.
And I hope a mod fixes that soon :)
Thanks, I was going nuts trying to figure out what the heck was wrong.
My pleasure, fellow code-warrior ;)
It seems like submit tests for Python are broken too. I tried throwing AssertionError and ValueError, in either case the test that expects error (6F43E8) doesn't work:
Note that all tests seem passed, yet the console frame is red, and the kata is not considered solved.
To fix this, use
Test.expect_error("It should give an error for invalid input", lambda: luck_check('6F43E8'))
instead of
Test.expect_error("It should give an error for invalid input", luck_check('6F43E8'))
Mh, it behaved strangely, and I had to create a small function to check for errors, but now it should work; I used your idea for the easiest case in the sample cases, though.
Thanks for your feed and let me know if the fix works fine :)
[Ruby version should now be fixed too!]
Works like a charm now, thanks! :)
Ohh, good! And thanks to you :)
If you are going to test for an exception being thrown, you should probably edit the problem description to not specifically state that the code should return False for illegal inputs. Also, references to 'true' and 'false' should be changed to True and False for any Python kata.
Just be aware it checks string like "123 ", number + space. That is tricky part )) Have fun )))
Something seems amiss. When I developed a code that passed all the test cases, it failed in the submit. Furthermore, the 'best practice' code passes the submit, but fails the test cases.
There was some bug in the older code: let me know if now it is all fixed :)
.
Not going through because of this....?!?!
1234 The function somehow worked with wrong input
Test Passed
It's strange but tests work differently when I click 'Run test' and 'Submit'. I had my tests failed with incorrect argument when I use
raise unless ...
but same tests had passed when I click 'Submit'!I got the same. The last one in Example test cases is invalid, assuming that Kata Test Cases are valid.
It should now work, let me know if not :)
I've handled 4 kind of errors and it worked!
"4) You haev to check that the string's length is even" odd length string are specified as valid inputs in the instructions!
This comment has been hidden.
NO FUN !! This kata is doesnt tell you exactly what cases need to be covered. I just dont think its well defined. It should give cases when it passes, when it fails ans what exceptions are suposed to be raised
Try develop your test first before submitting. And refer to test docs at Codewars for syntax.
This comment has been hidden.
Like others I have no idea how to get past the error cases. Please advise.
Description is very vague, but basically author is expecting You to raise an exception if:
a) any char in the string is not a number
b) passed string is empty
Exception is the vague part because the test output just says
Test Failed Exception:Exception Exception:Exception
I was able to get it to pass with a specific Excpetion such as
InputException
. Also seing the solutions now, there are a few different options here but it can't just be raising anException
. Hopefully this is more clear.I eventually got it to pass by simply doing
raise "random text" if
, followed by the error conditions.I think referring to Codewars Test Docs will help.
Is there any bug in the kata? It says NoMethodError for using Array#any?
Maybe I'll try again later.
Finally done it.
Did you change your code, or did the method mysteriously start working? I'm still getting NoMethodErrors for Array#any? and Array#all?
I'm not sure why the intrepeter (Ruby 1.9.3 used @Codewars) sometimes behaves like that (it happens a few times). It thinks that the object is not instantiated/created (an array object in your example), thus send a NoMethodError exception.
In my case, I tweaked my code to create an Array object using different method.
The instructions can/should be more explicit when defining how errors should be handled
It should handle errors for empty strings or strings which don't represent a decimal number
. So far returningfalse
or raising anException
don't appear to get through the error test cases.See my answer to @arieh.
I got the main right but with error hadling the below code is wrong... What am I missing..?
raise ArgumentError, 'String is empty' if str.empty?
raise ArgumentError, 'String is not numeric' if str.to_i == 0
break if (str.empty? || str.to_i == 0)
This seems wrong aswell... :/ I dont know how to handle exeptions
This comment has been hidden.
This comment has been hidden.
"543970707"
=> true
Test Failed: the output is wrong
Error on submit.
It should be false as the 7 in the middle is not on the right nor on the left (It should be ignored)
When a solution fails a test, it would be helpful to display the input that caused the failure.
Or if not display the exact input, at least give a hint as to what sort of input is causing the failure.
Test suite give wrong answer for invalid string
The example test cases you provide have incorrect results. Namely, the number you initially provide is not a valid lucky number, but the test treats it as one. And the second test has the same issue.
Fixed. Thank you