6 kyu
Valid Phone Number
7,097 of 21,204xDranik
Loading description...
Regular Expressions
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This comment has been hidden.
there's only one test case for js, please provide more.
While there is no random tests in JS, there is more than one test in the submission tests suite.
Example tests ideed have only one test, but it is not considered an issue, especially not for a yellow kata. Users can add their own tests there, for example they can type in inputs and outputs presented as examples in description.
This comment has been hidden.
Okay this does seem wrong, let me take a look...
Should be fixed.
thanks
Please, accept my BF translation: https://www.codewars.com/kumite/6814d606bae4b7b60da6328f?sel=6814d606bae4b7b60da6328f
Rejected by someone
This comment has been hidden.
Very interesting oneliner, funny kata.
This comment has been hidden.
Regex makes my head spin. Does it ever get better?
I have a solution and I confirmed it with several console.log of all the possible numbers and it gives me exactly what the exercise wants but it keeps throwing me that I did not approve
Tried your code and it passes all tests. Also this kata is in your solved kata's list.
The exercise lacks examples because in my case it had been solved, but since there were options that I did not give you as an example, it prevented the code from being considered correct, so be careful
This comment has been hidden.
Fixed by adding "$" at the end of my Regex
This comment has been hidden.
This comment has been hidden.
Very easy, not a 6kyu
Ranks won't be changed anytime soon. It could be a 7kyu kata tho.
I solved this kata, but I get an error only with the third telefon number ("(098) 123 4567"), i don't understand where is the mistake. I'm not US.
This comment has been hidden.
No testing for the number of digits in each group, in-fact, the top rated solution in python marks
' 0-0'
as valid.edit : found the problem but either way thanks
Can someone approve my translation? https://www.codewars.com/kumite/6109ad33bd3e4500070f3641?sel=6109ad33bd3e4500070f3641
You can promote your translation on discord or leave it as a suggestion so that more ppl can come across it.
This is hardly a 6 kyu. More like 7 or 8.
Regarding the sample tests and test attempts for the bash solutions: there is no testing if there are any non-numericals on spots in the string that are reversed for for numbers. In bash at least. My script only tests: length of phone number and if the special characters are placed correctly.
In the sample tests for C language**, the below needs fixing as it's actually an invalid input and should be returned as false. This can cause confusion as in the attempt test cases it should be false.
cr_assert_eq(valid_phone_number("(123)456-7890"), true);
someone fixed it apparently i also added more sample tests as tehre was only one
Python: No random tests. How did this pass approval?
Translation with random tests kumited.
Resolved.
I learned to use Regex in Haskell from this assignment. Thanks man! :)
hey, i don't know why but if my code is actually outputting false on the last example, but your "compiler" treats it as true. I'm on python 3.8, and if you need i can provide the code.
CW uses standard CPython interpreter, there's no "compiler treating
False
asTrue
". You may post your code in a comment but it's guaranteed to be wrong somehow.I tested my code in my vscode and works as intended, so i don't really know why it shouldn't work here.
See if this helps: https://docs.codewars.com/training/troubleshooting#works-but-no
i don't think my code was truly sufficient to prove a valid phone number but still worked. to me a valid number only contains numbers but thats just me. i know that here are numbers like 1800codewar or something dumb like that
This comment has been hidden.
This comment has been hidden.
Please use new python test framework.
done by B4B ~~
debugging is such a pain without knowing the input strings for the tests.. please can this be added?
Whenever I run into this problem, I just add a log statement in the function that will show me what the value is from the test. For JavaScript, this was:
log.debug(phoneNumber);
Attempt to pass again and you will see the test value along with the pass/fail response.
This comment has been hidden.
This comment has been hidden.
Your regular expression is wrong, you can test it here: https://regex101.com/ make sure you select ECMAScript in flavor at the left.
This comment has been hidden.
if you want to check for boolean then you'll have to use '$regexp.test($input)' and the syntax you're asking right now is probably 'match'
This comment has been hidden.
Problems with your solution are not a valid kata issue.
Here you can find how to check for input of failing test cases: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#how-can-i-see-which-input-causes-my-solution-to-fail
This comment has been hidden.
In Powershell the variables are case unsensitive: $alfa = $Alfa. Here they are case sensitive: $phonenumber != PhoneNumber
Python version should use snake_case.
I learning regex now, but this kata doesn't even required them, all u have to do, check if phone number equal to '(123) 456 789' should be 7 kyu imo
Im getting a lot of it should be false but returned false errors doing this in powershell. and also many that don't let me view the code that returned true that were supposed to be false...
This comment has been hidden.
@Chrono79 already raised the same issue below :point_down:
done by B4B ~~
This comment has been hidden.
Agreed. In any test where letters appear, there are other problems like too long and/or missing parens or dash. They need to isolate the problem of a non-digit appearing where there should be a digit.
done by B4B ~~
C translation kumited
please scrutinize for approval
This comment has been hidden.
PowerShell Translation Kumited! :)
'approved some time ago'
Kotlin translation
Could you please review and approve ?
Thanks in advance ! Regards !
This comment has been hidden.
I think you must output to the console a literal string "True" or "False"
No, the output should be a boolean, not a string, his code is wrong.
Needs random tests in js (probably other languages as well).
Fixed for JS.
Well, my solution works for the example test. Then, when attempting to submit my solution, there are 4 test failing but ... I can't see those tests or the expected vs. actual diff so I can't actully complete this. Dumb.
Use
console.log(phoneNumber)
to see what the input is.dude, thank you.
For js there is only 1 true case and no random tests, so there's a lot of space for cheating solutions. Additional tests would certainly help.
"Test failed. Expected false got true"
Where is the string? What can I do with only that info?
well print the input to the console so you can see it :P
I'm getting an error when I run my code, but the real problem is that the test does not show the string which it was used by the function. You should add more tests.
Just add first line : print((phoneNumber).
duplicate issue above ~~
I am not sure why but I keep failing this basic test which runs fine in my eclipse...everything else passes. assertEquals(true, Kata.validPhoneNumber("(123) 456-7890"));
I mean it is the most basic test so really do not get why my algorithm is failing when I run it through the online compiler. Works fine in my Eclipse. Oh well.
This comment has been hidden.
please, DO NOT post code without the spoiler flag. Thx (Note: I flagged it)
About your code, I'd bet that you're not failing the test you believe you're failing...: the test in the sample tests is not the only one used to test your code for submission. There are several others your code do not currently support.
I forfeited the kata, viewed the expanded test cases, ran them one by one... that's the one it failed...weird. only 1 failed...so pls provide these "several" I don't pass...something is not adding up...
This comment has been hidden.
There should be more tests. Since I am not from US, I don't know how phone numbers work.
print the inputs.
This comment has been hidden.
This comment has been hidden.
Not a kata issue.
It seems like Text.Regex.Posix is not available for Haskell compiler, so what lib could I use ?
It's available these days; not that you have to solve everything using regexps in any case.
As for Python, PEP8 says: 'Function names should be lowercase, with words separated by underscores as necessary to improve readability.'
done by B4B ~~
I keep getting FASLE with this test case: (123)456-7890 - which is part of the submissiont attempt. Expected: false, instead got: true
this case should be true.
If I try this case locally as Run Samples, it works and returns true but returns false for attemp with my solution. Can someone review?
thanks
This case is
false
.It's missing a space.
This comment has been hidden.
There is only one example testcase, but multiple testcases. The other testcases are hidden from the user so they can't manipulate the results. The Example Test case box is for the author to show you what the testcases look like (what inputs you get etc), and for the user to make their own tests.
To find out what the input is, simply print the input parameter to the console. The test you are failing on is:
print(phoneNumber) -> "(1111)555 2345"
This comment has been hidden.
Your code is falsely returning the right answer. "phoneNumber" object exists therefore it will default to True. Your regex isn't checking any string, you need to add the variable in. You have to use .match() to return a Boolean value and not just compiling it. Your regex is also slightly wrong (look at point 3 and 4).
if phoneNumber
toif new_variable_name.match(phoneNumber)
This comment has been hidden.
i agree @eyezick, i don't believe the tests for this kata really tested what the prompt was looking for. my solution was equally dumb.
This comment has been hidden.
done by B4B ~~
This comment has been hidden.
No it must follow the format given in the description
Only worry about the following format: (123) 456-7890 (don't forget the space after the close parentheses)
This comment has been hidden.
✘ Be careful with characters surrounding the phone number expected:<false> but was:<true>
Dont even know what this is saying. Would be nice if the site showed what the input was so I could correct for it.
This comment has been hidden.
Not a kata issue.
Description of the valid format is very unspecific & it looks like the last test case is wrong without further indications of what actually went wrong, thus preventing one to solve the problem and submit it.
Always fun to use regex!!!
This comment has been hidden.
I have no idea. But it doesn't look like a kata issue.
This comment has been hidden.
you reg exp would wail for instances like (123) 123-8943anything here
Not a kata issue.
Unfortunately, the tests can't be changed anymore. So we'll have to live with it.
Closing.
Javascript version of Kata should ideally include random tests, keep that in mind when authoring future Kata.
Java translation kumited.
C# translation kumited.
I tried all possible options and still have an issue with the last test. It will be useful if we can see what parameters are used.
Try using
puts
orconsole.log
to output the phone number for that last case. You can use that to re-create the test that is failing. I had the same problem and this was what got me past it.This comment has been hidden.
I'm having the same problem it fails the last test. I added your test cases and my code passes. I wish we could see all the test cases.
This comment has been hidden.
console.log
is your friend. Always print your input and intermediate results when stuck on an error. It helps a lot. It would be better if the Kata showed you the failed input along with the error but I can't change the tests for this kata as it has more than 500 submissions.As for your solution, it is wrong because you are missing a special case. Think what your function will return for
"abc(123) 456-7890"
and How can you avoid return true for this?Also, when you fail a test, no further tests are run. So, it's always the "last" test. There are two more tests after this one.
Looks like u solved it ~~
For some reason on the final test, I don't understand what I've done wrong here. Everything seems to be correct, except for very last test. Does anyone know what I've done wrong here?
Nevermind. Got it!
Do you remember what fixed it for you? I'm getting stuck on the last case too
This felt way too easy for a 6 kyu.
Personally I feel like this isn't a 6 kyu, probably more a 7 or 8.
The description of the valid format is very unspecific and it looks like the last test case is wrong.
Issue is too unspecific. You could at least mention in which language the last test case is wrong. Closing.
I see that almost all the solutions are Regex based. I have heard that regex should be avoided unless there is a very strong reason to use it. Here, the regex is simple and quite easy to get right but I'm wondering why we WOULD or WOULD_NOT choose a regex in the wild
Fairly new to regex myself. I wondered the same after reading your comment and found this SE thread very informative. http://programmers.stackexchange.com/questions/113237/when-you-should-not-use-regular-expressions
This comment has been hidden.
The input may have 0's in unexpected places - try making the ranges 0-9 and see if that is the issue...
Yea the last test isnt working for me too?? what exactly is it entering?
This comment has been hidden.
This comment has been hidden.
Not a kata issue.
When making a kata for Ruby you should follow the Ruby naming conventions, function and variable names are lower case with underscores, not camel case.
Done in this fork
What regexp module is available for Haskell?
Text.Regex
doesn't work for me.As far as I know: none.
That's very disappointing. Thank you anyway! :-)
I think this kata should be disqualified, or have been moved to inactive state.
The last test is not correct according to the task description.
yo
have you solved this?
any hint as to why the last test case is wrong?
This comment has been hidden.
Hint: does your regular expression match the whole string or just a substring?
Thank you very much for your reply.
Not to be overly pedantic, but (123) is not a valid area code, 0 and 1 are reserved first digits, both in 10 and 7 digit phone numbers.
Does anybody know what unit test framework this is using?
http://www.codewars.com/docs/kata-test-framework
This comment has been hidden.
/^\(?\d{3}\)?[ ]\d{3}[-]?\d{4}$/.test("(123)456-7890");
returns false in the dev console. It was returning true for you?No. I am just trying to understand why
?[ ]
is considered invalid but?
works. I was under the impression[ ]
would be matching a space. Just trying to wrap my head around RegEx a bit... I understand that it could still be a cleaner solution after looking at some of the other solutions.This comment has been hidden.
I did the same thing and didn't even realise it till you brought it up ><. Thanks for pointing it out! And yes, that test case needs to be added.
I can't make any changes to the Kata :/ Sorry
It may just be this example, or it may be codewars, but my code didn't pass the test validation (yet it passes on irb no problem). Seems that something is up with the compiler.
Nearly 700 solutions has passed, so it is most likely your solution not doing exactly what is required.
You can post (as a comment) your current solution here and mark it as "Spoiler". Warriors already having solved the kata can then assist in pointing out what may be wrong (your code or the test code).
It's not very idiomatic Ruby to force a regexp match to return a boolean (true/false).
In Ruby, the Regexp class's #=~ and #match methods return nil (false-y in Ruby) if no match was found, or a Fixnum (truth-y in Ruby) which is the first match's index position in the string if a match was detected.
Therefore, in my opinion, the test case should have read:
Test.expect(validPhoneNumber("(123) 456-7890"), 'expected "(123) 456-7890" to be valid' )
This comment has been hidden.
Thanks a lot man. That helped :D
That helped me as well! Seems unfair to use tests that you're not going to show!
What seems unfair? : )
@blz777: @Sp33k3rph433k has completed his last kata ~6 months ago and probably isn't active on CW anymore. That being said, he was probably surprised that hidden and example test cases differ. ¯\(ツ)/¯
I had some trouble understanding why the last test case failed. It didn't help that it didn't show the phone number that was used in the test. We should add it as a message:
Test.assertEquals(validPhoneNumber("asdfad(123) 456-78904adsfs"), false, "asdfad(123) 456-78904adsfs");
Thanks! I was puzzled by exactly the same thing!
Thanks, the test showing just "test failed" is not really helpful.
Juhamatti, thanks so much for mentioning this. I was frustrated that I couldn't see the last test as well and only came to the conclusion that invalid characters may be at the beginning of the test, but not at the end. Cheers!
Another thank you from here as well. Test case should be showing in this one.
One more test case:
Test.assertEquals(validPhoneNumber("(a23) 456-7890"), false);
This comment has been hidden.
Added a few more test cases to cover that :)
This comment has been hidden.
Thanks for the feedback. Updated the description.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
The above code is still incorrectly accepted. The following test case should reject the above answer: