aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-11 04:20:25 +0100
committerGitHub <noreply@github.com>2020-09-11 04:20:25 +0100
commit3cc9f74e15304e882221647cebf4109115cde8df (patch)
treeecc2b9a4f6ff6c02ca7ce9d1bcfffc776254ee5e
parent835a1fc585bfc51540fc7adb7d5d8a336133dcab (diff)
parentc53084b39a60d87ef29009092d54d151f528dfc0 (diff)
downloadperlweeklychallenge-club-3cc9f74e15304e882221647cebf4109115cde8df.tar.gz
perlweeklychallenge-club-3cc9f74e15304e882221647cebf4109115cde8df.tar.bz2
perlweeklychallenge-club-3cc9f74e15304e882221647cebf4109115cde8df.zip
Merge pull request #2248 from ash/master
77-2 in Python 3
-rw-r--r--challenge-077/ash/javascript/ch-2.html106
-rw-r--r--challenge-077/ash/python/ch-2.py42
2 files changed, 148 insertions, 0 deletions
diff --git a/challenge-077/ash/javascript/ch-2.html b/challenge-077/ash/javascript/ch-2.html
new file mode 100644
index 0000000000..3b1cd76bb6
--- /dev/null
+++ b/challenge-077/ash/javascript/ch-2.html
@@ -0,0 +1,106 @@
+<html>
+
+<!--
+ Task 2 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+ Comments: https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/
+-->
+
+<head>
+ <meta charset="UTF-8"/>
+ <title>Lonely X</title>
+ <style>
+ * {
+ text-align: center;
+ font-family: Arial, sans-serif;
+ }
+ #Shape table {
+ margin: auto;
+ /* border-collapse: collapse; */
+ }
+ /* #Shape table tr td input {
+ width: 50px;
+ height: 50px;
+ } */
+ #Shape table tr td {
+ border: 5px solid white;
+ }
+ #Shape table tr td.lonely {
+ border: 5px solid green;
+ }
+ </style>
+</head>
+<body>
+ <p>
+ <input type="text" name="width" id="MatrixWidth" size="3" value="3" />
+ ×
+ <input type="text" name="height" id="MatrixHeight" size="3" value="3" />
+ &nbsp;
+ <input type="submit" value="Set the shape" onclick="set_shape()" />
+ </p>
+
+ <div id="Shape"></div>
+
+ <p>Note: Lonely X is marked green.</p>
+
+ <script>
+ let w = 0;
+ let h = 0;
+
+ function set_shape() {
+ w = parseInt(document.getElementById('MatrixWidth').value);
+ h = parseInt(document.getElementById('MatrixHeight').value);
+
+ let table = '<table>';
+ for (let y = 0; y != h; y++) {
+ table += '<tr>';
+ for (let x = 0; x != w; x++) {
+ table += '<td><input type="checkbox" data-x="' + x + '" data-y="' + y + '" onclick="update_lonely()" /></td>';
+ }
+ table += '</tr>';
+ }
+ table += '</table>';
+ document.getElementById('Shape').innerHTML = table;
+ }
+
+ function update_lonely() {
+ let cells = document.getElementById('Shape').getElementsByTagName('input');
+
+ let neigbours = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [-1, -1], [1, -1], [-1, 1]];
+
+ for (let i = 0; i != cells.length; i++) {
+ cells[i].parentNode.className = '';
+
+ if (!cells[i].checked) continue;
+ let x = parseInt(cells[i].getAttribute('data-x'));
+ let y = parseInt(cells[i].getAttribute('data-y'));
+
+ let is_ok = true;
+ for (let c = 0; c != neigbours.length; c++) {
+ if (!test_cell(x + neigbours[c][0], y + neigbours[c][1])) {
+ is_ok = false;
+ break;
+ }
+ }
+
+ if (is_ok) {
+ cells[i].parentNode.className = 'lonely';
+ }
+ }
+ }
+
+ function test_cell(x, y) {
+ if (x < 0 || y < 0 || x >= w || y >= h) {
+ return true;
+ }
+ else {
+ let cell = document.querySelector('#Shape tr td input[data-x="'+ x + '"][data-y="' + y + '"]');
+ return !cell.checked;
+ }
+ }
+
+ set_shape();
+ </script>
+</body>
+</html>
diff --git a/challenge-077/ash/python/ch-2.py b/challenge-077/ash/python/ch-2.py
new file mode 100644
index 0000000000..d5b7b5e506
--- /dev/null
+++ b/challenge-077/ash/python/ch-2.py
@@ -0,0 +1,42 @@
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+# Comments: https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/
+
+# Output for the given example:
+#
+# $ python3 ch-2.py
+# Lonely X at position (0, 0).
+# Lonely X at position (2, 1).
+# Lonely X at position (0, 2).
+
+matrix = [
+ ['X', 'O', 'O'],
+ ['O', 'O', 'X'],
+ ['X', 'O', 'O'],
+]
+
+def is_O_cell(x, y):
+ if 0 <= x < len(matrix[0]) and 0 <= y < len(matrix):
+ return matrix[y][x] == 'O'
+ else:
+ return True
+
+x_cells = [
+ [col_i, row_i]
+ for row_i, row in enumerate(matrix)
+ for col_i, cell in enumerate(row)
+ if cell == 'X'
+]
+
+for x_cell in x_cells:
+ x, y = x_cell
+ if is_O_cell(x , y + 1) and \
+ is_O_cell(x , y - 1) and \
+ is_O_cell(x + 1, y ) and \
+ is_O_cell(x - 1, y ) and \
+ is_O_cell(x + 1, y + 1) and \
+ is_O_cell(x + 1, y - 1) and \
+ is_O_cell(x - 1, y + 1) and \
+ is_O_cell(x - 1, y - 1):
+ print(f"Lonely X at position ({x}, {y}).")