Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compareFriends
#1
How can I approach with the solution to the below question?

A String array “F1” has got names of Facebook users and their friend association.
For example if we write: U1,U2 it implies that U1 is a friend of U2. This also implies that U2 is a friend of U1.
 
Write a program which will read “F1” and remove duplicates and write all the unique pairs to “F2”.
But, before removing duplicates sort the input array based on the 0th index in alphabetical order
 
For example, if the input “F1” might have the following data: 
Input String => ["U1,U2","U3,U4","U2,U1","U1,U5"]
Output String => ["U1,U2","U1,U5","U3,U4"]
Reply
#2
what have you tried?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
May be you should try split and then compare.

list1=["U1,U2","U3,U4","U2,U1","U1,U5"]

list2= map(lambda each:each.strip('"'), list1)  # remove double quotes

temp=[]

for i in list2:

                #print(i)

                temp.append(i.split(','))  #split seperated comma

print(temp)

print(set(tuple(sorted(i)) for i in temp))

#print(set(frozenset(item) for item in temp))#pull unique stuff
Output:
python 6jan_2.py [['U1', 'U2'], ['U3', 'U4'], ['U2', 'U1'], ['U1', 'U5']] {('U3', 'U4'), ('U1', 'U5'), ('U1', 'U2')}
Best regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#4
@sandeep_ganga Your 'remove double quotes' step does absolutely nothing. You could as well remove it. (Why?)
Reply
#5
Yes that makes sense, you are correct. we can remove that line.

list1=["U1,U2","U3,U4","U2,U1","U1,U5"]
temp=[]
for i in list1:
	temp.append(i.split(','))  #split seperated comma
print(temp)
print(set(tuple(sorted(i)) for i in temp))
#print(set(frozenset(item) for item in temp))#pull unique stuff
Output:
python test.py [['U1', 'U2'], ['U3', 'U4'], ['U2', 'U1'], ['U1', 'U5']] {('U3', 'U4'), ('U1', 'U2'), ('U1', 'U5')}
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#6
def compare_friends(frnds_list):
    unique_users = []
    for items in frnds_list:
        users = items.split(",")
        user1 = users[0]
        user2 = users[1]

        key = user2 + "," + user1 if user1 > user2 else user1 + "," + user2
        unique_users.append(key)

    unique_users.sort()
    res = [i for n, i in enumerate(unique_users) if i not in unique_users[:n]]
    return str(res)
Reply
#7
How to approach this problem? Describe it.

One way to do it as follows:

For every connection (presented as pair of values separated by comma):
- sort values in connection
- keep only unique connections

From this description some more or less obvious choices: for-loop (for every connection), split (to get values to sort), sorted (to sort values), .join (to construct string with sorted values) and set (to uniquify connections).

Using set comprehension it can be expressed in quite condensed way (shorter than in spoken language):

>>> connections = ["U1,U2","U3,U4","U2,U1","U1,U5"]
>>> {','.join(sorted(connection.split(','))) for connection in connections}
{'U1,U2', 'U1,U5', 'U3,U4'} 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
perfringo Wrote:Using set comprehension it can be expressed in quite condensed way (shorter than in spoken language)
In spoken language, you could say that we want the sorted set of sorted pairs of friends, which description of the problem is very close to the code.
Reply
#9
(Jan-07-2020, 10:06 AM)Gribouillis Wrote:
perfringo Wrote:Using set comprehension it can be expressed in quite condensed way (shorter than in spoken language)
In spoken language, you could say that we want the sorted set of sorted pairs of friends, which description of the problem is very close to the code.

I agree but at the same time have an opinion that in order to avoid ambiguity the description in spoken language should be much longer (Fun fact: Merriam-Webster dictionary has 24 definitions of set as noun Big Grin).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020