# Update the code below to solve this problem t = int(input()) for i in range(t): N, A, B = map(int, input().split()) X=list(map(int,input().split(" "))) s=6-N print("%.8f" %(1/s))
Please explain your issue.
It’s not necessary that N will always be 5 or less than 5 (N<=5) hence it will cause issues (such as resulting in negative probability which is incorrect).
Below the number of faces in dice having A and B have been counted using variables A_count and B_count respectively. Using these variables one can find the probability of landing A and B as:
Probability of Landing A=(A_count)/(Total number of faces on the dice)
Probability of Landing B=(B_count)/(Total number of faces on the dice)
Using these probabilities one can find the probability of landing A on the first and B on the second toss which is: (Probability of Landing A)*(Probability of Landing B)
My Code: (Explained using comments)
t = int(input())
for i in range(t):
N, A, B = map(int, input().split())
Dice_Numbers=[int(x) for x in input().split(" ")]
''' Two variables: A_count and B_count Count the number of A and B on dice These will be used to find the probability Probability of landing A= (Number of A faces)/(Total number of faces) Probability of landing B= (Number of B faces)/(Total number of faces) Probability of Landing A on first toss and B on second toss= (Probability of landing A)*(Probability of landing B) ''' A_count=Dice_Numbers.count(A) B_count=Dice_Numbers.count(B) Probability_of_landing_A=(A_count)/N Probability_of_landing_B=(B_count)/N Probability_AB=Probability_of_landing_A*Probability_of_landing_B print("%.8f"%Probability_AB)