Question

4a. In the card game Set, the deck consists of 81 cards, each with four attributes:...

4a. In the card game Set, the deck consists of 81 cards, each with four attributes:

  • number (one, two, three)
  • color (red, green, purple)
  • shape (diamond, squiggle, oval)
  • texture (open, solid, striped). Each possible combination of attributes occurs exactly once. Build a Python list of tuples representing this deck; each tuples should include the four attributes in the order given above.

4b. Define a function is_set that, on input three distinct cards from your deck, returns True if and only if they form a "set" according to the rules of the game. That is, in each of the four attributes, they are either all the same (say, all green) or all different (say, one red, one green, one purple), not two of one and one of another (say, two greens and a red). If possible, make this a lambda function.

4c. In the game, one normally looks at twelve cards at a time and attempts to find a set among these. Write a lambda function that, given a list of cards, returns True if and only if it contains a set. Your answer may use the function is_set that you defined in 4b.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

*** Please give proper indentation, as shown in the image ***

#################################################################################################

# defining card, haveing four attribute number,color,shape,texture
class card:
def __init__(self,number,color,shape,texture):
self.number=number
self.color=color
self.shape=shape
self.texture=texture

""" Task 4b """

# defining is_set
def is_set(c1,c2,c3): # three cards are c1, c2 and c3

# getting is_set for number attribute using lambda
ans0=lambda c1,c2,c3:c1.number==c2.number==c3.number or (c1.number!=c2.number and c2.number!=c3.number and c1.number!=c3.number)

# getting is_set for color attribute using lambda
ans1=lambda c1,c2,c3:c1.color==c2.color==c3.color or (c1.color!=c2.color and c2.color!=c3.color and c1.color!=c3.color)

# getting is_set for shape attribute using lambda
ans2=lambda c1,c2,c3:c1.shape==c2.shape==c3.shape or (c1.shape!=c2.shape and c2.shape!=c3.shape and c1.shape!=c3.shape)

# getting is_set for texture attribute using lambda
ans3=lambda c1,c2,c3:c1.texture==c2.texture==c3.texture or (c1.texture!=c2.texture and c2.texture!=c3.texture and c1.texture!=c3.texture)

return bool(ans0) or bool(ans1) or bool(ans2) or bool(ans3) # returning the oring of all is_set of each attribute for required result

""" Task 4c """

# defining check_set for list of 12 cards that tells whether any set is formed or not
def check_set(c):

# getting all the permutation of all the 3 cards from the 12 cards
comb = [(x,y,z) for x in c for y in c for z in c if x != y and y!=z and x!=z]

# removing the repeated , to get the combination
for cc in comb:
if (cc[2],cc[1],cc[0]) in comb:
comb.remove((cc[2],cc[1],cc[0]))
elif (cc[2],cc[0],cc[1]) in comb:
comb.remove((cc[2],cc[0],cc[1]))
elif (cc[1],cc[0],cc[2]) in comb:
comb.remove((cc[1],cc[0],cc[2]))
elif (cc[1],cc[2],cc[0]) in comb:
comb.remove((cc[1],cc[2],cc[0]))
elif (cc[0],cc[2],cc[1]) in comb:
comb.remove((cc[0],cc[2],cc[1]))

ans=False
for cc in comb:
ans=ans or bool(lambda cc:is_set(cc[0],cc[1],cc[2])) # finding set using function defined above and lambda

return ans # returning the answer

# main function
def main():
# initializing aal the attribute tuples for cards
number=('one', 'two', 'three')
color=('red', 'green', 'purple')
shape=('diamond', 'squiggle', 'oval')
texture=('open', 'solid', 'striped')

""" Task 4a """
  
# creating deck
deck=[]
for n in number:
for c in color:
for s in shape:
for t in texture:
deck.append(card(n,c,s,t))

print(check_set(deck[0:12:1])) # printing one test care output

if __name__=='__main__':
main()


#################################################################################################

*** If you have any doubt regarding the solution, please do ask in comments ***

Add a comment
Know the answer?
Add Answer to:
4a. In the card game Set, the deck consists of 81 cards, each with four attributes:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT