Advertisement
Adehumble

Party-Handshake

May 7th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. #Parameters Initialisation
  2. all_unique_id_list=[]
  3. dict={}
  4. all_dislikes=[]
  5. ind_likes_list=[]
  6. answer=[]
  7.  
  8. #This extracts information from the text file
  9. with open("handshake.txt", "r") as party:
  10.     #The first line in the text file represents the total number of people at the party
  11.     no_people=int(party.readline().rstrip("\n"))
  12.     #This will iterates over the next lines of the text file
  13.     for i in range(1,no_people):
  14.         file_line=(party.readline().rstrip("\n").split(" "))
  15.         try:
  16.             file_row=[int(j) for j in file_line]
  17.         except ValueError:
  18.             break
  19.         all_dislikes.append(file_row)
  20.  
  21.        
  22. all_attendees_list=[i for i in range(1,no_people+1)]
  23. all_attendees_set=set(all_attendees_list)
  24.  
  25. #To match individual unique ID with the people they dislikes
  26. for i in all_dislikes:
  27.     ind_unique_id=i[0]
  28.     all_unique_id_list.append(ind_unique_id)
  29.     dict.update({ind_unique_id:i[1:]})
  30.    
  31. #To find the missing identity  
  32. all_unique_id_set=set(all_unique_id_list)
  33. missing_id=list(all_attendees_set-all_unique_id_set)
  34.  
  35. #To update the dictionary
  36. for i in missing_id:
  37.     current_attendees=all_attendees_list.copy()
  38.     current_attendees.remove(i)
  39.     dict.update({i:[]})
  40.  
  41. #The main algorithm
  42. for i in dict:
  43.     current_attendees=all_attendees_list.copy()
  44.     current_attendees.remove(i)
  45.     for j in current_attendees:
  46.         if (j not in dict[i]) and (i not in dict[j]):
  47.             ans=f"{i}:{j}"
  48.             answer.append(ans)
  49.         else:
  50.             continue
  51.            
  52. #Total number of repeated handshakes
  53. answer1=len(answer)
  54. print(f"The total number of handshake is: {round(answer1/2)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement