diff options
Diffstat (limited to 'challenge-288/paulo-custodio/python')
| -rw-r--r-- | challenge-288/paulo-custodio/python/ch-1.py | 55 | ||||
| -rw-r--r-- | challenge-288/paulo-custodio/python/ch-2.py | 86 |
2 files changed, 141 insertions, 0 deletions
diff --git a/challenge-288/paulo-custodio/python/ch-1.py b/challenge-288/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..168d6ce085 --- /dev/null +++ b/challenge-288/paulo-custodio/python/ch-1.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +# Challenge 288 +# +# Task 1: Closest Palindrome +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str, which is an integer. +# +# Write a script to find out the closest palindrome, not including itself. +# If there are more than one then return the smallest. +# +# The closest is defined as the absolute difference minimized between two +# integers. +# +# Example 1 +# Input: $str = "123" +# Output: "121" +# Example 2 +# Input: $str = "2" +# Output: "1" +# +# There are two closest palindrome "1" and "3". Therefore we return the smallest "1". +# Example 3 +# Input: $str = "1400" +# Output: "1441" +# Example 4 +# Input: $str = "1001" +# Output: "999" + +import sys + +def is_palindrome(n): + return str(n) == str(n)[::-1] + +def next_palindrome(n): + out = None + i = 1 + while out is None or i <= n: + t = n-i + if t >= 0 and is_palindrome(t): + if out is None: + out = t + elif abs(out-n) > abs(t-n): + out = t + t = n+i + if is_palindrome(t): + if out is None: + out = t + elif abs(out-n) > abs(t-n): + out = t + i += 1 + return out + +n = int(sys.argv[1]) +print(next_palindrome(n)) diff --git a/challenge-288/paulo-custodio/python/ch-2.py b/challenge-288/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..43b4407ab8 --- /dev/null +++ b/challenge-288/paulo-custodio/python/ch-2.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +# Challenge 288 +# +# Task 2: Contiguous Block +# Submitted by: Peter Campbell Smith +# You are given a rectangular matrix where all the cells contain either x or o. +# +# Write a script to determine the size of the largest contiguous block. +# +# A contiguous block consists of elements containing the same symbol which +# share an edge (not just a corner) with other elements in the block, and where +# there is a path between any two of these elements that crosses only those +# shared edges. +# +# Example 1 +# Input: $matrix = [ +# ['x', 'x', 'x', 'x', 'o'], +# ['x', 'o', 'o', 'o', 'o'], +# ['x', 'o', 'o', 'o', 'o'], +# ['x', 'x', 'x', 'o', 'o'], +# ] +# Ouput: 11 +# +# There is a block of 9 contiguous cells containing 'x'. +# There is a block of 11 contiguous cells containing 'o'. +# Example 2 +# Input: $matrix = [ +# ['x', 'x', 'x', 'x', 'x'], +# ['x', 'o', 'o', 'o', 'o'], +# ['x', 'x', 'x', 'x', 'o'], +# ['x', 'o', 'o', 'o', 'o'], +# ] +# Ouput: 11 +# +# There is a block of 11 contiguous cells containing 'x'. +# There is a block of 9 contiguous cells containing 'o'. +# Example 3 +# Input: $matrix = [ +# ['x', 'x', 'x', 'o', 'o'], +# ['o', 'o', 'o', 'x', 'x'], +# ['o', 'x', 'x', 'o', 'o'], +# ['o', 'o', 'o', 'x', 'x'], +# ] +# Ouput: 7 +# +# There is a block of 7 contiguous cells containing 'o'. +# There are two other 2-cell blocks of 'o'. +# There are three 2-cell blocks of 'x' and one 3-cell. + +import sys + +SEEN = ' ' + +def parse_matrix(lines): + m = [[x for x in line] for line in lines] + return m + +def calc_max_block(m): + def size_block(m, ch, r, c): + m[r][c] = SEEN + count = 1 + if r-1 >= 0: + if m[r-1][c] == ch: + count += size_block(m, ch, r-1, c) + if r+1 < len(m): + if m[r+1][c] == ch: + count += size_block(m, ch, r+1, c) + if c-1 >= 0: + if m[r][c-1] == ch: + count += size_block(m, ch, r, c-1) + if c+1 < len(m[0]): + if m[r][c+1] == ch: + count += size_block(m, ch, r, c+1) + return count + + max_block = 0 + for r in range(len(m)): + for c in range(len(m[0])): + if m[r][c] != SEEN: + block = size_block(m, m[r][c], r, c) + max_block = max(max_block, block) + return max_block + +m = parse_matrix(sys.argv[1:]) +print(calc_max_block(m)) |
