+ 14

Coding Challenge #3: Validate IPv4 address (My code included)

IPv4 Address (By: cheeze) Create a function validate() that accepts a string whether it is a valid IPv4 address. Examples of IPv4 address are below. The range is assumed to be from 0.0.0.0 to 255.255.255.255. e.g. validate("1.2.3.4") // true e.g. validate("7.08.9.10") // true e.g. validate("69.69.69.256") // false https://code.sololearn.com/cAEHET5zrDmv/?ref=app

13th May 2017, 5:37 PM
Pixie
Pixie - avatar
25 Antworten
+ 12
https://code.sololearn.com/ck5yzQ7KQRzR/?ref=app
13th May 2017, 5:45 PM
Burey
Burey - avatar
13th May 2017, 5:57 PM
Nikolay Nachev
Nikolay Nachev - avatar
13th May 2017, 5:59 PM
Tashi N
Tashi N - avatar
+ 15
@Tashi N, grrrrr RegEx, I am still postponing learning them. I admire anyone who already did...
13th May 2017, 6:03 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 13
@Burey this returns true : 69.69.69.-1 ;D
13th May 2017, 5:59 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 13
@Nikolay Had to learn that for bash scripting some time ago ^^ But I still don't like it... It's harder to read than to write.
13th May 2017, 6:09 PM
Tashi N
Tashi N - avatar
+ 12
@Burey. Nice! 😊 You should change output for 7.08.9.10 to "true" too 😅
13th May 2017, 5:47 PM
Pixie
Pixie - avatar
+ 10
oh...right 😅
13th May 2017, 5:54 PM
Burey
Burey - avatar
+ 10
ty @Nikolay 😅 forgot to check negative values D:
13th May 2017, 6:03 PM
Burey
Burey - avatar
+ 10
Mine: https://code.sololearn.com/cu3PNk80SKyr/#java Tashi's solution is much nicer.
13th May 2017, 6:12 PM
Tamás Barta
Tamás Barta - avatar
14th May 2017, 5:27 AM
Vaibhav Sharma
Vaibhav Sharma - avatar
+ 9
Two more test cases added: - Should not accept negative values - Should not accept anything other than number e.g 1.2.3.abc //Invalid or a24.1.2.3 //Invalid Here is my version: https://code.sololearn.com/W1jsGVsLUpZ8/?ref=app
13th May 2017, 6:14 PM
Ashwani Kumar
Ashwani Kumar - avatar
+ 9
Marking Burey's answer as best since he posted it first ☺
14th May 2017, 12:43 AM
Pixie
Pixie - avatar
+ 9
Tashi's code declares strings like 127.001.001.009 as valid, but do we want these? I considered using Regex as well, but mine became ugly af, when I excluded these. A tip for everyone who wants to learn Regex properly: Get yourself some lecture notes from a course on theoretical computer science and read about formal languages/grammars, DFAs and regular languages. Exercise: Build something like this: https://code.sololearn.com/cbCEngJc2SP4 If you need an explanation, I'm happy to help.
14th May 2017, 2:22 AM
Tob
Tob - avatar
+ 8
@burey I also forgot to check negative values.
13th May 2017, 6:07 PM
Vishnu ks
Vishnu ks - avatar
+ 7
Nice codes guys. I've been postponing regex as well 😅. And congrats on the platinum @Ashwani. ^_^
14th May 2017, 12:41 AM
Pixie
Pixie - avatar
+ 6
@Tobi: you are right, your example is an invalid address format and none of the posted solutions mark it as invalid. This checking needs a bit more difficult code.
14th May 2017, 5:34 AM
Tamás Barta
Tamás Barta - avatar
+ 5
Adding a validator showing OS accepts addresses that would fail this check. + link showing why + sample ping you can try https://code.sololearn.com/c4KPcxWDu9Lv/?ref=app
15th May 2017, 2:55 AM
Kirk Schafer
Kirk Schafer - avatar
+ 5
@Nadstratosfer Gonczy... I can't tell if you're berating (but no opinion here). fwiw, it's all strings here (I/O, anything not bytes representing magnitudes >0)...Javascript is all 64-bit floats...I used a leading zero myself. Octal and hex have "standard notation" and I am not enforcing formatting; I send to the OS straight up; all I do is ask the OS to connect. It works on Windows, but not because it's Microsoft's way -- I write code only on Android (the ping is for >3 OSs). RFC...Request For Comments...suggested standards. Guides...RFC's implementation differences are why we can fingerprint TCP/IP stacks across vendors. Mozilla's been great about following them but that's about it. It's like people can't follow instructions or something...which is why I'm reminding people: There are "the rules" and then there's the way things actually work. I think it's nice that you can ignore that, and that I don't need it to be popular, cool or impressive. When I bend rules, I'm leaving a reminder "to be structured, not rigid", so that--10 years from now--instead of someone tearing their hair out for a week because it never occurs to someone to try something "outside parameters", they remember some dummy wrote a pointless, non-compliant program--who remembers how THAT worked--...but it did, and that reminds them to bend; not break.
16th May 2017, 3:52 AM
Kirk Schafer
Kirk Schafer - avatar