Generate Hashtags

Generate Hashtags

Today’s problem was especially difficult. Not because the problem was hard, the problem is a no-brainer. No, it was difficult because I decided to only work in Emacs for these challenges.

I do not know the first thing about emacs.

To make things even more interesting I also decided I would not be using the mouse while I’m in emacs. It was a very interesting experience.

Generate hashtags problem

You are given a string with words and spaces. Turn it into a Hashtag.

Example

“Hello world” => #HelloWord

Caveats

  • Each word should be capitalized
  • Empty string return false
  • Hashtags over 140 characters are invalid and return false

Solution

So I guess the question is why on earth would I try out emacs? I mean I use Webstorm for work so what’s wrong with continuing with that? Am I trying to be a hipster? Am I being contrary just to be contrary? No, I actually have reasons, they aren’t good reasons but reasons non the less;

  • I’m starting to learn Rust, I can’t use Webstorm for it so I need an editor
  • I wanted to see what the fuss was about

People have been waging the Editor War since before I started programming. I’ve mostly kept to the sidelines and focused on my code but I think it’s about time I see for myself just how much more productive I can be. So for the next couple of months, I’m going to slowly start doing more and more stuff in emacs.

Another question would be why did I not start with vim? That’s easy, the coin landed heads.

On yeah, this is supposed to be a code challenge thing right?

Actual Solution

Let’s not overthink it. Generating hashtags can be brute-forced. So let’s do that.

function hashtag (str) { if (str.trim() === ''){ return false } const hash = str.split(/\s+/).reduce((output, word) => (output + word[0].toUpperCase() + word.slice(1)), '#') if(hash.length > 140){ return false } return hash }
Code language: JavaScript (javascript)

Trim white space, reduce while capitalizing, and Bob’s your uncle.

So what editor are you using? Is this Editor War still a thing? Something something code challenge, something something, hit me up on Twitter, @phoexer, happy coding.


Leave a Reply

Your email address will not be published.