Validate IP4 addressNumber-to-word converterTweets per secondTypeahead Talent BuddyHackerRank “Service Lane” in PythonAutocomplete Trie OptimizationCount the accumulated rainfall in a 2D mountain rangePython IBAN validationWAKE UP! CodingBat alarm clockHackerrank Gemstones SolutionCounting lower vs non-lowercase tokens for tokenized text with several conditions

voltage of sounds of mp3files

Bash method for viewing beginning and end of file

Understanding "audieritis" in Psalm 94

Is there a good way to store credentials outside of a password manager?

Applicability of Single Responsibility Principle

Is there any reason not to eat food that's been dropped on the surface of the moon?

Can I use my Chinese passport to enter China after I acquired another citizenship?

Hide Select Output from T-SQL

Was Spock the First Vulcan in Starfleet?

What to do with wrong results in talks?

Should my PhD thesis be submitted under my legal name?

Student evaluations of teaching assistants

What would happen if the UK refused to take part in EU Parliamentary elections?

Is it correct to write "is not focus on"?

Coordinate position not precise

Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?

apt-get update is failing in debian

Generic lambda vs generic function give different behaviour

What are the ramifications of creating a homebrew world without an Astral Plane?

What defines a dissertation?

Short story about space worker geeks who zone out by 'listening' to radiation from stars

Will it be accepted, if there is no ''Main Character" stereotype?

Implement the Thanos sorting algorithm

What is the intuitive meaning of having a linear relationship between the logs of two variables?



Validate IP4 address


Number-to-word converterTweets per secondTypeahead Talent BuddyHackerRank “Service Lane” in PythonAutocomplete Trie OptimizationCount the accumulated rainfall in a 2D mountain rangePython IBAN validationWAKE UP! CodingBat alarm clockHackerrank Gemstones SolutionCounting lower vs non-lowercase tokens for tokenized text with several conditions













0












$begingroup$


Validate IP Address



I got this problem during an interview. And would like to get some code review. I also wrote several tests with the expected output, and they all passed as expected.




Validate an IP address (IPv4). An address is valid if and only if it
is in the form "X.X.X.X", where each X is a number from 0 to 255.



For example, "12.34.5.6", "0.23.25.0", and "255.255.255.255" are valid
IP addresses, while "12.34.56.oops", "1.2.3.4.5", and
"123.235.153.425" are invalid IP addresses.




Examples:

"""

ip = '192.168.0.1'
output: true

ip = '0.0.0.0'
output: true

ip = '123.24.59.99'
output: true

ip = '192.168.123.456'
output: false
"""

def validateIP(ip):
#split them by '.' , and store them in an array
#check the array if the length is 4 length
arr = ip.split('.')
if len(arr) != 4:
return False
#0 check for special edge cases when non-digit
#1. check if they are digit,
#2. check if check the integer is between 0 and 255

for part in arr:
if len(part) > 1:
if part[0] == '0':
return False
if not part.isdigit():
return False
digit = int(part)
if digit < 0 or digit > 255:
return False
return True

#case#0

ip0="08.0.0.0" # False
test0= validateIP(ip0)
print(test0)

#case#1
ip1 = "192.168.0.1"
test1 = validateIP(ip1)
print(test1)

#case#2
ip2 = '0.0.0.0'
test2 = validateIP(ip2)
print(test2)

#case#3
ip3 = '123.24.59.99'
test3 = validateIP(ip3)
print(test3)

#case#4
ip4 = '192.168.123.456'
test4 = validateIP(ip4)
print(test4)

#case5
ip5 = "255.255.255.255"
test5 = validateIP(ip5)
print(test5)








share









$endgroup$
















    0












    $begingroup$


    Validate IP Address



    I got this problem during an interview. And would like to get some code review. I also wrote several tests with the expected output, and they all passed as expected.




    Validate an IP address (IPv4). An address is valid if and only if it
    is in the form "X.X.X.X", where each X is a number from 0 to 255.



    For example, "12.34.5.6", "0.23.25.0", and "255.255.255.255" are valid
    IP addresses, while "12.34.56.oops", "1.2.3.4.5", and
    "123.235.153.425" are invalid IP addresses.




    Examples:

    """

    ip = '192.168.0.1'
    output: true

    ip = '0.0.0.0'
    output: true

    ip = '123.24.59.99'
    output: true

    ip = '192.168.123.456'
    output: false
    """

    def validateIP(ip):
    #split them by '.' , and store them in an array
    #check the array if the length is 4 length
    arr = ip.split('.')
    if len(arr) != 4:
    return False
    #0 check for special edge cases when non-digit
    #1. check if they are digit,
    #2. check if check the integer is between 0 and 255

    for part in arr:
    if len(part) > 1:
    if part[0] == '0':
    return False
    if not part.isdigit():
    return False
    digit = int(part)
    if digit < 0 or digit > 255:
    return False
    return True

    #case#0

    ip0="08.0.0.0" # False
    test0= validateIP(ip0)
    print(test0)

    #case#1
    ip1 = "192.168.0.1"
    test1 = validateIP(ip1)
    print(test1)

    #case#2
    ip2 = '0.0.0.0'
    test2 = validateIP(ip2)
    print(test2)

    #case#3
    ip3 = '123.24.59.99'
    test3 = validateIP(ip3)
    print(test3)

    #case#4
    ip4 = '192.168.123.456'
    test4 = validateIP(ip4)
    print(test4)

    #case5
    ip5 = "255.255.255.255"
    test5 = validateIP(ip5)
    print(test5)








    share









    $endgroup$














      0












      0








      0





      $begingroup$


      Validate IP Address



      I got this problem during an interview. And would like to get some code review. I also wrote several tests with the expected output, and they all passed as expected.




      Validate an IP address (IPv4). An address is valid if and only if it
      is in the form "X.X.X.X", where each X is a number from 0 to 255.



      For example, "12.34.5.6", "0.23.25.0", and "255.255.255.255" are valid
      IP addresses, while "12.34.56.oops", "1.2.3.4.5", and
      "123.235.153.425" are invalid IP addresses.




      Examples:

      """

      ip = '192.168.0.1'
      output: true

      ip = '0.0.0.0'
      output: true

      ip = '123.24.59.99'
      output: true

      ip = '192.168.123.456'
      output: false
      """

      def validateIP(ip):
      #split them by '.' , and store them in an array
      #check the array if the length is 4 length
      arr = ip.split('.')
      if len(arr) != 4:
      return False
      #0 check for special edge cases when non-digit
      #1. check if they are digit,
      #2. check if check the integer is between 0 and 255

      for part in arr:
      if len(part) > 1:
      if part[0] == '0':
      return False
      if not part.isdigit():
      return False
      digit = int(part)
      if digit < 0 or digit > 255:
      return False
      return True

      #case#0

      ip0="08.0.0.0" # False
      test0= validateIP(ip0)
      print(test0)

      #case#1
      ip1 = "192.168.0.1"
      test1 = validateIP(ip1)
      print(test1)

      #case#2
      ip2 = '0.0.0.0'
      test2 = validateIP(ip2)
      print(test2)

      #case#3
      ip3 = '123.24.59.99'
      test3 = validateIP(ip3)
      print(test3)

      #case#4
      ip4 = '192.168.123.456'
      test4 = validateIP(ip4)
      print(test4)

      #case5
      ip5 = "255.255.255.255"
      test5 = validateIP(ip5)
      print(test5)








      share









      $endgroup$




      Validate IP Address



      I got this problem during an interview. And would like to get some code review. I also wrote several tests with the expected output, and they all passed as expected.




      Validate an IP address (IPv4). An address is valid if and only if it
      is in the form "X.X.X.X", where each X is a number from 0 to 255.



      For example, "12.34.5.6", "0.23.25.0", and "255.255.255.255" are valid
      IP addresses, while "12.34.56.oops", "1.2.3.4.5", and
      "123.235.153.425" are invalid IP addresses.




      Examples:

      """

      ip = '192.168.0.1'
      output: true

      ip = '0.0.0.0'
      output: true

      ip = '123.24.59.99'
      output: true

      ip = '192.168.123.456'
      output: false
      """

      def validateIP(ip):
      #split them by '.' , and store them in an array
      #check the array if the length is 4 length
      arr = ip.split('.')
      if len(arr) != 4:
      return False
      #0 check for special edge cases when non-digit
      #1. check if they are digit,
      #2. check if check the integer is between 0 and 255

      for part in arr:
      if len(part) > 1:
      if part[0] == '0':
      return False
      if not part.isdigit():
      return False
      digit = int(part)
      if digit < 0 or digit > 255:
      return False
      return True

      #case#0

      ip0="08.0.0.0" # False
      test0= validateIP(ip0)
      print(test0)

      #case#1
      ip1 = "192.168.0.1"
      test1 = validateIP(ip1)
      print(test1)

      #case#2
      ip2 = '0.0.0.0'
      test2 = validateIP(ip2)
      print(test2)

      #case#3
      ip3 = '123.24.59.99'
      test3 = validateIP(ip3)
      print(test3)

      #case#4
      ip4 = '192.168.123.456'
      test4 = validateIP(ip4)
      print(test4)

      #case5
      ip5 = "255.255.255.255"
      test5 = validateIP(ip5)
      print(test5)






      python interview-questions





      share












      share










      share



      share










      asked 6 mins ago









      NinjaGNinjaG

      855632




      855632




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216311%2fvalidate-ip4-address%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216311%2fvalidate-ip4-address%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

          Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

          चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि