Skip to content

Commit cd49cb3

Browse files
committed
Add brain-gcd game
1 parent 5d072f4 commit cd49cb3

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ brain-even:
1010
brain-calc:
1111
uv run brain-calc
1212

13+
brain-gcd:
14+
uv run brain-gcd
15+
1316
build:
1417
uv build
1518

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=pwr44_devops-engineer-from-scratch-project-49&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=pwr44_devops-engineer-from-scratch-project-49)
1111
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=pwr44_devops-engineer-from-scratch-project-49&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=pwr44_devops-engineer-from-scratch-project-49)
1212

13-
### Asciinema: step 5
13+
### Asciinema: step 5: btain-even
1414

1515
[![asciicast](https://asciinema.org/a/746759.svg)](https://asciinema.org/a/746759)
1616

17-
### Asciinema: step 6
17+
### Asciinema: step 6: brain-calc
1818

1919
[![asciicast](https://asciinema.org/a/xBVfA6WioDJ7SYInLt7sZVp6X.svg)](https://asciinema.org/a/xBVfA6WioDJ7SYInLt7sZVp6X)
20+
21+
### Asciinema: step 7: brain-gcd
22+
23+
[![asciicast](https://asciinema.org/a/LKymopkNQHeyv55y6M94obHR3.svg)](https://asciinema.org/a/LKymopkNQHeyv55y6M94obHR3)
24+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from random import randint
2+
3+
from brain_games.engine import engine
4+
5+
GAME_TASK = 'Find the greatest common divisor of given numbers.'
6+
MIN_NUMBER = 2
7+
MAX_NUMBER = 10
8+
9+
10+
def get_random_number():
11+
return randint(MIN_NUMBER, MAX_NUMBER)
12+
13+
14+
def get_second_number(first_number, random_member):
15+
second_number = get_random_number() * random_member
16+
while second_number == first_number:
17+
second_number = get_random_number() * random_member
18+
return second_number
19+
20+
21+
def get_gcd(num_1, num_2):
22+
max_num = num_1 if num_1 > num_2 else num_2
23+
gcd = 1
24+
counter = 2
25+
while counter <= max_num / 2:
26+
if num_1 % counter == 0 and num_2 % counter == 0:
27+
gcd = counter
28+
counter += 1
29+
return gcd
30+
31+
32+
def get_game_data():
33+
random_member = get_random_number()
34+
first_number = get_random_number() * random_member
35+
second_number = get_second_number(first_number, random_member)
36+
gcd = get_gcd(first_number, second_number)
37+
game_question = f'{first_number} {second_number}'
38+
game_answer = str(gcd)
39+
return [game_question, game_answer]
40+
41+
42+
def main():
43+
engine(GAME_TASK, get_game_data)
44+
45+
46+
if __name__ == "__main__":
47+
main()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ dev = [
2424
brain-games = "brain_games.scripts.games.brain_games:main"
2525
brain-even = "brain_games.scripts.games.brain_even:main"
2626
brain-calc = "brain_games.scripts.games.brain_calc:main"
27+
brain-gcd = "brain_games.scripts.games.brain_gcd:main"

0 commit comments

Comments
 (0)