Posts

Showing posts from October, 2023

Robbie Hatley's Solutions To The Weekly Challenge #241

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 This week (2023-10-29 through 2023-11-04) is weekly challenge #241. Task 241-1 is as follows: PROBLEM DESCRIPTION: Task 1: Arithmetic Triplets Submitted by: Mohammad S Anwar Given an array "nums" of 3-or-more integers in increasing order, and a positive integer "diff", write a script to find the number of unique "Arithmetic Triplets", where an "Arithmetic Triplet" is a trio of numbers from nums which satisfies these rules: a) i < j < k b) nums[j] - nums[i] == diff c) nums[k] - nums[j] == diff Example 1: Input: @nums = (0, 1, 4, 6, 7, 10), $diff = 3 Output: 2 Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3. Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. Example 2: Input: @nums = (4, 5, 6, 7

Robbie Hatley's Solutions To The Weekly Challenge #240

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 This week (2023-10-22 through 2023-10-28) is weekly challenge #240. Task 240-1 is as follows: Task 1: Acronym Submitted by: Mohammad S Anwar You are given an array of strings and a check string. Write a script to find out if the check string is the acronym of the words in the given array. Example 1: Input: @str = ("Perl", "Python", "Pascal"); $chk = "ppp"; Output: true Example 2: Input: @str = ("Perl", "Raku"); $chk = "rp"; Output: false Example 3: Input: @str = ("Oracle", "Awk", "C"); $chk = "oac"; Output: true This is just a matter of making these two subs: sub acronym (@list) {return join('', map {substr $_, 0, 1} @list)} sub ncscomp ($x,$y) {return (fc($x) eq fc($y))}

Robbie Hatley's Solutions To The Weekly Challenge #239

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 This week (2023-10-15 through 2023-10-21) is weekly challenge #239. Task 239-1 is as follows: Task 1: Same String Submitted by: Mohammad S Anwar Given two arrays of strings, write a script to find out if the word created by concatenating the array elements is the same. Example 1: Input: @arr1 = ("ab", "c") @arr2 = ("a", "bc") Output: true Using @arr1, word1 => "ab" . "c" => "abc" Using @arr2, word2 => "a" . "bc" => "abc" Example 2: Input: @arr1 = ("ab", "c") @arr2 = ("ac", "b") Output: false Using @arr1, word1 => "ab" . "c" => "abc" Using @arr2, word2 => "ac" . "b" => "acb&

Robbie Hatley's Solutions To The Weekly Challenge #238

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 This week (2023-10-08 through 2023-10-14) is weekly challenge #238. Task 238-1 is as follows: Task 1: Running Sum Submitted by: Mohammad S Anwar Given an array of integers*, write a script to return the running sum of the array. The running sum can be calculated as: sum[i] = num[0] + num[1] + … + num[i]. *[RH Note: this can be done for ANY kind of addable numbers: integer, real, complex, etc. For for the purpose of this script, I'll assume all numbers are real (non necessarily integers).] Example 1: Input: (1, 2, 3, 4, 5) Output: (1, 3, 6, 10, 15) Example 2: Input: (1, 1, 1, 1, 1) Output: (1, 2, 3, 4, 5) Example 3: Input: (0, -1, 1, 2) Output: (0, -1, 0, 2) This is what's known in mathematics as "the sequence of partial sums of a sequence of numbers", also known as a "seri

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 cus

Robbie Hatley's Solutions To The Weekly Challenge #237

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-10-01 through 2023-10-07) is weekly challenge #237. Task 1 is as follows: Task 1: Seize The Day Submitted by: Mark Anderson Given a year, a month, a weekday of month, and a day of week (1 (Mon) .. 7 (Sun)), print the day. Example 1: Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2 Output: 16 The 3rd Tue of Apr 2024 is the 16th Example 2: Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4 Output: 9 The 2nd Thu of Oct 2025 is the 9th Example 3: Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3 Output: 0 There isn't a 5th Wed in Aug 2026 I am soooooo not going to pull in all of the heavy artillery from my "day-of-week.pl" script; it's just not needed for this. I'll use "use Time::Local 'tim