Created
October 28, 2020 11:56
-
-
Save wildonion/69efb2e426222cd6d0f4ae5ae3e1776e to your computer and use it in GitHub Desktop.
unhappy friends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os | |
# unhappy friends | |
# https://leetcode.com/contest/weekly-contest-206/problems/count-unhappy-friends/ | |
# eg ::: input: [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]], output: 4 | |
if __name__ == "__main__": | |
f_n = int(input("Enter friends ::: ")) | |
if 2 <= f_n <= 500 and f_n % 2 == 0: | |
preferences = [] | |
pairs = [] | |
for i in range(f_n): | |
friends = [] | |
for j in range(1, f_n): | |
f = int(input(f"Enter friends of person {i} based on preferences order ::: ")) | |
if f not in friends and 0 <= f <= f_n-1 and f != i: | |
friends.append(f) | |
else: | |
print("[-] either repeated friend in rel list, lower/upper bound issue or found existing person her/him-self in his/her rel list") | |
sys.exit(1) | |
preferences.append(friends) | |
for k in range(int(f_n/2)): | |
p_friends = [] | |
for l in range(2): | |
f = int(input(f"Enter person {l} of pair {k} ::: ")) | |
if f not in p_friends and 0 <= f <= f_n-1: | |
p_friends.append(f) | |
else: | |
print("[-] either repeated friend in one pair or lower/upper bound issue") | |
sys.exit(1) | |
pairs.append(p_friends) | |
p_tmp = pairs[0] | |
for n in range(1, len(pairs)): | |
if pairs[n][0] == p_tmp[0] or pairs[n][1] == p_tmp[1]: | |
print("[-] found existing person in another pair") | |
sys.exit(1) | |
else: | |
p_tmp = pairs[n] | |
count_u_h_f = 0 | |
for p in pairs: | |
f_p = p[0] | |
s_p = p[1] | |
pref_f_p = preferences[f_p] | |
pref_s_p = preferences[s_p] | |
is_f_happy = True | |
is_s_happy = True | |
if pref_f_p[0] != s_p: | |
is_f_happy = False | |
count_u_h_f += 1 | |
if pref_s_p[0] != f_p: | |
is_s_happy = False | |
count_u_h_f += 1 | |
if count_u_h_f != 0: | |
print(f"{count_u_h_f} friends are unhappy :-(") | |
else: | |
print("no one is unhappy :-)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment