Skip to content

Commit f3d0ef4

Browse files
committed
Add brain-prime game
1 parent bae2cff commit f3d0ef4

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ brain-gcd:
1616
brain-progression:
1717
uv run brain-progression
1818

19+
brain-prime:
20+
uv run brain-prime
21+
1922
build:
2023
uv build
2124

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@
2525
### Asciinema: step 8: brain-progression
2626

2727
[![asciicast](https://asciinema.org/a/C6YfeEUsekaaOdIIdAwfl0vZi.svg)](https://asciinema.org/a/C6YfeEUsekaaOdIIdAwfl0vZi)
28+
29+
### Asciinema: step 9: brain-prime
30+
31+
[![asciicast](https://asciinema.org/a/5IFJqgQEw9TpbY3lFXAPhE8Go.svg)](https://asciinema.org/a/5IFJqgQEw9TpbY3lFXAPhE8Go)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from random import randint
2+
3+
from brain_games.engine import engine
4+
5+
GAME_TASK = 'Answer "yes" if given number is prime. Otherwise answer "no".'
6+
MIN_NUMBER = 2
7+
MAX_NUMBER = 50
8+
9+
10+
def get_random_number():
11+
return randint(MIN_NUMBER, MAX_NUMBER)
12+
13+
14+
def is_prime(num):
15+
counter = 2
16+
while counter <= num / 2:
17+
if num % counter == 0:
18+
return False
19+
counter += 1
20+
return True
21+
22+
23+
def get_game_data():
24+
game_question = get_random_number()
25+
game_answer = 'yes' if is_prime(game_question) else 'no'
26+
return [str(game_question), game_answer]
27+
28+
29+
def main():
30+
engine(GAME_TASK, get_game_data)
31+
32+
33+
if __name__ == "__main__":
34+
main()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ brain-even = "brain_games.scripts.games.brain_even:main"
2626
brain-calc = "brain_games.scripts.games.brain_calc:main"
2727
brain-gcd = "brain_games.scripts.games.brain_gcd:main"
2828
brain-progression = "brain_games.scripts.games.brain_progression:main"
29+
brain-prime = "brain_games.scripts.games.brain_prime:main"

0 commit comments

Comments
 (0)