1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
C = ["♣", "♦", "♥", "♠"] P = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] # Combine C and P to a card pack of ordered cards CP = tuple(c + p for p in P for c in C) # Look up the rank order for 'heart-7' and 'heart-6' M1 = CP.index("♥7") M2 = CP.index("♥6") for g in range(52): for m in range(g): # The three probabilities in question are straight away P = (51 - g)/50 * (51 - g - 1)/49 + m/50 * (m - 1)/49 P2 = (51 - g)/50 * (51 - g - 1)/49 + 22/50 * (M1 - 1)/49 P3 = (51 - g)/50 * (51 - g - 1)/49 + 18/50 * (M2 - 1)/49 # Remains to check if the conditions are met if P2 == P/2 and P3 == P/3: print(f"The cards were {CP[g]} and {CP[m]}.") |

I wasn’t aware that the editors involved did handle the graphical symbols for the suits as ordinary characters so, I updated that too. (Stole it from John.)

]]>
1 2 3 |
CP = tuple(c + p for p in P for c in C) |

and remove lines 7 to 11.

]]>The reason I added that I thought it could be done in a better way, was that I tried to get away with a list rather than a dictionary, like John has done below. But I found that I could not rely on the list’s ordering; it did not come out the same way it was built..

With the dictionary in place, however, it would have been more consistent to use it also in the first conversion, from card-faces to rank-order:

1 2 3 4 5 |
# Look up the rank order for 'heart-7' and 'heart-6' M1 = list(CP.values()).index("\u2665" + "7") M2 = list(CP.values()).index("\u2665" + "6") |

..and replace ‘22’ and ‘18’ in line 17 and 18 with M1 and M2. (I am not asking for that change to be done. It’s just for the record.)

]]>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
cards = [j + ' ' + i for i in 'AKQJT98765432' for j in '♠♥♦♣'] h7 = cards.index('♥ 7') h6 = cards.index('♥ 6') lc7 = 51 - h7 # number of cards below 7 of hearts lc6 = 51 - h6 # number of cards below 6 of hearts # Consider card picked by George for g in range(h7): # Compare positive outcomes for visitors for h7 and h6 if (g * (g - 1) + lc7 * (lc7 - 1)) * 2 == (g * (g - 1) + lc6 * (lc6 - 1)) * 3: print(f"George's card: {cards[g]}") break # Consider card picked my Martha for m in range(g + 1, h7): # number of cards below Martha's lcm = 51 - m # Compare positive outcomes for visitors for Martha's card and h7 if (g * (g - 1) + lc7 * (lc7 - 1)) * 2 == (g * (g - 1) + lcm * (lcm - 1)): print(f"Martha's card: {cards[m]}") |

manual solution for this reason). ]]>

I like your effort on the output.

]]>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
P = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] C = ["\u2663", "\u2666", "\u2665", "\u2660"] CP = dict() # Virtual card-pack in rank order k = -1 for i in range(13): for j in range(4): k += 1 CP[k] = C[j] + P[i] for g in range(52): for m in range(g): # The three probabilities in question are straight away P = (51 - g)/50 * (51 - g - 1)/49 + m/50 * (m - 1)/49 P2 = (51 - g)/50 * (51 - g - 1)/49 + 22/50 * (22 - 1)/49 P3 = (51 - g)/50 * (51 - g - 1)/49 + 18/50 * (18 - 1)/49 # Remains to check if the conditions are met if P2 == P/2 and P3 == P/3: print(f"The cards were {CP[g]} and {CP[m]}.") |