Skip to content

Instantly share code, notes, and snippets.

@captproton
Created June 17, 2011 10:32
Show Gist options
  • Select an option

  • Save captproton/1031187 to your computer and use it in GitHub Desktop.

Select an option

Save captproton/1031187 to your computer and use it in GitHub Desktop.
Ransom Note Problem
# Game plan:
## create an array of words to use in the ransom note
## create a larger hash of popularly used words with the second attribute of the hash as count of words available. Fails when note's counts of letters is less than vocab.
## search larger hash of words for words to be used in the note
## if word is not available, put "ransom note word 'word used' is not available"
## else put "ransom note successfully generated"
# Plussed Game Plan
## Collection of words is stored in google doc or at http://www.infochimps.com/datasets/word-frequencies-from-the-british-national-corpus, retrieved through yql, parsed into a hash
ransom_note_words = [ 'twinkle', 'little', 'star', 'how', 'I', 'wonder','Mike', 'Caldwell']
vocab_words = [ 'twinkle', 'little', 'star', 'how', 'I', 'wonder','Mike', 'Caldwell', 'the', 'founder', 'of', 'a', 'small', 'company', 'in', 'Salt', 'Lake', 'City', 'that', 'makes', 'timekeeping', 'software', 'considers', 'himself', 'financially', 'conservative.', 'He', 'also', 'a', 'geek', 'and', 'couldn', 'help', 'but', 'get', 'excited', 'late', 'last', 'year', 'when', 'he', 'heard', 'about', 'bitcoin', 'a', 'new', 'virtual', 'currency.', 'In', 'February', 'on', 'one', 'of', 'the', 'handful', 'of', 'online', 'exchanges', 'that', 'have', 'appeared', 'over', 'the', 'past', 'year', 'he', 'bought', 'about', '$20,000', 'worth', 'at', 'less', 'than', 'a', 'dollar', 'per', 'bitcoin.', '"I', 'felt', 'that', 'maybe', 'I', 'would', 'get', 'a', '20', 'or', '50', 'percent', 'return,"', 'says', 'Caldwell', '33.', 'By', 'early', 'June', 'when', 'he', 'sold', 'the', 'last', 'of', 'his', 'stake', 'a', 'bitcoin', 'was', 'worth', 'around', '$30.', 'Caldwell', 'says', 'his', 'total', 'return', 'was', 'well', 'over', '1,000', 'percent.', '"That', 'magnitude', 'was', 'totally', 'unexpected,"', 'he', 'says','For', 'those', 'who', 'got', 'in', 'early', 'it', 'a', 'gold', 'rush.', 'In', 'the', 'last', 'few', 'months', 'this', 'online', 'cash', 'has', 'exploded', 'in', 'usage', 'notoriety', 'and', 'value.', 'Like', 'dollars', 'or', 'yen', 'a', 'bitcoin', 'worth', 'fluctuates', 'with', 'demand', 'but', 'in', 'its', 'short', 'history', 'it', 'gone', 'mostly', 'up.', 'About', '$130', 'million', 'worth', 'of', 'bitcoins', 'are', 'now', 'in', 'existence', 'and', 'the', 'value', 'of', 'that', 'stash', 'has', 'grown', 'more', 'than', '6,000', 'percent', 'this', 'year.', 'Bitcoin', 'partisans', 'are', 'breathless:', '"This', 'is', 'the', 'biggest', 'invention', 'since', 'the', 'Internet,"', 'says', 'Bruce', 'Wagner', 'an', 'entrepreneur', 'who', 'hosts', 'a', 'monthly', 'bitcoin', 'meet-up', 'group', 'in', 'Manhattan','The', 'project', 'was', 'started', 'in', '2009', 'by', 'an', 'enigmatic', 'programmer', 'known', 'as', 'Satoshi', 'Nakamoto', 'He', 'always', 'communicated', 'electronically', 'never', 'answered', 'personal', 'questions', 'and', 'disappeared', 'from', 'online', 'forums', 'in', 'December', 'The', 'conspiracy-minded', 'speculate', 'that', 'Nakamoto', 'was', 'actually', 'a', 'group', 'of', 'people', 'or', 'a', 'government', 'cryptographer', 'or', 'a', 'pseudonym', 'for', 'Gavin', 'Andresen', 'the', 'Amherst', 'Mass', 'programmer', 'who', 'took', 'over', 'the', 'project', 'after', 'Nakamoto', 'vanished', 'I', 'spoke', 'to', 'one', 'guy', 'who', 'thought', 'aliens', 'might', 'have', 'come', 'to', 'earth', 'to', 'bring', 'us', 'this', 'technology', 'it', 'so', 'perfect', 'says', 'Wagner', 'Digital', 'transactions', 'normally', 'require', 'a', 'trusted', 'intermediary', 'such', 'as', 'PayPal', 'to', 'credit', 'and', 'debit', 'accounts', 'properly', 'and', 'prevent', 'cheating.', 'With', 'bitcoins', '"there', 'is', 'no', 'middleman', 'says', 'Andresenor,"', 'more', 'accurately', 'there', 'a', 'distributed', 'middleman.', 'Individual', 'transactions', 'are', 'encrypted', 'logged', 'by', 'a', 'decentralized', 'network', 'running', 'on', 'thousands', 'of', 'home', 'computers', 'and', 'recorded', 'in', 'a', 'public', 'ledger.', 'The', 'system', 'works', 'similarly', 'to', 'peer-to-peer', 'music-sharing', 'networks', 'in', 'that', 'files', 'are', 'shared', 'among', 'swarms', 'of', 'users', 'rather', 'than', 'downloaded', 'from', 'a', 'central', 'server','Suppose', 'you', 'in', 'the', 'market', 'for', 'alpaca', 'socks', 'one', 'of', 'the', 'few', 'consumer', 'items', 'you', 'can', 'buy', 'with', 'bitcoins.', 'Step', 'one', 'Get', 'some', 'BTC','that', 'the', 'symbol', 'a', 'currency', 'exchange', 'site', 'such', 'as', 'Mt.Gox.', 'Then', 'download', 'a', 'desktop', 'app', 'from', 'bitcoin.org', 'which', 'will', 'store', 'your', 'lucre', '(some', 'use', 'an', 'online', 'service', 'such', 'as', 'Instawallet.com', 'instead', 'and', 'connect', 'you', 'to', 'the', 'decentralized', 'bitcoin', 'network.', 'Next', 'find', 'the', 'alpaca', 'farms', 'bitcoin', 'address', 'a', 'string', 'of', 'characters', 'also', 'known', 'as', 'a', 'public', 'key.', 'Click', 'the', 'send', 'coins', 'button']
def create_wordcount_hash(my_words)
wordcount_hash = {}
my_words.each do |word|
if wordcount_hash.has_key?(word)
wordcount_hash[word] = wordcount_hash[word] + 1
else
wordcount_hash[word] = 1
end
end
# sort vocabulary hash by wordcount value
wordcount_hash.sort_by { |k,v| v }.reverse!
end
## create word count of each word in ransom note, just like pulling words out of a grab bag.
## iterate through ransom note. As long as the word in the ransom note is greater than the count of the word in the vocabulary, pass true
def note_words_in_vocabulary(note, vocabulary)
unless note.count == 0
note.each do |word|
matching_word = vocabulary.assoc(word.first)
if matching_word
matching_word_count = matching_word.fetch(1)
else
matching_word_count = 0
end
return false unless matching_word
return false unless word.fetch(1) >= matching_word_count
end
end
end
vocabulary = create_wordcount_hash(vocab_words)
note = create_wordcount_hash(ransom_note_words)
note_words_in_vocabulary(note, vocabulary)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment