aboutsummaryrefslogtreecommitdiff
path: root/challenge-281
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-08-06 11:21:46 +1000
committerMichael Manring <michael@manring>2024-08-06 11:21:46 +1000
commit070329afb38475de9ad40bcadd50ea1672126940 (patch)
tree84b39297106940182995ea9a2e8066134a9e51b8 /challenge-281
parentd4691a9d293de4cf2f42e44cdefabb0b455045fb (diff)
downloadperlweeklychallenge-club-070329afb38475de9ad40bcadd50ea1672126940.tar.gz
perlweeklychallenge-club-070329afb38475de9ad40bcadd50ea1672126940.tar.bz2
perlweeklychallenge-club-070329afb38475de9ad40bcadd50ea1672126940.zip
pwc281 solution in python
Diffstat (limited to 'challenge-281')
-rw-r--r--challenge-281/pokgopun/python/ch-1.py52
-rw-r--r--challenge-281/pokgopun/python/ch-2.py83
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-281/pokgopun/python/ch-1.py b/challenge-281/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..6b74504f58
--- /dev/null
+++ b/challenge-281/pokgopun/python/ch-1.py
@@ -0,0 +1,52 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-281/
+"""
+
+Task 1: Check Color
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given coordinates, a string that represents the coordinates of
+ a square of the chessboard as shown below:
+
+ Week_281_Task_1
+
+ Write a script to return true if the square is light, and false if the
+ square is dark.
+
+Example 1
+
+Input: $coordinates = "d3"
+Output: true
+
+Example 2
+
+Input: $coordinates = "g5"
+Output: false
+
+Example 3
+
+Input: $coordinates = "e6"
+Output: true
+
+Task 2: Knight’s Move
+"""
+### solution by pokgopun@gmail.com
+
+def checkColor(string: str):
+ return sum(
+ ord(c) for c in string
+ ) % 2 == 1
+
+import unittest
+
+class TestCheckColor(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "d3": True,
+ "g5": False,
+ "e6": True,
+ }.items():
+ self.assertEqual(checkColor(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-281/pokgopun/python/ch-2.py b/challenge-281/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..7e6b5f5807
--- /dev/null
+++ b/challenge-281/pokgopun/python/ch-2.py
@@ -0,0 +1,83 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-281/
+"""
+
+Task 2: Knight’s Move
+
+Submitted by: [48]Peter Campbell Smith
+ __________________________________________________________________
+
+ A Knight in chess can move from its current position to any square two
+ rows or columns plus one column or row away. So in the diagram below,
+ if it starts a S, it can move to any of the squares marked E.
+
+ Write a script which takes a starting position and an ending position
+ and calculates the least number of moves required.
+
+ Week_281_Task_2
+
+Example 1
+
+Input: $start = 'g2', $end = 'a8'
+Ouput: 4
+
+g2 -> e3 -> d5 -> c7 -> a8
+
+Example 2
+
+Input: $start = 'g2', $end = 'h2'
+Ouput: 3
+
+g2 -> e3 -> f1 -> h2
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 11th August
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+### https://afteracademy.com/blog/knight-on-chessboard/#:~:text=Solution,the%20shape%20of%20an%20L).
+
+def pos2xy(string: str):
+ return ord(string[0])-97, ord(string[1])-49
+
+def xy2pos(x, y):
+ return chr(97+x)+chr(49+y)
+
+def isOnBoard(x, y):
+ return x >=0 and x < 8 and y >= 0 and y < 8
+
+def knightMove(start, end):
+ XYd = ( (-2,-1), (-1,-2), ( 1,-2), ( 2,-1), (-2, 1), (-1, 2), ( 1, 2), ( 2, 1) )
+ XYa = tuple([None for y in range(8)] for x in range(8))
+ x0, y0 = pos2xy(start)
+ x1, y1 = pos2xy(end)
+ #XYa[x0][y0] = 0
+ XYa[x0][y0] = ""
+ XYq = [(x0, y0)]
+ while len(XYq) > 0:
+ x, y = XYq.pop(0)
+ if (x,y) == (x1,y1):
+ #return XYa[x][y]
+ print(XYa[x][y]+end,"=>",int(len(XYa[x][y])/2))
+ return int(len(XYa[x][y])/2)
+ for dx, dy in XYd:
+ xn, yn = x + dx, y + dy
+ if isOnBoard(xn, yn) and XYa[xn][yn] is None:
+ #XYa[xn][yn] = XYa[x][y] + 1
+ XYa[xn][yn] = XYa[x][y] + xy2pos(x,y)
+ XYq.append((xn,yn))
+ return -1
+
+import unittest
+
+class TestKnightMove(unittest.TestCase):
+ def test(self):
+ for (start,end), count in {
+ ('g2', 'a8'): 4,
+ ('g2', 'h2'): 3,
+ }.items():
+ self.assertEqual(knightMove(start, end), count)
+
+unittest.main()