Hi there folks!
It’s a really simple at first, Bob is a robot that answers you with the following rule:
“Bob answers ‘Sure.’ if you ask him a question, such as “How are you?”.
He answers ‘Whoa, chill out!’ if you YELL AT HIM (in all capitals).
He answers ‘Calm down, I know what I’m doing!’ if you yell a question at him.
He says ‘Fine. Be that way!’ if you address him without actually saying
He answers ‘Whatever.’ to anything else.”
So I started working on my algorithm and finished my first successful one:
I was not content with the final version. It has a lot of conditionals just to treat the message, it has more exceptions than rules, just for fun I made benchmark with criterion, for the sake of simplicity I just moved the code to the bench file:
I prepared just five simple tests with different parameters and made some runs:
It runs from approximately 1 to 29 microseconds to run. It was not bad, but I was still bugged with the code itself so I decided to see how the most star solution went.
This guy had used the str methods in a very rust like way and this part made all sense to me:
Sometimes, when you are learning a language you make some verbose code because you do not know much about the tools the language has to offer. The char type has a method called is_alphabetic that can help me take away the Regex code I made, the ends_with encapsulate what I did before to find if it finishes with “?”, and probably makes my code faster and cleaner, so let’s try it.
The gain in performance is really impressive!
When we are learning a new programming language it’s important to look after how other people solved the same problem, although you are perfectly capable of coming up with a solution.
Researching over different solutions can be very rewarding and helps you improve your solution and learn new techniques.
So always try your own solution, make it works, be proud of it and then look for other solutions to learn more ways to solve those problems.
Thanks for your time!
And you? how would you solve this? Let me know.