diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-06 10:02:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-06 10:02:11 +0100 |
| commit | ed13794086402d1ed1822ed6ec3316add9026cda (patch) | |
| tree | b89436813fc08ca0006dc70a904784b62d5b6ab0 | |
| parent | f07df1b77e503c1ada06392e13f6323d6986ed65 (diff) | |
| parent | 8bd7329fb0dec9a57bb33590877839ac69728b82 (diff) | |
| download | perlweeklychallenge-club-ed13794086402d1ed1822ed6ec3316add9026cda.tar.gz perlweeklychallenge-club-ed13794086402d1ed1822ed6ec3316add9026cda.tar.bz2 perlweeklychallenge-club-ed13794086402d1ed1822ed6ec3316add9026cda.zip | |
Merge pull request #10557 from pokgopun/pwc281
Pwc281
| -rw-r--r-- | challenge-281/pokgopun/go/ch-1.go | 60 | ||||
| -rw-r--r-- | challenge-281/pokgopun/go/ch-2.go | 109 | ||||
| -rw-r--r-- | challenge-281/pokgopun/python/ch-1.py | 52 | ||||
| -rw-r--r-- | challenge-281/pokgopun/python/ch-2.py | 83 |
4 files changed, 304 insertions, 0 deletions
diff --git a/challenge-281/pokgopun/go/ch-1.go b/challenge-281/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..7a10b39811 --- /dev/null +++ b/challenge-281/pokgopun/go/ch-1.go @@ -0,0 +1,60 @@ +//# 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 + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func checkColor(str string) bool { + return ((str[0] % 2) + (str[1] % 2)) == 1 +} + +func main() { + for _, data := range []struct { + input string + output bool + }{ + {"d3", true}, + {"g5", false}, + {"e6", true}, + } { + io.WriteString(os.Stdout, cmp.Diff(checkColor(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-281/pokgopun/go/ch-2.go b/challenge-281/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9f4f229646 --- /dev/null +++ b/challenge-281/pokgopun/go/ch-2.go @@ -0,0 +1,109 @@ +//# 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). + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type position struct { + x, y int +} + +func newPosition(label string) position { + return position{int(label[0]) - 96, int(label[1]) - 48} +} + +func (pt position) String() string { + return string([]byte{byte(pt.x) + 96, byte(pt.y) + 48}) +} + +type data struct { + count int + visited bool +} + +type board [8][8]data + +func (bd board) contains(p position) bool { + return p.x >= 1 && p.x <= 8 && p.y >= 1 && p.y <= 8 +} + +func knightMove(start, end string) int { + var bd board + p0 := newPosition(start) + p1 := newPosition(end) + queue := []position{p0} + bd[p0.x-1][p0.y-1] = data{visited: true} + dx := [8]int{-2, -1, 1, 2, -2, -1, 1, 2} + dy := [8]int{-1, -2, -2, -1, 1, 2, 2, 1} + for len(queue) > 0 { + p := queue[0] + queue = queue[1:] + if p.x == p1.x && p.y == p1.y { + return bd[p.x-1][p.y-1].count + } + for i := 0; i < 8; i++ { + pn := position{p.x + dx[i], p.y + dy[i]} + if bd.contains(pn) && !bd[pn.x-1][pn.y-1].visited { + bd[pn.x-1][pn.y-1] = data{visited: true, count: bd[p.x-1][p.y-1].count + 1} + queue = append(queue, pn) + } + } + } + return -1 +} + +func main() { + for _, data := range []struct { + start, end string + count int + }{ + {"g2", "a8", 4}, + {"g2", "h2", 3}, + } { + io.WriteString(os.Stdout, cmp.Diff(knightMove(data.start, data.end), data.count)) // blank if ok, otherwise show the difference + } +} 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() |
