diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-18 19:59:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-18 19:59:32 +0100 |
| commit | 85073f31dae3decce780d1fd66cdcb79c82c7f65 (patch) | |
| tree | 318d3bc02836f4cde215f7f15e9ec875c74a1490 /challenge-061/paulo-custodio/python/ch-2.py | |
| parent | bfe139d559d498bcf1ae48cc08520bfc99ee9b85 (diff) | |
| parent | 51c035e04c5ae5f9f02555206f57eb675a737617 (diff) | |
| download | perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.gz perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.bz2 perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.zip | |
Merge pull request #10860 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-061/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-061/paulo-custodio/python/ch-2.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-061/paulo-custodio/python/ch-2.py b/challenge-061/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..0cbd7cd9c9 --- /dev/null +++ b/challenge-061/paulo-custodio/python/ch-2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# Challenge 061 +# +# TASK #2 > IPv4 Partition +# Reviewed by: Ryan Thompson +# You are given a string containing only digits (0..9). The string should have +# between 4 and 12 digits. +# +# Write a script to print every possible valid IPv4 address that can be made by +# partitioning the input string. +# +# For the purpose of this challenge, a valid IPv4 address consists of four +# "octets" i.e. A, B, C and D, separated by dots (.). +# +# Each octet must be between 0 and 255, and must not have any leading zeroes. +# (e.g., 0 is OK, but 01 is not.) +# +# Example +# Input: 25525511135, +# +# Output: +# +# 255.255.11.135 +# 255.255.111.35 + +import re +import sys + +def partition1(prefix, digits): + if re.search(r'^(\d+\.){4}$', prefix): + if digits == "": + prefix = prefix[:-1] + print(prefix) + else: + for l in range(1, 4): + if l <= len(digits): + part = digits[:l] + if int(part) <= 255: + partition1(prefix+part+".", digits[l:]) + +def partition(digits): + partition1("", digits) + +digits = sys.argv[1] +partition(digits) |
