Robbie Hatley's Solutions To The Weekly Challenge #291 (Middle Index and Poker)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2024-10-13 through 2024-10-19 is #291.

The tasks for challenge #291 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 291-1: "Middle Index"
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints. Write a script to find
the leftmost middle index (MI) i.e. the smallest amongst all the
possible ones. A "middle index" is an index where
   ints[0]    + ints[1]    + ... + ints[MI-1]
== ints[MI+1] + ints[MI+2] + ... + ints[ints.length-1].
If MI == 0, the left side sum is considered to be 0. Similarly,
if MI == ints.length - 1, the right side sum is considered to be 0.
Return the leftmost MI that satisfies the condition, or -1 if
there is no such index.

Example 1:  Input: @ints = (2, 3, -1, 8, 4)  Output: 3
The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
The sum of the numbers after index 3 is: 4 = 4

Example 2:  Input: @ints = (1, -1, 4)  Output: 2
The sum of the numbers before index 2 is: 1 + -1 = 0
The sum of the numbers after index 2 is: 0

Example 3:  Input: @ints = (2, 5)  Output: -1
There is no valid MI.

I'll start by making a variable "$return" and setting it to -1. Then I'll make an index variable $i, and increment it in a ranged for loop from 0 upwards to $#ints; if, for any index $i, the sum of values to its left is equal to the sum of values to its right, then set $return=$i and exit loop. Then just return $return.

Robbie Hatley's Perl Solution to The Weekly Challenge 291-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 291-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
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.

This problem involves a lot of thinking about how probability works. The solution will be heavily-dependent on the mathematical principle of "combinations", and thus also factorials. One could use CPAN module "Math::Combinatorics" for this, but factorials and combinations are so simple that I just rolled my own. The rest is just a matter of calculating "ways" to get each rank, then adding them together and showing that the sum is equal to 52c5 (the total number of combinations of 52 things taken 5-at-a-time).

Robbie Hatley's Perl Solution to The Weekly Challenge 291-2

That's it for challenge 291; see you on challenge 292!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #276