问题
Background
This is a followup question to my previous finding a straight in a cribbage hand question
Objective
Count the number of pairs, then score 2 points for each pair.
What I have Tried
I currently have two different working methods for counting pairs. I was trying to find something potentially better and trying to work with manual arrays (hope that is the right term) ie. {2,3,4}
Working formulas are:
=(SUM(--(AGGREGATE(15,6,MOD(B1:F1-1,13)+1,1)=AGGREGATE(15,6,MOD(B1:F1-1,13)+1,{2,3,4,5})))
+SUM(--(AGGREGATE(15,6,MOD(B1:F1-1,13)+1,2)=AGGREGATE(15,6,MOD(B1:F1-1,13)+1,{3,4,5})))
+SUM(--(AGGREGATE(15,6,MOD(B1:F1-1,13)+1,3)=AGGREGATE(15,6,MOD(B1:F1-1,13)+1,{4,5})))
+(AGGREGATE(15,6,MOD(B1:F1-1,13)+1,4)=AGGREGATE(15,6,MOD(B1:F1-1,13)+1,5)))*2
and
=((MOD(B1-1,13)+1=MOD(C1-1,13)+1)
+(MOD(B1-1,13)+1=MOD(D1-1,13)+1)
+(MOD(B1-1,13)+1=MOD(E1-1,13)+1)
+(MOD(B1-1,13)+1=MOD(F1-1,13)+1)
+(MOD(C1-1,13)+1=MOD(D1-1,13)+1)
+(MOD(C1-1,13)+1=MOD(E1-1,13)+1)
+(MOD(C1-1,13)+1=MOD(F1-1,13)+1)
+(MOD(D1-1,13)+1=MOD(E1-1,13)+1)
+(MOD(D1-1,13)+1=MOD(F1-1,13)+1)
+(MOD(E1-1,13)+1=MOD(F1-1,13)+1))*2
I liked the ability to use {2,3,4,5} to help build all the possible combinations. However AGGREGATE is performing a sort step that I do not actually need. I was trying to tinker with INDEX to bypass the sort step and I thought it was one of those functions that performs array operations without being an array like AGGREGATE or SUMPRODUCT. However I could not seem to get it to work. This is what I was trying:
=SUM(--((MOD(INDEX(B1:F1,1)-1,13)+1)=(MOD(INDEX(B1:F1,{2,3,4,5})-1,13)+1)))
=SUM(--((MOD(INDEX(B1:F1,1,1)-1,13)+1)=(MOD(INDEX(B1:F1,1,{2,3,4,5})-1,13)+1)))
Note this is just a proof of concept for checking the first card against the rest of the hand and the turn card. I did not see a point of building out the rest of the formula if I could not get the first part to work. I tried it as both a 1D and 2D reference in case it made a difference...it did not! I also tried it with SUMPRODUCT instead of just SUM and I also tried CSE entry.
Question
Is my thought on using INDEX wrong? Is there a better way with formulas to check for all the potential pairings?
Sample Data
| B | C | D | E | F | POINTS
+----+----+----+----+----+
| 1 | 14 | 27 | 40 | 2 | <= 12 (4 of a kind, 6 pairs)
| 1 | 2 | 27 | 28 | 14 | <= 8 (1 triple and 1 double aka full house in poker, 4 pairs)
| 1 | 14 | 27 | 28 | 52 | <= 6 (1 triple, 3 pairs)
| 1 | 2 | 27 | 28 | 52 | <= 4 (2 pairs)
| 1 | 2 | 14 | 51 | 52 | <= 2 (1 pair)
| 1 | 2 | 3 | 4 | 52 | <= 0 (no pair)
Excel Version
Excel 2013
回答1:
I thought of this which seemed a good idea last thing at night. Will have to see if it's still any good in the morning:
SUM(--(A1:E1=TRANSPOSE(A1:E1)))-5
entered as an array formula. This deliberately counts each pair twice, to get two points for each pair, but then you also get five because there is always a match on the diagonal of the resulting matrix so that needs to be subtracted.
EDIT
Here you go
=SUM(--(MOD(A1:E1-1,13)=MOD(TRANSPOSE(A1:E1)-1,13)))-5
来源:https://stackoverflow.com/questions/62903517/counting-pairs-in-cribbage-hand