Posts

Showing posts from January, 2024

Robbie Hatley's Solutions To The Weekly Challenge #254

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 (2024-01-28 through 2024-02-03) is weekly challenge #254. Its tasks are as follows: Task 254-1: Three Power Submitted by: Mohammad S Anwar You are given a positive integer, $n. Write a script to return true if the given integer is a power of three otherwise return false. Example 1: Input: $n = 27 Output: true 27 = 3 ^ 3 Example 2: Input: $n = 0 Output: true 0 = 0 ^ 3 Example 3: Input: $n = 6 Output: false First of all, I had to think hard whether "Three Power" means "x^3" or "3^x". But the latter would be "3 to the x power", whereas the former is "x to the 3 power", so I think "Three Power" means "x^3". This is also corroborated by Example #2, which states that 0 is a "Three Power" because "0 = 0^3", ...

Robbie Hatley's Solutions To The Weekly Challenge #253

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 (2024-01-21 through 2024-01-27) is weekly challenge #253. Its tasks are as follows: Task 253-1: Split Strings Submitted by: Mohammad S Anwar You are given an array of strings and a character separator. Write a script to return all words separated by the given character excluding empty string. Example 1: Input: @words = ("one.two.three","four.five","six") $separator = "." Output: "one","two","three","four","five","six" Example 2: Input: @words = ("$perl$$", "$$raku$") $separator = "$" Output: "perl","raku" This is just a matter of splitting strings based on the given separator, then dumping the empty strings. One complicating factor...

Robbie Hatley's Solutions To The Weekly Challenge #252

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 (2024-01-14 through 2024-01-20) is weekly challenge #252. Its tasks are as follows: Task 252-1: Special Numbers Submitted by: Mohammad S Anwar You are given an array of integers, @ints. Write a script to find the sum of the squares of all "special" elements of the given array. An element $int[i] of @ints is called "special" if i divides n, i.e. n % i == 0, where n is the length of the given array. Also, the array is 1-indexed for the task. Example 1: Input: @ints = (1, 2, 3, 4) Output: 21 There are exactly 3 special elements in the given array: $ints[1] since 1 divides 4 $ints[2] since 2 divides 4 $ints[4] since 4 divides 4 Hence, the sum of the squares of all special elements of the given array is: 1 * 1 + 2 * 2 + 4 * 4 = 21. Example 2: Input: @ints = (2, 7, 1, 19, 18, 3) O...

Robbie Hatley's Solutions To The Weekly Challenge #251

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 (2024-01-07 through 2024-01-13) is weekly challenge #251. Its tasks are as follows: Task 251-1: Concatenation Value Submitted by: Mohammad S Anwar You are given an array of integers, @ints. Write a script to find the concatenation value of the given array. The concatenation of two numbers is the number formed by concatenating their numerals. For example, the concatenation of 10, 21 is 1021. The concatenation value of @ints is initially equal to 0. Perform this operation until @ints becomes empty: If there exists more than one number in @ints, pick the first element and last element in @ints respectively and add the value of their concatenation to the concatenation value of @ints, then delete the first and last element from @ints. If one element exists, add its value to the concatenation value of ...

Robbie Hatley's Solutions To The Weekly Challenge #250

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-12-31 through 2024-01-06) is weekly challenge #250. Its tasks are as follows: Task 250-1: Smallest Index Submitted by: Mohammad S Anwar Given an array of integers @ints, write a script to find the smallest index i such that i mod 10 == $ints[i]; otherwise, return -1. Example 1: Input: @ints = (0, 1, 2) Output: 0 i=0: 0 mod 10 = 0 == $ints[0]. i=1: 1 mod 10 = 1 == $ints[1]. i=2: 2 mod 10 = 2 == $ints[2]. All indices have i mod 10 == $ints[i], so we return the smallest index, 0. Example 2: Input: @ints = (4, 3, 2, 1) Output: 2 i=0: 0 mod 10 = 0 != $ints[0]. i=1: 1 mod 10 = 1 != $ints[1]. i=2: 2 mod 10 = 2 == $ints[2]. i=3: 3 mod 10 = 3 != $ints[3]. 2 is the only index which has i mod 10 == $ints[i]. Example 3: Input: @ints = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) Output: -1 No index satisfies i mo...