Last active
August 13, 2016 23:33
-
-
Save misternu/0764b026780a787730a2f3bf8bdf03d9 to your computer and use it in GitHub Desktop.
Six Degrees of Separation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Get all the tweets in an array of hashes | |
| tweets = [] | |
| File.open('sample_input.txt') do |f| | |
| tweets = f.map do |line| | |
| { | |
| user: line[/^\w+(?=:)/], | |
| mentions: line.scan(/(?<=@)\w+/) | |
| } | |
| end | |
| end | |
| # Get all the users that are mentioned by users | |
| mentions = {} | |
| tweets.each do |tweet| | |
| mentions[tweet[:user]] ||= [] | |
| mentions[tweet[:user]] += tweet[:mentions] | |
| mentions[tweet[:user]] = mentions[tweet[:user]].uniq | |
| end | |
| p mentions | |
| # Get only the users that mention you back | |
| connections = Hash[mentions.map { |k, _| [k, []] }] | |
| mentions.keys.combination(2) do |a, b| | |
| if mentions[a].include?(b) && mentions[b].include?(a) | |
| connections[a] << b | |
| connections[b] << a | |
| end | |
| end | |
| p connections | |
| # For each user | |
| connections.keys.each do |user| | |
| # Say the username | |
| puts user | |
| # Don't count themselves as a potential connection | |
| covered = [user] | |
| # Their connections are the first layer | |
| layer = connections[user] | |
| # Until the layer we are on is empty | |
| until layer == [] | |
| # Print who is in it | |
| puts layer.join(', ') | |
| # Add them to the connections we have already found | |
| covered += layer | |
| # And replace layer with their connections minus what's covered | |
| layer = layer.flat_map { |u| connections[u] } .uniq - covered | |
| end | |
| puts | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've started a repo to try to do this with objects. If you're reading this, maybe go have a look there