Robbie Hatley's Solutions To The Weekly Challenge #222

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-06-18 through 2023-06-24) is weekly challenge #222.

Task 1 is as follows:

"You are given a list of positive integers, @ints. Write a script to find the total matching members after sorting the list increasing order."

The word "matching" is a tad vague in that context, but from the first example we can see that it means "the two arrays have the same element at the same index":

Example 1:
Original list: (1, 1, 4, 2, 1, 2)
Sorted list  : (1, 1, 1, 2, 3, 4)
Comparing the two lists, we found 3 matching members:
Index 0, Value 1
Index 1, Value 1
Index 3, Value 2

My solution is to compare the sorted and unsorted versions of each array using a 3-part loop and counting matching elements, like so:

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

Task 2 is as follows:

"You are given an array of positive integers, @ints. Write a script to find the last member if found, otherwise return 0. Each turn pick 2 biggest members (x, y), then decide based on the following conditions and continue this until you are left with 1 member or none:
      a) if x == y then remove both members
      b) if x != y then remove both members and add new member (y-x)"

This is very straightforward, just a matter of repeatedly sorting, popping, and unshifting until either zero or one elements are left:

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

That's it for 222; see you on 223!

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 #273