0

Say i have a dict, dict is a dictonary with dictonary as value

dict = {'**Leon**':{'Name':'Leon L','**follow**':['Apple', 'PPy','Jack','Tommy']},'**Jack**':{'name':'Jack Y','**follow**':['Apple','Cruise','Jay']},'**Tommy**':{'name':'Tommy T','**follow**':['Hill']},'**Apple**':{'name':'Apple A','**follow**':['Jack']},**'Cruise'**:{'name':'Cruise L','**follow**':['Jay']}}

**follow** means users that is followed by this user, for example: Leon follows Apple, PPy,Jack,Tommy

and i have query file that has tasks in it. We need to accomplish the task and return a list of usernames (the usernames are the keys in dict, eg 'Leon','Jack','Tommy'), the format of that file is:

SEARCH
Leon
follow
follow-by # there might be many more follow, and follow-by
FILTER
name-include Leon
follow Apple # format: keyword follow, a space and a username. same apply to follow-by username
follow-by Leon # there might be more name-include, , follow username, follow by username

the meaning of the query file: The SEARCH and FILTER are keywords. The line after SEARCH is the starting username (we need to put it in the list, lets name the list user_list). The search specification has the 2 steps (in this case):

  1. create a list that has Leon in it, call this list L1
  2. follow says to replace each person p in L1 with people who follows p, then we get L2
  3. follow-by says to replace each person p in L2 with people who are followed by p (people who are followed by p are in the follow list of the user profile). Then we get L3

The filter specification (in this example):

  1. for each person p in L3, if p's name has 'Leon' in it, the user is kept in the list.Then we get L4
  2. for each person p in L4, if p follows Apple, then p is kept in the list, we get L5
  3. for each person p in L5, if p is followed by Leon, p is kept in the list. Then we get our final list. We need to return the final list

Can anyone help me to write a program that can accomplish the task?

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
John
  • 71
  • 2
  • 4
  • 5
    It would be nice if you showed us what you had so far or tell us what you are stuck on. We aren't here to do your work for you. – GWW Nov 26 '10 at 01:17

1 Answers1

0

I would suggest using a state machine, or more specifically pushdown automata. You would initialize the machine with the data dict, then for every line in the input file, you will transition into a new state and do the work specified, storing the result in the stack. Every step can access the data returned from the previous step.

knutin
  • 5,033
  • 19
  • 26