diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-23 17:09:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-23 17:09:54 +0100 |
| commit | a7098451a3fffc4e31ac9e363fe9e17301d0f600 (patch) | |
| tree | c3889395cbf564fb189be96bff21d254bd930f49 /challenge-288/zapwai/python | |
| parent | 12a6acf31c5392b354e117870b6066d8d3e1205b (diff) | |
| parent | 2f5a634972c673db4d80141ce72c09b713b7200f (diff) | |
| download | perlweeklychallenge-club-a7098451a3fffc4e31ac9e363fe9e17301d0f600.tar.gz perlweeklychallenge-club-a7098451a3fffc4e31ac9e363fe9e17301d0f600.tar.bz2 perlweeklychallenge-club-a7098451a3fffc4e31ac9e363fe9e17301d0f600.zip | |
Merge pull request #10900 from zapwai/branch-for-288
Week 288
Diffstat (limited to 'challenge-288/zapwai/python')
| -rw-r--r-- | challenge-288/zapwai/python/ch-1.py | 29 | ||||
| -rw-r--r-- | challenge-288/zapwai/python/ch-2.py | 83 |
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-288/zapwai/python/ch-1.py b/challenge-288/zapwai/python/ch-1.py new file mode 100644 index 0000000000..48ebd7985c --- /dev/null +++ b/challenge-288/zapwai/python/ch-1.py @@ -0,0 +1,29 @@ +def is_pal(s) : + r = str(s)[::-1] + return (str(s) == r) + +def proc(s) : + print("Input:", s) + n = int(s) + found = False + step = 1 + while not found : + m = n - step + if is_pal(m) : + found = True + n = m + else : + m = n + step + if is_pal(m) : + found = True + n = m + step += 1 + print("Output:", n) + +s = "123" +proc(s) +s = "2" +proc(s) +s = "1400" +proc(s) + diff --git a/challenge-288/zapwai/python/ch-2.py b/challenge-288/zapwai/python/ch-2.py new file mode 100644 index 0000000000..d1976af50a --- /dev/null +++ b/challenge-288/zapwai/python/ch-2.py @@ -0,0 +1,83 @@ +import re + +def intersection(a, b): + for entry1 in a.split(" "): + for entry2 in b.split(" "): + if entry1 == entry2: + return True + return False + +def merge(c): + for i1 in range(len(c) - 1): + for i2 in range(i1 + 1, len(c)): + if intersection(c[i1], c[i2]): + h = {} + for i in c[i1].split(" "): + h[i] = 1 + for i in c[i2].split(" "): + h[i] = 1 + c[i1] = "" + c[i2] = " ".join(h.keys()) + +def contigu(matrix, m, n, entry, c): + blocks = [entry] + for block in blocks: + (a, b) = block.split("-") + y = int(a) + x = int(b) + if x + 1 <= n: + if matrix[y][x] == matrix[y][x+1]: + blocks.append(str(y)+"-"+str(x+1)) + if y + 1 <= m: + if matrix[y][x] == matrix[y+1][x]: + blocks.append(str(y+1)+"-"+str(x)) + c.append(" ".join(blocks)) + +def proc(matrix): + print("Input: matrix =") + for i in matrix: + print(i) + print() + m = -1 + len(matrix) + n = -1 + len(matrix[0]) + contig = [] + contigu(matrix, m, n, "0-0", contig) + for i in range(m+1): + for j in range(n+1): + got_flag = False + entry = str(i)+"-"+str(j) + for b in contig: + if entry in b.split(" "): + got_flag = True + break + if not got_flag: + contigu(matrix, m, n, entry, contig) + merge(contig) + cnt = 0 + for c in contig: + if c: + print(c) + leng = len(c.split(" ")) + if cnt < leng: + cnt = leng + print("\nOutput:", cnt, "\n") + +matrix = [['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'o', 'o'], + ] +proc(matrix) +matrix = [['x', 'x', 'x', 'x', 'x'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ] +proc(matrix) +matrix = [['x', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ['o', 'x', 'x', 'o', 'o'], + ['o', 'o', 'o', 'x', 'x'], + ] +proc(matrix) + |
