Python : helper function confusion

Go To StackoverFlow.com

0

For one of the functions of my assignment I am supposed to write a code for this recommendation score below.

But I have no idea where I should start off. I was told to use helper functions but I dont know what kinda helper functions, like for instance their name

Recommendation score

For each person, all people in the social network who they are not currently friends with are potential friends. For a particular person, each potential friend is scored using the following point system:

  • for every mutual friend that the person and the potential friend have, add 1 point to the potential friend's score
  • for each network that the person and the potential friend both belong to, add 1 point to the potential friend's score
  • if the person has the same last name as a potential friend who was identified through one or both of the previous two methods, add 1 point to the potential friend's score

The link to the assignment is at http://bit.ly/H9hCia, if it makes it easier, its just based on this how would I start it. I was told by everyone that as long as I understand this description then I can start on it.But that doesn't seem like it, when you guys read it do u need more information? Because Im so confused so I decided to post here and ask what you guys think. Since I can't start on it at all. If I do need to further explain more I will.

2012-04-04 19:57
by kungkung
It looks like you have enough information here. Have you studied classes or are you just supposed to use functions - aaronasterling 2012-04-04 20:05
Uh what do u mean by classes.. yea functions. This is a basic course, so im learning all from scratch so I dont think its anything advanceddd - kungkung 2012-04-04 20:07
They have given you starter code, and described the datastructures. What more do you want - Marcin 2012-04-04 20:16
... Well im sorry to me its confusingg. Im still trying to peice things togeher. I just dont know how to start off the code itself. The starer code are just the functions definitions . thehelper functions is wat I am stuck a - kungkung 2012-04-04 20:22
I suggest you start by learning how to create a dictionary, and playing around with the data, trying do things with your string when you are comfortable doing that, you can start thinking about what the project want, and how to do them. I hope this help - George 2012-04-04 22:10


1

A few hints.

You have a social graph. The 'person to friends' dict is its incidence table: for each node (person) it shows which nodes it connects to.

"Mutual friends" is obviously two persons that have each other in their incidence lists. Make a helper function that determines if two given people are mutual friends. Maybe make a function that lists all mutual friends of a given person.

Then you can enumerate persons in 'persons to friends' (dict.keys() is your friend) and compute scores. Be sure to check that you don't include actual friends of a person into potential friends.

To find whether two persons belong to at least one common network, convert lists of networks into sets and use set intersection (&).

To find first and last names, use str.split() on them.

Start an interactive Python interpreter. Write simple functions, play with your data a bit. Don't try to fit everything in your head at once and write down in one go. Instead, experiment.

2012-04-04 20:18
by 9000
Hmm, I understand what your saying, :) Thanks. So how would I start it off tho, like, like i started off naming my first helper function "def ptsforfriends():" Im not too sure of the parameters. So under here I'd do all the comparing right - kungkung 2012-04-04 20:23


0

Before you start thinking about writing functions and all. You should tell us what do you know or what do you not know. Do you know what is a dictionary? Your project require dictionary, do you know how to create variables? As a student myself, I suggestion you start by writing the code itself first, playing around with the data, (such as loading them, printing them, comparing them). Doing so you will be more comfortable with writing code.

First thing you should do is to learn dictionary , figure out how to create one, how to set the value, get the values, etc. Read the following tutorial, and see if you understand dictionary. It is no point to try to write a function, if you don't know how to use the dictionary to being with.

Read this for dictionary: http://docs.python.org/library/stdtypes.html#typesmapping

Read this for string: http://docs.python.org/tutorial/introduction.html#strings

Read this for for, if and others: http://docs.python.org/tutorial/controlflow.html

When you finish the that three tutorials, you will learn how to create and use dictionary, and string. Then you can think about how to put your codes into function, and how to write your program to solve the problem. We want to help, but we need to know where you get stuck, give us information such as the error message, or (how to do xxx) (try google it first). Good luck.

2012-04-04 22:01
by George
Thank you so much, And yes I do know what dictionaries are, its just wen we get to varyin them and incorporating them into somany diffferent functions i get so lost - kungkung 2012-04-05 01:09
If you know how to use them, then you can read function here: http://www.tutorialspoint.com/python/python_functions.htm And you are welcome to post question if you are stuc - George 2012-04-05 03:46
Ads