Skip to content

Commit 2292ba5

Browse files
[Sync Iteration] python/bob/2
1 parent 243ff2e commit 2292ba5

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

solutions/python/bob/2/bob.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Your task is to determine what Bob will reply to someone when they say something to him or ask him a question.
3+
4+
Bob only ever answers one of five things:
5+
6+
- "Sure." This is his response if you ask him a question, such as "How are you?"
7+
The convention used for questions is that it ends with a question mark.
8+
- "Whoa, chill out!" This is his answer if you YELL AT HIM.
9+
The convention used for yelling is ALL CAPITAL LETTERS.
10+
- "Calm down, I know what I'm doing!" This is what he says if you yell a question at him.
11+
- "Fine. Be that way!" This is how he responds to silence.
12+
The convention used for silence is nothing, or various combinations of whitespace characters.
13+
- "Whatever." This is what he answers to anything else.
14+
"""
15+
16+
17+
def response(hey_bob: str) -> str:
18+
"""
19+
Determine what Bob will reply to someone when they say something to him or ask him a question.
20+
21+
:param hey_bob: str
22+
:return: str
23+
"""
24+
# Remove whitespaces
25+
hey_bob = hey_bob.strip()
26+
27+
# Empty string -> responds to silence.
28+
if not hey_bob:
29+
return "Fine. Be that way!"
30+
31+
# Yell at Bob
32+
if hey_bob == hey_bob.upper() and any(char.isalpha() for char in hey_bob):
33+
# Yell a question
34+
if is_question(hey_bob):
35+
return "Calm down, I know what I'm doing!"
36+
return "Whoa, chill out!"
37+
38+
# Ask question
39+
if is_question(hey_bob):
40+
return "Sure."
41+
42+
# Anything else.
43+
return "Whatever."
44+
45+
46+
def is_question(hey_bob: str) -> bool:
47+
"""
48+
Determine if you ask/yell a question.
49+
50+
:param hey_bob: str
51+
:return: bool
52+
"""
53+
return hey_bob[-1] == '?'

0 commit comments

Comments
 (0)