aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-10-20 23:39:49 +0100
committerGitHub <noreply@github.com>2024-10-20 23:39:49 +0100
commit5fd3689582eb170d5c7fcd9842dd9770465dea4a (patch)
treea19bf18a0306c38caa780013217f6ed22ce5b30e
parent767802c54302283277458496f81876eafd83d8c3 (diff)
parentde2c3a028161cefaf720a63980dfd79a37cb19bc (diff)
downloadperlweeklychallenge-club-5fd3689582eb170d5c7fcd9842dd9770465dea4a.tar.gz
perlweeklychallenge-club-5fd3689582eb170d5c7fcd9842dd9770465dea4a.tar.bz2
perlweeklychallenge-club-5fd3689582eb170d5c7fcd9842dd9770465dea4a.zip
Merge pull request #11051 from pauloscustodio/master
Add solution to challenge 291 task 2
-rw-r--r--challenge-291/paulo-custodio/perl/ch-2.pl73
-rw-r--r--challenge-291/paulo-custodio/python/ch-2.py72
-rw-r--r--challenge-291/paulo-custodio/t/test-2.yaml16
3 files changed, 161 insertions, 0 deletions
diff --git a/challenge-291/paulo-custodio/perl/ch-2.pl b/challenge-291/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..e629c89383
--- /dev/null
+++ b/challenge-291/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+
+# Challenge 291
+#
+# Task 2: Poker Hand Rankings
+# Submitted by: Robbie Hatley
+# A draw poker hand consists of 5 cards, drawn from a pack of 52: no jokers,
+# no wild cards. An ace can rank either high or low.
+#
+# Write a script to determine the following three things:
+#
+# 1. How many different 5-card hands can be dealt?
+# 2. How many different hands of each of the 10 ranks can be dealt?
+# See here for descriptions of the 10 ranks of Poker hands:
+# https://en.wikipedia.org/wiki/List_of_poker_hands#Hand-ranking_categories
+# 3. Check the ten numbers you get in step 2 by adding them together
+# and showing that they're equal to the number you get in step 1.
+
+# see https://brilliant.org/wiki/math-of-poker/
+
+use Modern::Perl;
+use ntheory 'binomial';
+use List::Util 'sum';
+
+# 1. How many different 5-card hands can be dealt?
+my $N = binomial(52, 5);
+say "N=",$N;
+
+# 2. How many different hands of each of the 10 ranks can be dealt?
+# high card
+my @n;
+$n[0] = (binomial(13,5)-10)*(binomial(4,1)**5-4);
+say "n0=",$n[0];
+
+# one pair hand
+$n[1] = binomial(13,1)*binomial(4,2)*binomial(12,3)*binomial(4,1)**3;
+say "n1=",$n[1];
+
+# two pair hand
+$n[2] = binomial(13,2)*binomial(4,2)**2*binomial(11,1)*binomial(4,1);
+say "n2=",$n[2];
+
+# three of a kind
+$n[3] = binomial(13,1)*binomial(4,3)*binomial(12,2)*binomial(4,1)**2;
+say "n3=",$n[3];
+
+# straight hand
+$n[4] = binomial(10,1)*(binomial(4,1)**5-4);
+say "n4=",$n[4];
+
+# flush hand
+$n[5] = (binomial(13,5)-10)*binomial(4,1);
+say "n5=",$n[5];
+
+# full house hand
+$n[6] = binomial(13,1)*binomial(4,3)*binomial(12,1)*binomial(4,2);
+say "n6=",$n[6];
+
+# four of a kind hand
+$n[7] = binomial(13,1)*binomial(4,4)*binomial(12,1)*binomial(4,1);
+say "n7=",$n[7];
+
+# straight flush hand
+$n[8] = binomial(10,1)*binomial(4,1)-4;
+say "n8=",$n[8];
+
+# royal flush hand
+$n[9] = 4;
+say "n9=",$n[9];
+
+# 3. Check the ten numbers you get in step 2 by adding them together
+# and showing that they're equal to the number you get in step 1.
+$N == sum(@n) or die;
diff --git a/challenge-291/paulo-custodio/python/ch-2.py b/challenge-291/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..1fac2ecdfa
--- /dev/null
+++ b/challenge-291/paulo-custodio/python/ch-2.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+
+# Challenge 291
+#
+# Task 2: Poker Hand Rankings
+# Submitted by: Robbie Hatley
+# A draw poker hand consists of 5 cards, drawn from a pack of 52: no jokers,
+# no wild cards. An ace can rank either high or low.
+#
+# Write a script to determine the following three things:
+#
+# 1. How many different 5-card hands can be dealt?
+# 2. How many different hands of each of the 10 ranks can be dealt?
+# See here for descriptions of the 10 ranks of Poker hands:
+# https://en.wikipedia.org/wiki/List_of_poker_hands#Hand-ranking_categories
+# 3. Check the ten numbers you get in step 2 by adding them together
+# and showing that they're equal to the number you get in step 1.
+
+# see https://brilliant.org/wiki/math-of-poker/
+
+from math import comb
+from functools import reduce
+
+# 1. How many different 5-card hands can be dealt?
+N = comb(52, 5)
+print("N=", N)
+
+# 2. How many different hands of each of the 10 ranks can be dealt?
+# high card
+n = [0] * 10
+n[0] = (comb(13, 5) - 10) * (comb(4, 1) ** 5 - 4)
+print("n0=", n[0])
+
+# one pair hand
+n[1] = comb(13, 1) * comb(4, 2) * comb(12, 3) * (comb(4, 1) ** 3)
+print("n1=", n[1])
+
+# two pair hand
+n[2] = comb(13, 2) * (comb(4, 2) ** 2) * comb(11, 1) * comb(4, 1)
+print("n2=", n[2])
+
+# three of a kind
+n[3] = comb(13, 1) * comb(4, 3) * comb(12, 2) * (comb(4, 1) ** 2)
+print("n3=", n[3])
+
+# straight hand
+n[4] = comb(10, 1) * (comb(4, 1) ** 5 - 4)
+print("n4=", n[4])
+
+# flush hand
+n[5] = (comb(13, 5) - 10) * comb(4, 1)
+print("n5=", n[5])
+
+# full house hand
+n[6] = comb(13, 1) * comb(4, 3) * comb(12, 1) * comb(4, 2)
+print("n6=", n[6])
+
+# four of a kind hand
+n[7] = comb(13, 1) * comb(4, 4) * comb(12, 1) * comb(4, 1)
+print("n7=", n[7])
+
+# straight flush hand
+n[8] = comb(10, 1) * comb(4, 1) - 4
+print("n8=", n[8])
+
+# royal flush hand
+n[9] = 4
+print("n9=", n[9])
+
+# 3. Check the ten numbers you get in step 2 by adding them together
+# and showing that they're equal to the number you get in step 1.
+assert N == sum(n)
diff --git a/challenge-291/paulo-custodio/t/test-2.yaml b/challenge-291/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..cc1f25d089
--- /dev/null
+++ b/challenge-291/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,16 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ |N=2598960
+ |n0=1302540
+ |n1=1098240
+ |n2=123552
+ |n3=54912
+ |n4=10200
+ |n5=5108
+ |n6=3744
+ |n7=624
+ |n8=36
+ |n9=4