Coding Challenge #3: Validate IPv4 address (My code included) | Sololearn: Learn to code for FREE!

+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

5/13/2017 5:37:09 PM

Pixie

26 Answers

New Answer

+12

https://code.sololearn.com/ck5yzQ7KQRzR/?ref=app

+17

here is mine: https://code.sololearn.com/WjtEH0CvAXW1/#html

+17

Mine: https://code.sololearn.com/cjmKLvoI6b43/?ref=app

+15

@Tashi N, grrrrr RegEx, I am still postponing learning them. I admire anyone who already did...

+13

@Burey this returns true : 69.69.69.-1 ;D

+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.

+12

@Burey. Nice! 😊 You should change output for 7.08.9.10 to "true" too πŸ˜…

+10

oh...right πŸ˜…

+10

ty @Nikolay πŸ˜… forgot to check negative values D:

+10

Mine: https://code.sololearn.com/cu3PNk80SKyr/#java Tashi's solution is much nicer.

+10

This challenge is, in theory, nice but there are so many corner cases to check for validity. See the following article for examples​: http://smallbusiness.chron.com/ip-address-invalid-68006.html

+10

My answer https://code.sololearn.com/c0vyKFVZhJZY/?ref=app

+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

+9

Marking Burey's answer as best since he posted it first ☺

+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.

+8

@burey I also forgot to check negative values.

+8

https://www.sololearn.com/discuss/389647/?ref=app

+7

Nice codes guys. I've been postponing regex as well πŸ˜…. And congrats on the platinum @Ashwani. ^_^

+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.

+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