+ 22

[ Code Coach: ] Password Validation Problem!!?

The assignment clearly states: The password is valid if it has at least 2 numbers, 2 of the following special characters ('!', '@', '#', '

#x27;, '%', '&', '*'), and a length of at least 7 characters. I passed all 8 tests with the following regex pattern you can see in the code: pattern = /[\w][\d]{1,}[\W]{2,}/ ✔ passes also for the pattern = /\w\d\W/ ✔ Of course, the example given in the assignment cannot pass Input: Hello@$World19 Output: Weak ❌ Can I get a reasonable explanation? Thanks! • https://code.sololearn.com/ciViHwF6n370/?ref=app

5th Jan 2020, 8:06 PM
Danijel Ivanović
Danijel Ivanović - avatar
31 Answers
+ 19
AZTECCO My friend, I absolutely agree this is a great idea and I really like it.😀 Yes, the task requires a more complicated regex, I did it in three other languages ​​but here I found this absurd! As you say, the meaning of learning is lost because the pattern that makes the least sense has solved the task!!? Nevertheless, this is a difficult level task, I understand it needs to be tricky and use various tricks to come up with a solution. It doesn't make sense for me to take xp, I'm trying to solve tasks in languages ​​I haven't worked with, like Ruby, it's a challenge for me already and I just like regex so I tried to do the task that way but...
6th Jan 2020, 6:56 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 18
Danijel Ivanović Just maded an sinpler regex which is possibly ok for all present test cases at assignment. As regex can differe as the input can be in any pattern so to recognize that pattern for Unkown input required some testing. Will post some fixed solution when get some time for testing but this can be done with regex in thus way Have an look over it. https://code.sololearn.com/cVRLWiMfiInK/?ref=app version 1.1 Michail Getmanskiy this version should work correctly with all input cases AZTECCO you can too have an look on version 1.1 and if you have some test cases then check and let me know https://code.sololearn.com/cpMwf4y0uP0P/?ref=app
6th Jan 2020, 9:16 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 17
AZTECCO Yes,😊 it's great that different languages ​​bring different approaches and therefore a different mindset.🍻 Also, I did Java code with regex and it works for most test cases. We have a similar task to this one in the Coding Challenges section so we can try to find the right pattern and for this one like GAWEN STEASY did and thank you!😊 My goal was to point out (if I'm wrong please tell me) that the task went through regardless of the wrong pattern being used!? Michail Getmanskiy, I did the task in a much easier way, you offered the similar and clearly solution.👍 I wanted to test myself and try to do the task using regex in a language unknown to me. Thank you all for your answers!🍻
6th Jan 2020, 2:07 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 16
Diego Okay, Thank you very much for your explanation.👌 I can understand how given regex pattern works,👍 but my question is: Do we need to guess what the right pattern is to pass the test? OR Do we need to solve the task in the right way? This pattern that passed the test does not meet the conditions in the assignment, while the corresponding one cannot pass the test? Did I misunderstand something?
5th Jan 2020, 10:04 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 15
Your regex pattern cares about order. It returns the index of the first ocurrence of a word character [\w] followed by one or more digit characters [\d]{1,} followed by two or more non-word characters [\W]{2,}, in that order. Let's see how your is_valid() function works. Input: @$42Hello42@$ Output: 8 (it matches "o42@
quot;, but ignores "@$42H") Input: @a$b4c2d Output: nil (wrong, it's a Strong password) [Sample input] Input: Hello@$World19 Output: nil (wrong, it's a Strong password) [Test #1] Input: PassWord19$# Output: 7 (it matches "d19$#") [Test #2] Input: LeTme!n Output: nil (correct, it's a Weak password) https://ruby-doc.org/core-2.6.5/Regexp.html
5th Jan 2020, 8:36 PM
Diego
Diego - avatar
+ 13
Danijel Ivanović Check this type of patterns. @h@ello1w4orld $h@ello1w4orld$ I'm improving more regex for that code I guess some more strong regex should be need for this I'm implementing by some other approach. I've tested and seems like for start and end position if any special symbols came it will give weak as output instead of strong. might something which was I missing here will look back over that again. here is one more version with some similar kind of regex is added and tested https://code.sololearn.com/cK3mCRHbpjXq/?ref=app
7th Jan 2020, 1:49 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 11
GAWEN STEASY, AZTECCO, Diego, Michail Getmanskiy, I changed my code, so when you have time you can test. I tried to combine the regex pattern but tested only a few cases.
7th Jan 2020, 1:43 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 11
Akintobi Idris A. You are welcome friend!👍 [ Edited: ] If you unabled to do the task, please feel free to contact again. I have done this task in several different languages, including python. In each of them I tried to do the task in a different way. Also, check out the other codes in the post as well, maybe you'll find an idea.😉 **Stay Safe & Happy SoloLearning ;)
26th Mar 2020, 3:42 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 9
To solve this type of problem, regular winding is an absolutely redundant tool ... in this example there are three rules, the regular expression code is already difficult to understand and cumbersome ... but there can be many more rules and they can change ... moreover the logic of regular expression itself is very resource-intensive ... it’s much simpler here, or a solution similar to it, with usually. It’s much simpler here, or a solution similar to it, with the usual one-time line scan. In general, my rule is: if the opportunity is not to use regular expressions, then they should not be used. passstr = input() nc = 0 sc = 0 ln = 0 dnm = "0123456789" dsc = "!@#$%&*" ln = len(passstr) for c in passstr : if dnm.find(c) != -1: nc += 1 elif dsc.find(c) != -1: sc += 1 if (nc>=2) and (sc>=2) and (ln >= 7): print("Strong") else: print("Weak")
6th Jan 2020, 4:40 AM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 9
Michail Getmanskiy thanks! 😊 GAWEN STEASY yes, I did not consider test case for the first or last special symbol in the password, in that case all passwords are weak! Thank you 👍 [ Edited: ] when special symbols are together password passes test, but when we separate them, the password does not pass the test: @$oloLearn20 -> Strong @Dan&Jel5615 -> Weak
7th Jan 2020, 3:45 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
Danijel Ivanović I think solving this challenge with just one regex require a more complicated regex.. I initially used two different regexes, and I think it's the more clear and readable way. Then I merged the two in { [C++] std::regex r("^(?=.*[0-9].*[0-9])(?=.*[!@#$%&*].*[!@#$%&*]).*
quot;); } Anyway the problem is that code coach ( which is a great idea to give xp for codes ) should be improved a lot.. - Rules are often not well defined. - Poor specifications. - only 5 test cases. - only one example. Especially the lack of test cases gives the opportunity ( voluntarily or accidentally) to hack the test, and the fact that the 3rd, 4th and 5th tests are hidden implies that it's accidentally.. similar to undefined behaviour lol. Personally, in many questions, I often had to try many solutions with little tweaks , losing the perception of what was going on. Losing the learning experience! This is a weak point of code coach. However it is still a great new feature in my opinion!
5th Jan 2020, 11:16 PM
AZTECCO
AZTECCO - avatar
+ 7
Akintobi Idris A. yes, it works great with the regex pattern you have in ruby ​​code, passes all test cases.👌
26th Mar 2020, 8:58 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
Danijel Ivanović same here ! I'm using this code coach as an opportunity to pick the various languages.. plus I tried a lot of regex .. It's awesome to see how different languages bring different approaches!
6th Jan 2020, 8:52 AM
AZTECCO
AZTECCO - avatar
+ 6
Michail Getmanskiy I agree with you , in this particular problem(where there's not a particular pattern) a simple iteration like yours would be the best and most intuitive approach. Regex are not really efficient and are difficult to read. Under the hood the purpose of this thread is regex practice and may be specified. [EDIT] although there's a [tag:regex] it may be specified more clearly..
6th Jan 2020, 10:10 AM
AZTECCO
AZTECCO - avatar
+ 6
Danijel Ivanović yes.. due to too few test cases there are many chances to make a wrong solution that works. I made this random test generator with two solver: a full regex /^(?=.*[0-9].*[0-9])(?=.*[!@#$%&*].*[!@#$%&*])(?=.{7}).*$/ and a loop solution. It also compares the results. Note that ,based on the rules , strings whit no alpha characters match( as not specified that a password must contain alpha characters ) I also added two more symbols characters. https://code.sololearn.com/ckErasro5Byj/?ref=app GAWEN STEASY and everyone's interested, feel free to test regexes on that code if you want.
6th Jan 2020, 5:02 PM
AZTECCO
AZTECCO - avatar
+ 6
Danijel Ivanović didn't saw your edited message. I think if numbers are in this format then too it's not give correct answer need to check for that too. @$olo1Learn2 @$@$olo1Learn2 it only recognize pattern in chunks for which you have written regular expression. PS it always good to work in regex but quite some time it becomes tricky and can put us in ambiguous path
7th Jan 2020, 8:01 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
Hoà Xuân Nông : "The pattern is: '^(?=.*\d{2,})(?=.*\w{2,}).{7,}
#x27;" - it’s not true ... since it requires two numbers in a row and two characters (not alphanumeric) in a row ... but it is not so ...... ...... for example , "Hello1$2World$" - it doesn’t work .... add shuffle combinations - \d*.*\W, \W*.*\d... etc example 3 of 6 combinations ((?=.*\d{1,}.*\W{1,}.*\d{1,}.*\W{1,}.*)|(?=.*\d{1,}.*\d{1,}.*\W{1,}.*\W{1,}.*)|(?=.*\W{1,}.*\W{1,}.*\d{1,}.*\d{1,}.*)).{7,} right for: Hello1$2$World Hello12$World Hello$12World
6th Jan 2020, 7:26 AM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 4
The pattern is: '^(?=.*\d{2,})(?=.*\W{2,}).{7,}
#x27; https://code.sololearn.com/c8ILkfiIM55O/?ref=app
6th Jan 2020, 7:16 AM
Hoà Xuân Nông
Hoà Xuân Nông - avatar
+ 4
Danijel Ivanović: Here is a regular expression, all tests pass ... as I wrote 6 combinations and is long. import re slnk = input() ok = re.match(r"((?=.*\d{1,}.*\W{1,}.*\d{1,}.*\W{1,}.*)|(?=.*\d{1,}.*\d{1,}.*\W{1,}.*\W{1,}.*)|(?=.*\W{1,}.*\W{1,}.*\d{1,}.*\d{1,}.*)|(?=.*\W{1,}.*\d{1,}.*\W{1,}.*\d{1,}.*)|(?=.*\d{1,}.*\W{1,}.*\W{1,}.*\d{1,}.*)|(?=.*\W{1,}.*\d{1,}.*\d{1,}.*\W{1,}.*)).{7,}",slnk) if ok : print("Strong") else: print("Weak") or using sort and regex + len (replacing char '@' with '
#x27; - since the sort order for char '@' after digits ...) import re instr = input() instr = "".join(sorted(instr.replace('@','
#x27;))) ok = re.match( r"(\W{2,}\d{2,})\w*",instr ) and len(instr) > 6 if ok : print("Strong") else: print("Weak")
7th Jan 2020, 2:55 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 3
Danijel Ivanović Michail Getmanskiy GAWEN STEASY I also finally found how to match N times instead of a doubled .*[...] sub expression. /(?=(.*[0-9]){2})(?=(.*[!@#$%&*]){2})(?=.{7})/ https://code.sololearn.com/cV5QYR8owhAv/?ref=app
7th Jan 2020, 11:00 PM
AZTECCO
AZTECCO - avatar