multiple set operations in python
Asked Answered
M

9

6

I am new to python and learning it now. I am practicing it online and came across with the below problem. I tried to solve it, however, though I am getting the expected result the online validator is saying it as wrong. Please suggest where I am going wrong.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In a school, there are total 20 students numbered from 1 to 20. You’re given three lists named ‘C’, ‘F’, and ‘H’, representing students who play cricket, football, and hockey, respectively. Based on this information, find out and print the following:

  • Students who play all the three sports
  • Students who play both cricket and football but don’t play hockey
  • Students who play exactly two of the sports
  • Students who don’t play any of the three sports

Format:

Input:

3 lists containing numbers (ranging from 1 to 20) representing students who play cricket, football and hockey respectively.

Output:

4 different lists containing the students according to the constraints provided in the questions.

Examples: Input:

[[2, 5, 9, 12, 13, 15, 16, 17, 18, 19]
[2, 4, 5, 6, 7, 9, 13, 16]
[1, 2, 5, 9, 10, 11, 12, 13, 15]] 

Expected Output:

[2, 5, 9, 13]
[16]
[12, 15, 16]
[3, 8, 14, 20]

Below is my code

C = set(input_list[0])
F = set(input_list[1])
H = set(input_list[2])
A= set(range(1, 21))

print(sorted(list(C & F & H)))
print(sorted(list((C & F) - H)))
print(sorted(list(((C-F)&H | (C-H)&F))))
print(sorted(list(A-(C|F|H))))

I am not sure if A is really needed or not.

Thanks,

Monometallism answered 1/10, 2018 at 15:30 Comment(3)
print(sorted(list(C.intersection(F).intersection(H)))) Also docs.python.org/2/library/sets.htmlFabian
The result seems correct but there is a mistake for the third list as blhsing pointed out. But his answer is also wrong...Glomerulus
@JeanPaul Fixed with parentheses. Forgot that | and ^ do not have precedence over -.Spirit
S
4

You're correct on all but the students who play exactly two of the sports, which should be:

(C|F|H) - (C^F^H)
Spirit answered 1/10, 2018 at 15:42 Comment(2)
Now it seems good but beware it will only work for 3 sports. It may be shortest answer but not the easiest to interpret.Glomerulus
@JeanPaul: Extrapolating to >3 sports is probably best done with collections.Counter. update the Counter with all the sets, then set comprehension for {sid for sid, cnt in counter.items() if cnt == 2}Gurias
F
3
import ast,sys

input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)

C = input_list[0]
F = input_list[1]
H = input_list[2]
C = set(input_list[0])
F = set(input_list[1])
H = set(input_list[2])

print(sorted(list(C & F & H)))
print(sorted(list((C & F) - (C & F & H))))
print(sorted(list(((C & F) | (F & H) | (C & H)) - (C & F & H))))
print(sorted(list(set(range(1,21)) - (C | F | H))))
Fray answered 20/1, 2019 at 15:51 Comment(1)
Code-only answers aren't always the most helpful. I think you need to, at a minimum add some explanation, and correctly format your code.Wardwarde
H
2

Without the A set, the result should find the expected students out of nowhere because they are not part of any other set (by definition). So, the A set is really needed to contain the students that are not part of the other sets.

Hereditable answered 1/10, 2018 at 15:39 Comment(1)
That does not answer the main question.Glomerulus
F
2
print(sorted(list(set(C)&set(F)&set(H))))

print(sorted(list(set(C)&set(F)-set(H))))

y=set(C)&set(F)&set(H)

print(sorted(list(((set(C)&set(F))|(set(H)&set(F))|(set(C)&set(H)))-y)))

print(sorted(list(A-(set(C)|set(F)|set(H)))))
Flirt answered 29/12, 2018 at 19:10 Comment(1)
Consider including a description of your code to help others understand it and how it answers the posted question.Cleora
C
2
# Read the three input lists, i.e. 'C', 'F', and 'H'.

C = input_list[0]
F = input_list[1]
H = input_list[2]

# Write your code here
CS=set(C)
FS=set(F)
HS=set(H)
CHF=set(range(1,21))

A=(CS.intersection(FS.intersection(HS)))
B=((FS.intersection(CS))-CS.intersection(FS.intersection(HS)))
C=((((CS-FS).intersection(HS)).union((HS-CS).intersection(FS))).union((FS-HS).intersection(CS)))
D=(CHF-(CS.union(FS.union(HS))))

print(sorted(list(A)))
print(sorted(list(B)))
print(sorted(list(C)))
print(sorted(list(D)))
Crymotherapy answered 5/10, 2019 at 19:11 Comment(0)
T
1
input_list_c = [2, 5, 9, 12, 13, 15, 16, 17, 18, 19]
input_list_f = [2, 4, 5, 6, 7, 9, 13, 16, 20]
input_list_h = [1, 2, 5, 9, 10, 11, 12, 13, 15, 20]

mc = max(input_list_c)
mf = max(input_list_f)
mh = max(input_list_h)

if mc > mf & mc > mh:
    m = mc
elif mf > mc & mf > mh:
    m = mf
else:
    m = mh

nc = min(input_list_c)
nf = min(input_list_f)
nh = min(input_list_h)

if nc < nf & nc < nh:
    n = nc
elif nf < nc & nf > nh:
    n = nf
else:
    n = nh

C = set(input_list_c)
F = set(input_list_f)
H = set(input_list_h)
A = set(range(n,m))
Y = (C&F&H)

print(sorted(list(Y)))
print(sorted(list((C&F)-H)))
print(sorted(list((C&F|C&H|F&H)-Y)))
print(sorted(list(A-(C|F|H))))
Tricho answered 6/1, 2019 at 11:56 Comment(0)
T
0

Working with the same question, I found that the lack of explanation with the answers made it difficult to understand what was going on so I'll post my code with an explanation of how I solved the problem.

Context code :

C = input_list[0]
F = input_list[1]
H = input_list[2]

set_C = set(C)
set_F = set(F)
set_H = set(H)
set_all = set([x for x in range(1,21)])

Students who play all the three sports : We can just take intersection of all 3 sets to give us the answer.

all_three_sports = set_C.intersection(set_F,set_H)

Students who play both cricket and football but don’t play hockey : We can take the intersection of the set of players who play football and the set of players who play cricket and then subtract the set of players that play hockey.

c_and_f_but_not_h = (set_F.intersection(set_C)).difference(set_H)

Students who play exactly two of the sports : For this, we take the intersection of the set of players who play football and hockey and then subtract the set of players that play cricket. Secondly, we take the intersection of set of players who play cricket and hockey and then subtract the set of players that play football. Lastly, we take the intersection of set of players who play football and cricket and then subtract the set of players who play hockey.

As we can see, this leaves us with three sets of players that only play two sports. We get our answer by union-ing all the results.

only_two_sports = (set_C.intersection(set_F)-set_H).union((set_H.intersection(set_F)-set_C),(set_C.intersection(set_H)-set_F))

Students who don’t play any of the three sports : Here we just subtract the union of all the sets of players we received with the total set of players.

neither_sports = set_all - (set_C.union(set_F,set_H))

Now, we just convert set to list and sort them before printing :

print(sorted(list(all_three_sports)))
print(sorted(list(c_and_f_but_not_h)))
print(sorted(list(only_two_sports)))
print(sorted(list(neither_sports)))
Torpid answered 30/12, 2019 at 15:38 Comment(0)
H
0
C = input_list[0]
F = input_list[1]
H = input_list[2]

C = set(input_list[0])
F = set(input_list[1])
H = set(input_list[2])
print(sorted(list(C & F & H)))
print(sorted(list((C & F) - (C & F & H))))
print(sorted(list(((C & F) | (F & H) | (H & C)) - (C&F&H))))
print(sorted(list(set(range(1,21)) -(C | F | H))))
Hatpin answered 10/4, 2020 at 12:9 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Hulbert
I
0

print(sorted(list(set(C).intersection(F).intersection(H))))

print(sorted(list(set(C).intersection(F).difference(H))))

print(sorted(list(set(C).intersection(F).union(set(F).intersection(H)).union(set(C).intersection(H)).difference(set(C).intersection(F).intersection(H)))))

print(sorted(list(set(range(1,21)).difference(set(C).union(F).union(H)))))

Iq answered 7/5, 2024 at 6:42 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Jocelynjocelyne

© 2022 - 2025 — McMap. All rights reserved.