Posts: 2
Threads: 1
Joined: Jan 2020
Jan-05-2020, 09:29 AM
(This post was last modified: Jan-05-2020, 09:38 AM by Larz60+.)
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"]
Posts: 8,160
Threads: 160
Joined: Sep 2016
Posts: 50
Threads: 2
Joined: Nov 2019
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
Posts: 4,790
Threads: 76
Joined: Jan 2018
Jan-06-2020, 02:27 PM
(This post was last modified: Jan-06-2020, 02:28 PM by Gribouillis.)
@ sandeep_ganga Your 'remove double quotes' step does absolutely nothing. You could as well remove it. (Why?)
Posts: 50
Threads: 2
Joined: Nov 2019
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
Posts: 2
Threads: 1
Joined: Jan 2020
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)
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 4,790
Threads: 76
Joined: Jan 2018
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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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  ).
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.
|