Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions solutions/python/bob/2/bob.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Your task is to determine what Bob will reply to someone when they say something to him or ask him a question.

Bob only ever answers one of five things:

- "Sure." This is his response if you ask him a question, such as "How are you?"
The convention used for questions is that it ends with a question mark.
- "Whoa, chill out!" This is his answer if you YELL AT HIM.
The convention used for yelling is ALL CAPITAL LETTERS.
- "Calm down, I know what I'm doing!" This is what he says if you yell a question at him.
- "Fine. Be that way!" This is how he responds to silence.
The convention used for silence is nothing, or various combinations of whitespace characters.
- "Whatever." This is what he answers to anything else.
"""


def response(hey_bob: str) -> str:
"""
Determine what Bob will reply to someone when they say something to him or ask him a question.

:param hey_bob: str
:return: str
"""
# Remove whitespaces
hey_bob = hey_bob.strip()

# Empty string -> responds to silence.
if not hey_bob:
return "Fine. Be that way!"

# Yell at Bob
if hey_bob == hey_bob.upper() and any(char.isalpha() for char in hey_bob):
# Yell a question
if is_question(hey_bob):
return "Calm down, I know what I'm doing!"
return "Whoa, chill out!"

# Ask question
if is_question(hey_bob):
return "Sure."

# Anything else.
return "Whatever."


def is_question(hey_bob: str) -> bool:
"""
Determine if you ask/yell a question.

:param hey_bob: str
:return: bool
"""
return hey_bob[-1] == '?'