What is the shortest Twitter #hashtag that has never been used? As an additional constraint, let’s focus on the lexicographically first hashtag composed of ASCII letters only of that kind. Let’s skip dessert and use Python and the Twitter Search API to find out:
import urllib import json import itertools import string import time k, max_k = 1, 10 while k < max_k: for tag in itertools.product(string.ascii_lowercase, repeat=k): tag = ''.join(tag) print "Searching for #%s" % tag search_url = 'https://search.twitter.com/search.json?q=%%23%s' % tag while True: search_result = json.loads(urllib.urlopen(search_url).read()) if 'results' in search_result: break print "Wait a few seconds because of Twitter Search API limit" time.sleep(5) search_result = search_result['results'] if not search_result: print "Unused hashtag: #%s" % tag k = max_k break k += 1
After a few minutes, the result: #agy. What could we use that one for?