Skip to content

Modular arithmetic and chess problems #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions week01/01. Problems/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Python Problems==============## Divisible by a given numberFind all numbers in the list `n` which are divisible by a given integer `d`.### Signature```pythondef 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 countGiven 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```pythondef 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|
Expand Down
21 changes: 21 additions & 0 deletions week01/01. Problems/solutions.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file added week01/README.md
Empty file.