diff options
| author | Steven <steven1170@zoho.eu> | 2024-08-06 20:44:08 +0100 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2024-08-06 20:44:08 +0100 |
| commit | de2d61d6472da35fb063738c72f966bf152386b0 (patch) | |
| tree | e2dbecfb330b2c28df91b7b3816ce1dcb5ab82c2 | |
| parent | 10621ff55d86161c732fef4c098784d7b35fcb4e (diff) | |
| download | perlweeklychallenge-club-de2d61d6472da35fb063738c72f966bf152386b0.tar.gz perlweeklychallenge-club-de2d61d6472da35fb063738c72f966bf152386b0.tar.bz2 perlweeklychallenge-club-de2d61d6472da35fb063738c72f966bf152386b0.zip | |
add solutions week 281 in python
| -rw-r--r-- | challenge-281/steven-wilson/python/ch-1.py | 21 | ||||
| -rw-r--r-- | challenge-281/steven-wilson/python/ch-2.py | 56 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-281/steven-wilson/python/ch-1.py b/challenge-281/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..a497fced01 --- /dev/null +++ b/challenge-281/steven-wilson/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + + +def check_colour(coordinates): + """ Given coordinates, a string that represents the coordinates of a square + of the chessboard return true if the square is light, and false if the + square is dark + >>> check_colour("d3") + True + >>> check_colour("g5") + False + >>> check_colour("e6") + True + """ + return not (ord(coordinates[0]) + int(coordinates[1])) % 2 == 0 + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-281/steven-wilson/python/ch-2.py b/challenge-281/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..2060cce74d --- /dev/null +++ b/challenge-281/steven-wilson/python/ch-2.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +from collections import deque + + +def knights_move(start, end): + """ A Knight in chess can move from its current position to any square two + rows or columns plus one column or row away. Take a starting position and + an ending position and calculate the least number of moves required. + + >>> knights_move('g2', 'a8') # g2 -> e3 -> d5 -> c7 -> a8 + 4 + >>> knights_move('g2', 'h2') # g2 -> e3 -> f1 -> h2 + 3 + """ + start_p = (ord(start[0]) - 96, int(start[1])) + end_p = (ord(end[0]) - 96, int(end[1])) + queue = deque([start_p]) + visited = {start_p} + number_moves = 0 + while queue: + for _ in range(len(queue)): + current = queue.popleft() + + if current == end_p: + return number_moves + + for position in possible_moves(current): + if position not in visited: + visited.add(position) + queue.append(position) + number_moves += 1 + return -1 + + +def possible_moves(position): + ''' + Return all possible knight moves within the board from a position + + >>> possible_moves((4, 4)) + [(6, 5), (5, 6), (3, 6), (2, 5), (2, 3), (3, 2), (5, 2), (6, 3)] + >>> possible_moves((1, 1)) + [(3, 2), (2, 3)] + ''' + moves = ([2, 1], [1, 2], [-1, 2], [-2, 1], [-2, -1], [-1, -2], [1, -2], + [2, -1]) + return [(position[0] + move[0], position[1] + move[1]) + for move in moves + if (position[0] + move[0]) in range(1, 9) + and (position[1] + move[1]) in range(1, 9)] + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
