Skip to content

Commit

Permalink
Merge pull request #46 from st0le/patch-1
Browse files Browse the repository at this point in the history
Refactor IPv4 and repeated space solution
  • Loading branch information
shabda committed Jan 14, 2019
2 parents 8425a8a + af49c8e commit a4ca59a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions string-manipulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ convert repeated spaces to one space

.. code-block:: python
import re; re.sub(r"[ ]+", ' ', 'this sentence has non-uniform spaces')
s = 'this sentence has non-uniform spaces'
print(' '.join(s.split()))
The above snippet clears out the repeated spaces in a text and replaces it with single space.
re is a regular expression module to find more than one occurrences of space with '[ ]+'.


Check if a string is a valid IP v4 address
Expand All @@ -224,7 +224,7 @@ Or if you want only traditionally formatted ip addresses.
def ipv4_check(ip):
try:
chunks = str(ip).split(".")
return all(int(chunk)<255 for chunk in chunks) and len(chunks) == 4
return all(0<=int(chunk)<=255 for chunk in chunks) and len(chunks) == 4
except ValueError:
return False
Expand Down

0 comments on commit a4ca59a

Please sign in to comment.