Introduction
Bob is a lackadaisical teenager.
He likes to think that he’s very cool.
And he definitely doesn’t get excited about things.
That wouldn’t be cool.
When people talk to him, his responses are pretty limited.
Instructions
Instructions
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.
Dig Deeper
If statements
if statements
// Package bob is a library for replying to Bob.
package bob
import (
"strings"
"unicode"
)
func isShout(phrase string) bool {
if strings.IndexFunc(phrase, unicode.IsLetter) == -1 {
return false
}
return strings.ToUpper(phrase) == phrase
}
func isQuestion(phrase string) bool {
return strings.HasSuffix(phrase, "?")
}
func isEmpty(s string) bool {
return s == ""
}
// Bob replies to what is remarked to Bob.
func Hey(remark string) string {
remark = strings.TrimSpace(remark)
if isEmpty(remark) {
return "Fine. Be that way!"
}
question := isQuestion(remark)
shout := isShout(remark)
if shout {
if question {
return "Calm down, I know what I'm doing!"
}
return "Whoa, chill out!"
}
if question {
return "Sure."
}
return "Whatever."
}
In this approach you have a series of if statements using the private functions to evaluate the conditions.
A private (also called unexported) function is indicated by starting with a lowercase letter.
More info on exported and unexported functions can be found [here](https://yourbasic.org/golang/public-private/).
As soon as the right condition is found, the correct response is returned.
Note that there are no `else if` or `else` statements.
If an `if` statement can return, then an `else if` or `else` is not needed.
Execution will either return or will continue to the next statement anyway.
In the isShout() function, the strings function IndexFunc() is used to ensure there is at least one letter character in the input.
If not, the function returns false, because if the input were only "123" it would equal itself uppercased, but without letters it would not be a shout.
If the input does contain a letter, the function returns whether the uppercased input is the same as the input.
The uppercasing is done by using the ToUpper() function.
In the isQuestion() function, the strings function HasSuffix() is used to determine if the input ends with a question mark.
In the Hey() function, the TrimSpace() function is used to eliminate any whitespace at the ends of the input.
If the string has no characters left, it returns the response for saying nothing.
If the string is not empty, the shout and question variables are set.
If shout is true, then the function returns the response for if the input is a shouted question os just a shout.
If the input is a question, then the function returns the response for that.
Finally, if the function has not returned by the end, the response for neither a shout nor a question is returned.
Switch statement
switch statement
// Package bob is a library for replying to Bob.
package bob
import (
"strings"
"unicode"
)
func isShout(phrase string) bool {
if strings.IndexFunc(phrase, unicode.IsLetter) == -1 {
return false
}
return strings.ToUpper(phrase) == phrase
}
func isQuestion(phrase string) bool {
return strings.HasSuffix(phrase, "?")
}
func isEmpty(s string) bool {
return s == ""
}
// Bob replies to what is remarked to Bob.
func Hey(remark string) string {
remark = strings.TrimSpace(remark)
if isEmpty(remark) {
return "Fine. Be that way!"
}
question := isQuestion(remark)
shout := isShout(remark)
switch true {
case question && shout:
return "Calm down, I know what I'm doing!"
case shout:
return "Whoa, chill out!"
case question:
return "Sure."
default:
return "Whatever."
}
}
In this approach you use a switch statement to test if there is a question or a shout.
The switch, with the help of some private functions to evaluate the conditions,
returns the right response for a question, shout, shouted question, or for not being a shout or a question.
A private (also called unexported) function is indicated by starting with a lowercase letter.
More info on exported and unexported functions can be found [here](https://yourbasic.org/golang/public-private/).
In the isShout() function, the strings function IndexFunc() is used to ensure there is at least one letter character in the input.
If not, the function returns false, because if the input were only "123" it would equal itself uppercased, but without letters it would not be a shout.
If the input does contain a letter, the function returns whether the uppercased input is the same as the input.
The uppercasing is done by using the ToUpper() function.
In the isQuestion() function, the strings function HasSuffix() is used to determine if the input ends with a question mark.
In the Hey() function, the TrimSpace() function is used to eliminate any whitespace at the ends of the input.
If the string has no characters left, it returns the response for saying nothing.
If the string is not empty, the shout and question variables are set.
The question and shout values are tested in a switch.
The logical AND operator (&&) is used to check if both question and shout are true.
If a switch arm evaluates to true, the appropriate response is returned.
If neither question nor shout is true, the default arm of the switch returns the response when the input is neither a question nor a shout.
Answer array
Answer array
// Package bob is a library for replying to Bob.
package bob
import (
"strings"
"unicode"
)
func isShout(phrase string) int {
if strings.IndexFunc(phrase, unicode.IsLetter) == -1 {
return 0
}
if strings.ToUpper(phrase) == phrase {
return 2
}
return 0
}
func isQuestion(phrase string) int {
if strings.HasSuffix(phrase, "?") {
return 1
}
return 0
}
func isEmpty(s string) bool {
return s == ""
}
var answers = [...]string{"Whatever.", "Sure.", "Whoa, chill out!", "Calm down, I know what I'm doing!"}
// Bob replies to what is remarked to Bob.
func Hey(remark string) string {
remark = strings.TrimSpace(remark)
if isEmpty(remark) {
return "Fine. Be that way!"
}
return answers[isQuestion(remark)+isShout(remark)]
}
In this approach you define an array that contains Bob’s answers, and each condition is given a score.
The correct answer is selected from the array by using the score as the array index.
In the isShout() function, the strings function IndexFunc() is used to ensure there is at least one letter character in the input.
If not, the function returns 0, because if the input were only "123" it would equal itself uppercased, but without letters it would not be a shout.
If the input does contain a letter, the function returns 2 if the uppercased input is the same as the input, otherwise it returns 0.
The uppercasing is done by using the ToUpper() function.
In the isQuestion() function, the strings function HasSuffix() is used to return 1 if the input ends with a question mark, otherwise it returns 0.
An array literal is used to define the responses.
In the Hey() function, the TrimSpace() function is used to eliminate any whitespace at the ends of the input.
If the string has no characters left, it returns the response for saying nothing.
The conditions of being a question and being a shout are assigned scores by adding the calls to isShout and isQuestion.
For example, giving a question a score of 1 and a shout a score of 0 would use an index of 1 to get the element from the answers array, which is "Sure.".
| isShout | isQuestion | Index | Answer |
|---|
| 0 | 0 | 0 + 0 = 0 | "Whatever." |
| 0 | 1 | 0 + 1 = 1 | "Sure." |
| 2 | 0 | 2 + 0 = 2 | "Whoa, chill out!" |
| 2 | 1 | 2 + 1 = 3 | "Calm down, I know what I'm doing!" |
Source: Exercism go/bob