Problem Statement
You have several cards, each with a number written on them, given in
If you select a card at random, what is the probability that what is written on the card matches the target value?
Definition
- Class:
- OneCardDraw
- Method:
- pickChance
- Parameters:
- int[], int
- Returns:
- double
- Method signature:
- double pickChance(int[] cards, int target)
- (be sure your method is public)
Constraints
- cards will contain between 1 and 50 elements, inclusive.
- Each element of cards will be between 1 and 10, inclusive.
- target will be between 1 and 10, inclusive.
Examples
{ 1, 2, 3 }
1
Returns: 0.3333333333333333
There's 1 card that matches, out of 3 total, giving a 1/3 probability.
{ 1, 1, 2, 3 }
1
Returns: 0.5
Two cards out of 4 match.
{ 1, 2, 3, 4, 5, 6, 7, 8 }
9
Returns: 0.0
Since no cards match, there's no chance at all.
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
4
Returns: 0.1
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
5
Returns: 0.1