+ 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
25 Respostas
+ 12
https://code.sololearn.com/ck5yzQ7KQRzR/?ref=app
+ 17
here is mine:
https://code.sololearn.com/WjtEH0CvAXW1/#html
+ 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
+ 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.
+ 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
+ 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.