Skip to content

Instantly share code, notes, and snippets.

Created April 10, 2016 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7ccdd86b43442731694e67a484069fe1 to your computer and use it in GitHub Desktop.
Save anonymous/7ccdd86b43442731694e67a484069fe1 to your computer and use it in GitHub Desktop.
# Description:
# You start working for a fancy new startup hoping to revolutionize social networking! GASP! They had this great idea that users should be able to specify relevant keywords to their posts using an ingenious idea by prefixing those keywords with the pound sign (#). Your job is to extract those keywords so that they can be used later on for whatever purposes.
# Note:
# Pound signs alone do not count, for example: the string "#" would return an empty array.
# If a word is preceded by more than one hashtag, only the last hashtag counts (e.g. "##alot" would return ["alot"])
# Hashtags cannot be within the middle of a word (e.g. "in#line hashtag" returns an empty array)
# Hashtags must precede alphabetical characters (e.g. "#120398" or "#?" are invalid)
def get_hashtags(phrase)
phrase = phrase.squeeze("#").split()
hashtagged_words = []
phrase.each do |word|
if (word[0] == "#") && (word.length > 1) && (word[1] =~ (/[[:alpha:]]/))
word.slice!(0)
hashtagged_words << word unless word.include?("#")
end
end
return hashtagged_words
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment