I am new to programming on nim, and I'm looking for feedback on some exercises
I've been doing on Exercism. If anyone could give some tips on how to improve
this solution I would appreciate it. I'll mark the task and the solution I have
as well as the test files if you guys would like to try it out
**Task:** 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.
**Solution**
import strutils
proc hey*(s: string): string =
var PossibleQuestion = strip(s) # Get rid of the whitespaces
# Check if the person said anything
if isEmptyOrWhitespace(PossibleQuestion):
return "Fine. Be that way!"
# Check if it has a question mark
elif contains(PossibleQuestion,"?"):
# Check if the question mark is at the last part
if endsWith(PossibleQuestion,"?"):
var qualifier:bool = true # By default the program thinks it's all
uppercase
var numCheck:bool = true # To check if it's all numbers
# Check if it's all uppercase letters or numbers
for i in PossibleQuestion[0..^1]:
if isLowerAscii(i):
qualifier = false
if i in Letters:
numCheck = false
# echo PossibleQuestion # for debugging
if qualifier and not numCheck: return "Calm down, I know what I'm
doing!"
elif numCheck: return "Sure."
else: return "Sure."
else: return "Whatever."
# At this point it's a statement
else:
# Create the statement string and remove punctuation and whitespace esc.
var statement:string
for i in PossibleQuestion:
if i in PunctuationChars or i in Whitespace:continue
else: statement.add(i)
# echo statement # for debugging
# Create a block to handle yelling
block YellBlock:
var qualifier:bool = true # By default the program thinks it's all
lowercase
var numCheck:bool = true # To check if it's all numbers
# Check if it's all capital letters, or all numbers
for i in statement:
if i in LowercaseLetters:
qualifier = false
if i in Letters:
numCheck = false
if qualifier and not numCheck: return "Whoa, chill out!"
else: return "Whatever."
discard
Run
**Test File**
import unittest
import bob
suite "Bob":
test "stating something":
check hey("Tom-ay-to, tom-aaaah-to.") == "Whatever."
test "shouting":
check hey("WATCH OUT!") == "Whoa, chill out!"
test "shouting gibberish":
check hey("FCECDFCAAB") == "Whoa, chill out!"
test "asking a question":
check hey("Does this cryogenic chamber make me look fat?") == "Sure."
test "asking a numeric question":
check hey("You are, what, like 15?") == "Sure."
test "asking gibberish":
check hey("fffbbcbeab?") == "Sure."
test "talking forcefully":
check hey("Hi there!") == "Whatever."
test "using acronyms in regular speech":
check hey("It's OK if you don't want to go work for NASA.") ==
"Whatever."
test "forceful question":
check hey("WHAT'S GOING ON?") == "Calm down, I know what I'm doing!"
test "shouting numbers":
check hey("1, 2, 3 GO!") == "Whoa, chill out!"
test "no letters":
check hey("1, 2, 3") == "Whatever."
test "question with no letters":
check hey("4?") == "Sure."
test "shouting with special characters":
check hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") == "Whoa,
chill out!"
test "shouting with no exclamation mark":
check hey("I HATE THE DENTIST") == "Whoa, chill out!"
test "statement containing question mark":
check hey("Ending with ? means a question.") == "Whatever."
test "non-letters with question":
check hey(":) ?") == "Sure."
test "prattling on":
check hey("Wait! Hang on. Are you going to be OK?") == "Sure."
test "silence":
check hey("") == "Fine. Be that way!"
test "prolonged silence":
check hey(" ") == "Fine. Be that way!"
test "alternate silence":
check hey("\t\t\t\t\t\t\t\t\t\t") == "Fine. Be that way!"
test "multiple line question":
check hey("\nDoes this cryogenic chamber make me look fat?\nNo.") ==
"Whatever."
test "starting with whitespace":
check hey(" hmmmmmmm...") == "Whatever."
test "ending with whitespace":
check hey("Okay if like my spacebar quite a bit? ") == "Sure."
test "other whitespace":
check hey("\n\r \t") == "Fine. Be that way!"
test "non-question ending with whitespace":
check hey("This is a statement ending with whitespace ") ==
"Whatever."
Run