aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-10-27 12:37:02 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-10-27 12:37:02 +0100
commit3055f452eeb5554380fed44cc228f72e0236bae2 (patch)
tree80d6d0c72684e00bab48b091e1e6310167238353
parent45b1ff9bba6da2eb6c475dd5713bc9ba96d46e88 (diff)
downloadperlweeklychallenge-club-3055f452eeb5554380fed44cc228f72e0236bae2.tar.gz
perlweeklychallenge-club-3055f452eeb5554380fed44cc228f72e0236bae2.tar.bz2
perlweeklychallenge-club-3055f452eeb5554380fed44cc228f72e0236bae2.zip
Add Python solution to challenge 126
-rw-r--r--challenge-126/paulo-custodio/python/ch-1.py30
-rw-r--r--challenge-126/paulo-custodio/python/ch-2.py87
2 files changed, 117 insertions, 0 deletions
diff --git a/challenge-126/paulo-custodio/python/ch-1.py b/challenge-126/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..278fd30932
--- /dev/null
+++ b/challenge-126/paulo-custodio/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# TASK #1 > Count Numbers
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N.
+#
+# Write a script to print count of numbers from 1 to $N that don't contain digit 1.
+#
+# Example
+# Input: $N = 15
+# Output: 8
+#
+# There are 8 numbers between 1 and 15 that don't contain digit 1.
+# 2, 3, 4, 5, 6, 7, 8, 9.
+#
+# Input: $N = 25
+# Output: 13
+#
+# There are 13 numbers between 1 and 25 that don't contain digit 1.
+# 2, 3, 4, 5, 6, 7, 8, 9, 20, 22, 23, 24, 25.
+
+import sys
+import re
+
+N = int(sys.argv[1])
+count = 0
+for n in range(1, N+1):
+ if not re.search(r"1", str(n)):
+ count += 1
+print(count)
diff --git a/challenge-126/paulo-custodio/python/ch-2.py b/challenge-126/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..091f18eebb
--- /dev/null
+++ b/challenge-126/paulo-custodio/python/ch-2.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+
+# TASK #2 > Minesweeper Game
+# Submitted by: Cheok-Yin Fung
+# You are given a rectangle with points marked with either x or *. Please
+# consider the x as a land mine.
+#
+# Write a script to print a rectangle with numbers and x as in the Minesweeper
+# game.
+#
+# A number in a square of the minesweeper game indicates the number of mines
+# within the neighbouring squares (usually 8), also implies that there are no
+# bombs on that square.
+#
+# Example
+# Input:
+# x * * * x * x x x x
+# * * * * * * * * * x
+# * * * * x * x * x *
+# * * * x x * * * * *
+# x * * * x * * * * x
+#
+# Output:
+# x 1 0 1 x 2 x x x x
+# 1 1 0 2 2 4 3 5 5 x
+# 0 0 1 3 x 3 x 2 x 2
+# 1 1 1 x x 4 1 2 2 2
+# x 1 1 3 x 2 0 0 1 x
+
+import fileinput
+
+def read_input():
+ lines = []
+ for line in fileinput.input():
+ lines.append(line)
+ return lines
+
+def parse_board(lines):
+ board = []
+ for line in lines:
+ board.append(line.split())
+ return board
+
+def compute_mines(board):
+ height = len(board)
+ width = len(board[0])
+
+ def has_mine(r, c):
+ if r < 0 or r >= height:
+ return False
+ elif c < 0 or c >= width:
+ return False
+ elif board[r][c] == 'x':
+ return True
+ else:
+ return False
+
+ for r in range(0, height):
+ for c in range(0, width):
+ if not has_mine(r, c):
+ count = 0
+ if has_mine(r-1, c-1):
+ count += 1
+ if has_mine(r-1, c):
+ count += 1
+ if has_mine(r-1, c+1):
+ count += 1
+ if has_mine(r, c-1):
+ count += 1
+ if has_mine(r, c+1):
+ count += 1
+ if has_mine(r+1, c-1):
+ count += 1
+ if has_mine(r+1, c):
+ count += 1
+ if has_mine(r+1, c+1):
+ count += 1
+ board[r][c] = str(count)
+
+def print_board(board):
+ for row in board:
+ print(" ".join(row))
+
+lines = read_input()
+board = parse_board(lines)
+compute_mines(board)
+print_board(board)