Robbie Hatley's Solutions To The Weekly Challenge #236

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

The Weekly Challenge

This week (2023-09-24 through 2023-09-30) is weekly challenge #236.

Task 1 is as follows:

Task 1: Exact Change
Submitted by: Mohammad S Anwar
You are asked to sell juice. Each costs $5. You are presented with
an array containing only $5 and/or $10 and/or $20 bills. Each bill
is owned by one customer, and the customers are to be served in
left-to-right order. You are allowed to sell only one juice to
each customer, and only if you can provide exact change. You do
not have any cash in your till to begin with. Write a script to
find out if it is possible to give exact change to all customers.

Example 1:
Input: @bills = (5, 5, 5, 10, 20)
Output: true
From the first 3 customers, we collect three $5 bills in order.
From the fourth customer, we collect a $10 bill and give back a $5.
From the fifth customer, we give a $10 bill and a $5 bill.
Since all customers got correct change, we output true.

Example 2:
Input: @bills = (5, 5, 10, 10, 20)
Output: false
From the first two customers in order, we collect two $5 bills.
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
For the last customer, we can not give the change of $15 back because we only have two $10 bills.
Since not every customer received the correct change, the answer is false.

Example 3:
Input: @bills = (5, 5, 5, 20)
Output: true

Since ability to give change is determined by what's in the till, I'll start by making an array "@arrays" to hold multiple input "@bills" arrays, then for each "@bills" array I'll make a "@till" array that starts-off empty. For each "$bill" in "@bills", I'll push "$bill" to "@till", set "$change" to "$bill-5", and see if I can get "$change" down to 0 by subtracting bills from till. If I can get "$change" down to 0 for all customers I'll return "true" else "false".

The script I came up with was this:

Robbie Hatley's Solution to The Weekly Challenge 236-1

Task 2 is as follows:

Task 2: Array Loops
Submitted by: Mark Anderson
You are given an array of unique integers. Write a script to
determine how many loops are in the given array. To determine
a loop: Start at an index and take the number at array[index]
and then proceed to that index and continue this until you end up
at the starting index.

Example 1:
Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10)
Output: 3
To determine the 1st loop, start at index 0, the number at that
index is 4, proceed to index 4, the number at that index is 15,
proceed to index 15 and so on until you're back at index 0.
Loops are as below:
[4 15 1 6 13 5 0]
[3 8 7 18 9 16 12 17 2]
[14 11 19 10]

Example 2:
Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19)
Output: 6
Loops are as below:
[0]
[1]
[13 9 14 17 18 15 5 8 2]
[7 11 4 6 10 16 3]
[12]
[19]

Example 3:
Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17)
Output: 1
Loop is as below:
[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0]

Now this I found mathematically interesting. My first attempt at this failed, giving 20 loops for every array, because I was counting each loop many times (once per element) instead of just once. But I eventually hit upon the technique of marking each element as "visited" and abandoning all pathways which re-traced old ground, only keeping loops which tread only on fresh ground. After adding that approach, I finally got my script to work right:

Robbie Hatley's Solution to The Weekly Challenge 236-2

That's it for 236; see you on 237!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #262