Question

In: Computer Science

Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...

Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work.

Below is the starter template for the code:

def overlap(user1, user2, interests):
""" Return the number of interests that user1 and user2
have in common """
return 0

  
def most_similar(user, interests):
""" Determine the name of user who is most similar
to the input user defined by having the most interests
in common. Return a tuple: (most_similar_user, num_interests_in_common) """
return ("", 0)
  

def recommendations(user, interests):
""" Find the user who shares the most interests with the specified user.
Recommend to user any interests that his or her closest match has
that are not already in the user's list of interests.
Return a list of recommendations. """
return []
  
#%% Don't change this:
  
interests = {
"Pam":["Hadoop", "Big Data", "HBase", "Java", "Spark", "Storm", "Cassandra"],
"Ann":["NoSQL", "MongoDB", "Cassandra", "HBase", "Postgres"],
"Raj":["Python", "scikit-learn", "scipy", "numpy", "statsmodels", "pandas"],
"Jim":["R", "Python", "statistics", "regression", "probability", "programming languages"],
"Eve":["machine learning", "regression", "decision trees", "libsvm"],
"Carl Yastrzemski":["Python", "R", "Java", "C++", "Haskell", "programming languages"],
"Sam":["statistics", "probability", "mathematics", "theory"],
"Una":["machine learning", "scikit-learn", "Mahout", "neural networks"],
"Liv":["neural networks", "deep learning", "Big Data", "artificial intelligence"],
"Bob":["Hadoop", "Java", "MapReduce", "Big Data"],
"Jon":["statistics", "R", "statsmodels"],
"Tom":["pandas", "R", "Python"],
"Tim":["databases", "HBase", "Postgres", "MySQL", "MongoDB"],
"Hal":["libsvm", "regression", "support vector machines"],
"Joe":["C++", "deep learning", "artificial intelligence", "probability"]
}


#%% Don't change this either!

for user in interests.keys():
print(user, recommendations(user, interests))


#%% Put output here

"""
Your output
"""

Solutions

Expert Solution

PYTHON CODE:

""" Return the number of interests that user1 and user2
    have in common """
def overlap(user1, user2, interests):
    num_of_interests = 0;
    # Iterates through each value of user1
    for user1_values in interests[user1]:
        # if true, num_of_interests will be incremented by 1
        if (user1_values in interests[user2] ):
            num_of_interests += 1;
    return num_of_interests;

""" Determine the name of user who is most similar
to the input user defined by having the most interests
in common. Return a tuple: (most_similar_user, num_interests_in_common) """
def most_similar(user, interests):
    user_count = {};
    # Iterates through each user in the interest dictionary
    for dict_user in interests.keys():
        # Ignores the input user
        if (user != dict_user ):
            count = overlap(user, dict_user, interests);
            # All the count values with user names are stored as key and value in user_count 
            user_count[dict_user] = count;
            count = 0;
    # maximum value in the user_count dict is found and returned
    most_similar_user = max(user_count, key = user_count.get);
    return (most_similar_user, user_count[most_similar_user])
  
""" Find the user who shares the most interests with the specified user.
    Recommend to user any interests that his or her closest match has
    that are not already in the user's list of interests.
    Return a list of recommendations. """
def recommendations(user, interests):
    recommendation = [];
    # input_user_interests has the interests of the input user of type set
    input_user_interests = set(interests[user]);
    # Calls the most_similar function
    most_similar_user = most_similar(user, interests);
    # most_similar_user_interests has the interests of the most similar user of type set
    most_similar_user_interests = set(interests[most_similar_user[0]]);
    # UnionSet now has all the interests of both the users
    UnionSet = input_user_interests.union(most_similar_user_interests);
    # UnionSet now has only the interest of most similar user (common interests not included)
    UnionSet = UnionSet - input_user_interests;
    return list(UnionSet);
  
interests = {
"Pam":["Hadoop", "Big Data", "HBase", "Java", "Spark", "Storm", "Cassandra"],
"Ann":["NoSQL", "MongoDB", "Cassandra", "HBase", "Postgres"],
"Raj":["Python", "scikit-learn", "scipy", "numpy", "statsmodels", "pandas"],
"Jim":["R", "Python", "statistics", "regression", "probability", "programming languages"],
"Eve":["machine learning", "regression", "decision trees", "libsvm"],
"Carl Yastrzemski":["Python", "R", "Java", "C++", "Haskell", "programming languages"],
"Sam":["statistics", "probability", "mathematics", "theory"],
"Una":["machine learning", "scikit-learn", "Mahout", "neural networks"],
"Liv":["neural networks", "deep learning", "Big Data", "artificial intelligence"],
"Bob":["Hadoop", "Java", "MapReduce", "Big Data"],
"Jon":["statistics", "R", "statsmodels"],
"Tom":["pandas", "R", "Python"],
"Tim":["databases", "HBase", "Postgres", "MySQL", "MongoDB"],
"Hal":["libsvm", "regression", "support vector machines"],
"Joe":["C++", "deep learning", "artificial intelligence", "probability"]
}

#%% Don't change this either!

for user in interests.keys():
    print(user, recommendations(user, interests))

print("Pam and Ann have",overlap("Pam", "Ann", interests), "interests in common");
print(most_similar("Pam", interests)[0], "is the most similar to Pam");

OUTPUT:


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: g. def big_words(text, min_length=10): """ Return a list of big words whose length is at least min_length """ return [] h. def common_words(text, min_frequency=10): """ Return words occurring at...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
1. Write code in mips that will play battleships. Include comments in code on what each...
1. Write code in mips that will play battleships. Include comments in code on what each part is doing.
python code You are to write a program which produces a triangle as seen below. •Include...
python code You are to write a program which produces a triangle as seen below. •Include a function named goingUpwhich accepts a value and prints lines of stars beginning at 1 star and ending at the number of stars which was sent to it. For example, if the value sent to it is 4, the function would print this:********** •Include a function named goingDown which accepts a value and prints lines of stars beginning at 1 LESS THAN the value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT