diff --git a/week01/01. Problems/README.md b/week01/01. Problems/README.md new file mode 100644 index 0000000..96637f2 --- /dev/null +++ b/week01/01. Problems/README.md @@ -0,0 +1 @@ +Python Problems ============== ## Divisible by a given number Find all numbers in the list `n` which are divisible by a given integer `d`. ### Signature ```python def divisible_by_d(n: list, d: int) -> list: pass ``` ### Test examples ```python >>> divisible_by_d([1,2,3,4,5,6], 3) [3, 6] >>> divisible_by_d([0,1,2,3,4,5,6], 4) [0, 4] >>> divisible_by_d([0], 4) [0] >>> divisible_by_d([1,3,5], 2) [] ``` ## Knight valid moves count Given a tuple with two indexes that denote the knight position on a matrix board, return the count of all valid moves which the knight can made. ### Signature ```python def knight_moves(pos: tuple) -> int: # pos[0] denotes the knights row # pos[1] denotes the knights column pass ``` ### Test examples ```python >>> knight_moves((2, 6)) 6 >>> knight_moves((0, 7)) 2 >>> knight_moves((5, 0)) 4 >>> knight_moves((4, 3)) 8 >>> knight_moves((6, 7)) 3 ``` Consider these graphical examples `Eight legal moves where pos = (4, 3)` |M|0|1|2|3|4|5|6|7| |-|-|-|-|-|-|-|-|-| |**0**| | | | | | | | | |**1**| | | | | | | | | |**2**| | | \*| | \*| | | | |**3**| | \*| | | | \*| | | |**4**| | | | N| | | | | |**5**| | \*| | | | \*| | | |**6**| | | \*| | \*| | | | |**7**| | | | | | | | | `Two legal moves where pos = (7, 7)` |M|0|1|2|3|4|5|6|7| |-|-|-|-|-|-|-|-|-| |**0**| | | | | | | | | |**1**| | | | | | | | | |**2**| | | | | | | | | |**3**| | | | | | | | | |**4**| | | | | | | | | |**5**| | | | | | | \*| | |**6**| | | | | | | | | |**7**| | | | | | \*| | N| \ No newline at end of file diff --git a/week01/01. Problems/solutions.py b/week01/01. Problems/solutions.py new file mode 100644 index 0000000..ab89840 --- /dev/null +++ b/week01/01. Problems/solutions.py @@ -0,0 +1,21 @@ +""" +Copyright (c) 2019 Hack Bulgaria + +The project is released under MIT License +https://github.com/HackBulgaria/Programming-101-Python-2020-Spring +""" + + +def divisible_by_d(n: list, d: int) -> list: + '''Returns a list of all numbers from n divislbe by d.''' + return [i for i in n if i % d == 0] + + +def knight_moves(pos: tuple) -> int: + '''Returns the count of all legal moves which the knight can make.''' + offsets = ((-2, -1), (-2, 1), (-1, -2), (-1, 2), + (1, -2), (1, 2), (2, -1), (2, 1)) + + return [ 0 <= (pos[0] + o[0]) < 8 and + 0 <= (pos[1] + o[1]) < 8 + for o in offsets].count(True) diff --git a/week01/README.md b/week01/README.md new file mode 100644 index 0000000..e69de29