## The Friendship Algorithm

### The Friendship Algorithm

we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: The Friendship Algorithm

I (of course) implemented the Algorithm in Python.

Disclaimer: I HAVE NOT TESTED THIS. I just shoved it together in fifteen minutes of creative frenzy.
Also, I am not familiar with the show, so if there are any discrepancies I missed, feel absolutely free to fix the code.

Now, the Algorithm!

Code: Select all
`from __future__ import *import time# some boilerplate classesclass FriendWannabe:    """Base person class. Whoever runs the algorithm is a Person."""    def __init__(self, interests):        self.interests = interests # list with at least six itemsclass PotentialFriend(FriendWannabe):    """Special case for the person who the person running the algorithm        wants to be friends with.        Inherits from FriendWannabe for simplicity's sake."""    def __init__(self, home, wants_a_beverage, favorite_beverage, wants_to_dine, interests):        super(Person, self).__init__(interests)        self.home = home # bool        self.favorite_beverage = favorite_beverage # string        self.wants_to_dine = wants_to_dine    def call_back():        print("Hello?")# some boilerplate functionsdef dine_together():    print("Isn't this food delicious?")def have_beverage(beverage):    list_of_beverages = ["tea", "coffee", "cocoa"]    while beverage not in list_of_beverages:        raise StandardError("Hot beverage not in algorithm")    else:        time.sleep(5)        print("Here's your {}".format(beverage))def partake_in_interest(interest):    print(interest + " sure is fun!")def place_phone_call(recipient):    for i in range(3):        print("Ring!")        time.sleep(1)    if recipient.home:        print("Hello?")def begin_friendship():    print("Friends!")# let's roll!def the_algorithm(friend_wannabe, potential_friend):    """The Friendship Algorithm."""    place_phone_call(potential_friend)    if not potential_friend.home:        time.sleep(5)        potential_friend.call_back()    # else    print("Would you like to share a meal?")    if potential_friend.wants_to_dine:        dine_together()        begin_friendship()        return    # else    print("Do you enjoy a hot beverage?")    if potential_friend.wants_a_beverage:        have_beverage(potential_friend.favorite_beverage)        begin_friendship()        return    # else    print("Tell me one of your interests.")    for index, i in enumerate(potential_friend.interests):        if i in friend_wannabe.interests:            print("Why don't we do that together?")            partake_in_interest(i)            begin_friendship()            return        else:            print("Tell me another one of your interests.")    else:        least_objectionable_interest = ""        for index, i in enumerate(potential_friend.interests):            if len(i) > least_objectionable_interest:                least_objectionable_interest = i        partake_in_interest(least_objectionable_interest)        begin_friendship()        return`
Proverbs 26:14 describes me a bit too well.

Version: Python 3.4.3

https://github.com/4of92000
https://github.com/PythonForum/

XndrK

Posts: 208
Joined: Sat Jun 15, 2013 7:57 pm