Tell us what’s happening:
I was doing IPv4 Validator code challenge. I wrote the following code.
Your code so far
import re
def is_valid_ipv4(ipv4):
# Define the core logic exactly ONCE
num_pattern = r"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])"
# Use an f-string to piece the full pattern together without duplication
pattern = f"^{num_pattern}\.{num_pattern}\.{num_pattern}\.{num_pattern}$"
return bool(re.match(pattern, ipv4))
Now, my code passes all the test cases given for this challenge. But there is a bug in my code. If the IP address is “192.168.1.249”, then my code returns False instead of True. So, you should modify the test cases given for this challenge. My bug fee code is as follows.
import re
def is_valid_ipv4(ipv4):
# Define the core logic exactly ONCE
num_pattern = r"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
# Use an f-string to piece the full pattern together without duplication
pattern = f"^{num_pattern}\.{num_pattern}\.{num_pattern}\.{num_pattern}$"
return bool(re.match(pattern, ipv4))
Lesson URL (https://www.freecodecamp.org/learn/daily-coding-challenge/09-05)