#!/usr/bin/perl -w use strict; use warnings; sub fifteens { my $hand = shift; $hand =~ s/[SCHD]//g; $hand =~ s/[JQK]/10/g; $hand =~ s/A/1/g; my ( $c1, $c2, $c3, $c4, $c5 ) = split /,/, $hand; my $total = 0; # count the 2 card combinations that add up to 15 $total += 2 if ( ( $c1 + $c2 ) == 15 ); $total += 2 if ( ( $c1 + $c3 ) == 15 ); $total += 2 if ( ( $c1 + $c4 ) == 15 ); $total += 2 if ( ( $c1 + $c5 ) == 15 ); $total += 2 if ( ( $c2 + $c3 ) == 15 ); $total += 2 if ( ( $c2 + $c4 ) == 15 ); $total += 2 if ( ( $c2 + $c5 ) == 15 ); $total += 2 if ( ( $c3 + $c4 ) == 15 ); $total += 2 if ( ( $c3 + $c5 ) == 15 ); $total += 2 if ( ( $c4 + $c5 ) == 15 ); # count the 3 card combinations that add up to 15 $total += 2 if ( $c1 + $c2 + $c3 == 15 ); $total += 2 if ( $c1 + $c2 + $c4 == 15 ); $total += 2 if ( $c1 + $c2 + $c5 == 15 ); $total += 2 if ( $c1 + $c3 + $c4 == 15 ); $total += 2 if ( $c1 + $c3 + $c5 == 15 ); $total += 2 if ( $c1 + $c4 + $c5 == 15 ); $total += 2 if ( $c2 + $c3 + $c4 == 15 ); $total += 2 if ( $c2 + $c3 + $c5 == 15 ); $total += 2 if ( $c2 + $c4 + $c5 == 15 ); $total += 2 if ( $c3 + $c4 + $c5 == 15 ); # count the 4 card combinations that add up to 15 $total += 2 if ( $c1 + $c2 + $c3 + $c4 == 15 ); $total += 2 if ( $c1 + $c2 + $c3 + $c5 == 15 ); $total += 2 if ( $c1 + $c2 + $c4 + $c5 == 15 ); $total += 2 if ( $c1 + $c3 + $c4 + $c5 == 15 ); $total += 2 if ( $c2 + $c3 + $c4 + $c5 == 15 ); # See if all the cards add up to 15 $total += 2 if ( $c1 + $c2 + $c3 + $c4 + $c5 == 15 ); return $total; } ## end sub fifteens print "hand = A,2,3,4,5 \tTotal =", fifteens ( "A,2,3,4,5"), "\n"; print "hand = 5,5,5,J,5 \tTotal =", fifteens ( "5,5,5,J,5"), "\n"; print "hand = 6,7,8,9,5 \tTotal =", fifteens ( "6,7,8,9,5"), "\n";