Created
December 21, 2011 19:15
-
-
Save krrrr38/1507273 to your computer and use it in GitHub Desktop.
twitterの道探すやつ.途中でやめた
This file contains 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
search route between 2 users in twitter | |
""" | |
print __doc__ | |
import sys | |
import twitter | |
step = 0 | |
api = twitter.Api() | |
class PersonNode: | |
def __init__(self, user): | |
""" | |
@param User object | |
""" | |
self.name = user.screen_name | |
self.friends = api.GetFriends(user.id) | |
self.pointer = None | |
def __str__(self): | |
return self.name | |
def printRoute(node): | |
""" | |
show route | |
""" | |
if node.pointer == None: | |
print node, " (", step," steps)" | |
else: | |
print node, " -> ", print_solution(node.pointer) | |
def breadthSearch(user1, user2): | |
""" | |
search route with breadth first order | |
""" | |
opens = [user1] | |
closed = [] | |
while True: | |
if len(opens) == 0: | |
return False | |
node = opens.pop(0) | |
if node == user2: | |
return True | |
closed.append(node) | |
for friend in node.friends: | |
friend = PersonNode(friend) | |
if friend in opens or friend in closed: | |
continue | |
friend.pointer = node | |
if friend == user2: | |
opens.insert(0, friend) | |
else: | |
opens.append(friend) | |
if __name__ == "__main__": | |
argv = sys.argv | |
if len(argv) != 3: | |
print 'Usage: # python %s user1_name user2_name' % argv[0] | |
sys.exit(1) | |
user1, user2 = PersonNode(api.GetUser(argv[1])), PersonNode(api.GetUser(argv[2])) | |
success = breadthSearch(user1, user2) | |
if success: | |
print "--- show route ---" | |
printRoute(user2) | |
else: | |
print "cannot find route" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment