aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-21 14:19:11 +0000
committerGitHub <noreply@github.com>2023-11-21 14:19:11 +0000
commit4861800a599b9fc0aee3ba98c9880beb68da2999 (patch)
tree6507541e610e0c9f6b779abaaf9ac16730480c34
parenta1c03c88e8edae1d2763b41d743edf3893c834d9 (diff)
parent00bd4c4d5baa397b9211a7f5d40bac6581fa5e3e (diff)
downloadperlweeklychallenge-club-4861800a599b9fc0aee3ba98c9880beb68da2999.tar.gz
perlweeklychallenge-club-4861800a599b9fc0aee3ba98c9880beb68da2999.tar.bz2
perlweeklychallenge-club-4861800a599b9fc0aee3ba98c9880beb68da2999.zip
Merge pull request #9115 from pauloscustodio/master
Add Perl, C and C++ solutions
-rw-r--r--challenge-244/paulo-custodio/Makefile2
-rw-r--r--challenge-244/paulo-custodio/c/ch-1.c76
-rw-r--r--challenge-244/paulo-custodio/c/ch-2.c167
-rw-r--r--challenge-244/paulo-custodio/c/utarray.h252
-rw-r--r--challenge-244/paulo-custodio/cpp/ch-1.cpp66
-rw-r--r--challenge-244/paulo-custodio/cpp/ch-2.cpp150
-rw-r--r--challenge-244/paulo-custodio/perl/ch-1.pl47
-rw-r--r--challenge-244/paulo-custodio/perl/ch-2.pl53
-rw-r--r--challenge-244/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-244/paulo-custodio/t/test-2.yaml5
10 files changed, 833 insertions, 0 deletions
diff --git a/challenge-244/paulo-custodio/Makefile b/challenge-244/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-244/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-244/paulo-custodio/c/ch-1.c b/challenge-244/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..6430533ebe
--- /dev/null
+++ b/challenge-244/paulo-custodio/c/ch-1.c
@@ -0,0 +1,76 @@
+/*
+Challenge 244
+
+Task 1: Count Smaller
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to calculate the number of integers smaller than the integer
+at each index.
+Example 1
+
+Input: @int = (8, 1, 2, 2, 3)
+Output: (4, 0, 1, 1, 3)
+
+For index = 0, count of elements less 8 is 4.
+For index = 1, count of elements less 1 is 0.
+For index = 2, count of elements less 2 is 1.
+For index = 3, count of elements less 2 is 1.
+For index = 4, count of elements less 3 is 3.
+
+Example 2
+
+Input: @int = (6, 5, 4, 8)
+Output: (2, 1, 0, 3)
+
+Example 3
+
+Input: @int = (2, 2, 2)
+Output: (0, 0, 0)
+*/
+
+#include "utarray.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+UT_array* calc_smaller(UT_array* nums) {
+ UT_array* smaller;
+ utarray_new(smaller, &ut_int_icd);
+
+ for (size_t i = 0; i < utarray_len(nums); i++) {
+ int count = 0;
+ for (size_t j = 0; j < utarray_len(nums); j++) {
+ if (*(int*)utarray_eltptr(nums, j) < *(int*)utarray_eltptr(nums, i))
+ count++;
+ }
+ utarray_push_back(smaller, &count);
+ }
+ return smaller;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ fputs("Usage: ch-1 n n n ...\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ UT_array* nums;
+ utarray_new(nums, &ut_int_icd);
+
+ for (int i = 1; i < argc; i++) {
+ int n = atoi(argv[i]);
+ utarray_push_back(nums, &n);
+ }
+
+ UT_array* smaller = calc_smaller(nums);
+
+ for (size_t i = 0; i < utarray_len(smaller); i++) {
+ if (i != 0) printf(" ");
+ printf("%d", *(int*)utarray_eltptr(smaller, i));
+ }
+ printf("\n");
+
+ utarray_free(nums);
+ utarray_free(smaller);
+}
diff --git a/challenge-244/paulo-custodio/c/ch-2.c b/challenge-244/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..e820892487
--- /dev/null
+++ b/challenge-244/paulo-custodio/c/ch-2.c
@@ -0,0 +1,167 @@
+/*
+Challenge 244
+
+Task 2: Group Hero
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers representing the strength.
+
+Write a script to return the sum of the powers of all possible combinations;
+power is defined as the square of the largest number in a sequence, multiplied
+by the smallest.
+
+Example 1
+
+Input: @nums = (2, 1, 4)
+Output: 141
+
+Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8
+Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1
+Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64
+Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4
+Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32
+Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16
+Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16
+
+Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141
+*/
+
+#include "utarray.h"
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int calc_min(UT_array* nums) {
+ assert(utarray_len(nums) > 0);
+ int n = *(int*)utarray_eltptr(nums, 0);
+ for (size_t i = 1; i < utarray_len(nums); i++) {
+ int m = *(int*)utarray_eltptr(nums, i);
+ if (m < n)
+ n = m;
+ }
+ return n;
+}
+
+int calc_max(UT_array* nums) {
+ assert(utarray_len(nums) > 0);
+ int n = *(int*)utarray_eltptr(nums, 0);
+ for (size_t i = 1; i < utarray_len(nums); i++) {
+ int m = *(int*)utarray_eltptr(nums, i);
+ if (m > n)
+ n = m;
+ }
+ return n;
+}
+
+int calc_power(UT_array* nums) {
+ int mn = calc_min(nums);
+ int mx = calc_max(nums);
+ return mx * mx * mn;
+}
+
+UT_array* counters_new(int k) {
+ UT_array* counters_arr;
+ utarray_new(counters_arr, &ut_int_icd);
+
+ for (int i = 0; i < k; i++) {
+ int n = 0;
+ utarray_push_back(counters_arr, &n);
+ }
+ return counters_arr;
+}
+
+void counters_free(UT_array* counters_arr) {
+ utarray_free(counters_arr);
+}
+
+int* counters_ptr(UT_array* counters_arr) {
+ return (int*)utarray_eltptr(counters_arr, 0);
+}
+
+// return false when wrapping arround
+bool counters_inc(UT_array* counters_arr, int max) {
+ int* counters = counters_ptr(counters_arr);
+ int i = (int)utarray_len(counters_arr) - 1;
+ while (i >= 0) {
+ counters[i]++;
+ if (counters[i] < max)
+ return true;
+ else {
+ counters[i] = 0;
+ i--;
+ }
+ }
+ return false;
+}
+
+// return true if counters are all different and increasing
+bool counters_unique(UT_array* counters_arr) {
+ int* counters = counters_ptr(counters_arr);
+ for (int i = 1; i < (int)utarray_len(counters_arr); i++) {
+ if (counters[i] <= counters[i - 1])
+ return false;
+ }
+ return true;
+}
+
+UT_array* counters_select(UT_array* counters_arr, UT_array* nums_arr) {
+ UT_array* out;
+ utarray_new(out, &ut_int_icd);
+
+ int* counters = (int*)utarray_eltptr(counters_arr, 0);
+ int* nums = (int*)utarray_eltptr(nums_arr, 0);
+
+ for (int i = 0; i < (int)utarray_len(counters_arr); i++) {
+ int n = nums[counters[i]];
+ utarray_push_back(out, &n);
+ }
+
+ return out;
+}
+
+int compute_power_k(int k, UT_array* nums) {
+ UT_array* counters_arr = counters_new(k);
+ int* counters = counters_ptr(counters_arr);
+
+ int sum = 0;
+ do {
+ if (counters_unique(counters_arr)) {
+ UT_array* combo = counters_select(counters_arr, nums);
+ sum += calc_power(combo);
+ utarray_free(combo);
+ }
+ } while (counters_inc(counters_arr, (int)utarray_len(nums)));
+
+ counters_free(counters_arr);
+
+ return sum;
+}
+
+int compute_power(UT_array* nums) {
+ int sum = 0;
+ for (int k = 1; k <= (int)utarray_len(nums); k++)
+ sum += compute_power_k(k, nums);
+ return sum;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ fputs("Usage: ch-1 n n n ...\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ UT_array* nums;
+ utarray_new(nums, &ut_int_icd);
+
+ for (int i = 1; i < argc; i++) {
+ int n = atoi(argv[i]);
+ utarray_push_back(nums, &n);
+ }
+
+ int sum = compute_power(nums);
+
+ printf("%d\n", sum);
+
+ utarray_free(nums);
+}
diff --git a/challenge-244/paulo-custodio/c/utarray.h b/challenge-244/paulo-custodio/c/utarray.h
new file mode 100644
index 0000000000..1fe8bc1c74
--- /dev/null
+++ b/challenge-244/paulo-custodio/c/utarray.h
@@ -0,0 +1,252 @@
+/*
+Copyright (c) 2008-2022, Troy D. Hanson https://troydhanson.github.io/uthash/
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/* a dynamic array implementation using macros
+ */
+#ifndef UTARRAY_H
+#define UTARRAY_H
+
+#define UTARRAY_VERSION 2.3.0
+
+#include <stddef.h> /* size_t */
+#include <string.h> /* memset, etc */
+#include <stdlib.h> /* exit */
+
+#ifdef __GNUC__
+#define UTARRAY_UNUSED __attribute__((__unused__))
+#else
+#define UTARRAY_UNUSED
+#endif
+
+#ifndef utarray_oom
+#define utarray_oom() exit(-1)
+#endif
+
+typedef void (ctor_f)(void *dst, const void *src);
+typedef void (dtor_f)(void *elt);
+typedef void (init_f)(void *elt);
+typedef struct {
+ size_t sz;
+ init_f *init;
+ ctor_f *copy;
+ dtor_f *dtor;
+} UT_icd;
+
+typedef struct {
+ unsigned i,n;/* i: index of next available slot, n: num slots */
+ UT_icd icd; /* initializer, copy and destructor functions */
+ char *d; /* n slots of size icd->sz*/
+} UT_array;
+
+#define utarray_init(a,_icd) do { \
+ memset(a,0,sizeof(UT_array)); \
+ (a)->icd = *(_icd); \
+} while(0)
+
+#define utarray_done(a) do { \
+ if ((a)->n) { \
+ if ((a)->icd.dtor) { \
+ unsigned _ut_i; \
+ for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \
+ (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \
+ } \
+ } \
+ free((a)->d); \
+ } \
+ (a)->n=0; \
+} while(0)
+
+#define utarray_new(a,_icd) do { \
+ (a) = (UT_array*)malloc(sizeof(UT_array)); \
+ if ((a) == NULL) { \
+ utarray_oom(); \
+ } \
+ utarray_init(a,_icd); \
+} while(0)
+
+#define utarray_free(a) do { \
+ utarray_done(a); \
+ free(a); \
+} while(0)
+
+#define utarray_reserve(a,by) do { \
+ if (((a)->i+(by)) > (a)->n) { \
+ char *utarray_tmp; \
+ while (((a)->i+(by)) > (a)->n) { (a)->n = ((a)->n ? (2*(a)->n) : 8); } \
+ utarray_tmp=(char*)realloc((a)->d, (a)->n*(a)->icd.sz); \
+ if (utarray_tmp == NULL) { \
+ utarray_oom(); \
+ } \
+ (a)->d=utarray_tmp; \
+ } \
+} while(0)
+
+#define utarray_push_back(a,p) do { \
+ utarray_reserve(a,1); \
+ if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,(a)->i++), p); } \
+ else { memcpy(_utarray_eltptr(a,(a)->i++), p, (a)->icd.sz); }; \
+} while(0)
+
+#define utarray_pop_back(a) do { \
+ if ((a)->icd.dtor) { (a)->icd.dtor( _utarray_eltptr(a,--((a)->i))); } \
+ else { (a)->i--; } \
+} while(0)
+
+#define utarray_extend_back(a) do { \
+ utarray_reserve(a,1); \
+ if ((a)->icd.init) { (a)->icd.init(_utarray_eltptr(a,(a)->i)); } \
+ else { memset(_utarray_eltptr(a,(a)->i),0,(a)->icd.sz); } \
+ (a)->i++; \
+} while(0)
+
+#define utarray_len(a) ((a)->i)
+
+#define utarray_eltptr(a,j) (((j) < (a)->i) ? _utarray_eltptr(a,j) : NULL)
+#define _utarray_eltptr(a,j) ((void*)((a)->d + ((a)->icd.sz * (j))))
+
+#define utarray_insert(a,p,j) do { \
+ if ((j) > (a)->i) utarray_resize(a,j); \
+ utarray_reserve(a,1); \
+ if ((j) < (a)->i) { \
+ memmove( _utarray_eltptr(a,(j)+1), _utarray_eltptr(a,j), \
+ ((a)->i - (j))*((a)->icd.sz)); \
+ } \
+ if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,j), p); } \
+ else { memcpy(_utarray_eltptr(a,j), p, (a)->icd.sz); }; \
+ (a)->i++; \
+} while(0)
+
+#define utarray_inserta(a,w,j) do { \
+ if (utarray_len(w) == 0) break; \
+ if ((j) > (a)->i) utarray_resize(a,j); \
+ utarray_reserve(a,utarray_len(w)); \
+ if ((j) < (a)->i) { \
+ memmove(_utarray_eltptr(a,(j)+utarray_len(w)), \
+ _utarray_eltptr(a,j), \
+ ((a)->i - (j))*((a)->icd.sz)); \
+ } \
+ if ((a)->icd.copy) { \
+ unsigned _ut_i; \
+ for(_ut_i=0;_ut_i<(w)->i;_ut_i++) { \
+ (a)->icd.copy(_utarray_eltptr(a, (j) + _ut_i), _utarray_eltptr(w, _ut_i)); \
+ } \
+ } else { \
+ memcpy(_utarray_eltptr(a,j), _utarray_eltptr(w,0), \
+ utarray_len(w)*((a)->icd.sz)); \
+ } \
+ (a)->i += utarray_len(w); \
+} while(0)
+
+#define utarray_resize(dst,num) do { \
+ unsigned _ut_i; \
+ if ((dst)->i > (unsigned)(num)) { \
+ if ((dst)->icd.dtor) { \
+ for (_ut_i = (num); _ut_i < (dst)->i; ++_ut_i) { \
+ (dst)->icd.dtor(_utarray_eltptr(dst, _ut_i)); \
+ } \
+ } \
+ } else if ((dst)->i < (unsigned)(num)) { \
+ utarray_reserve(dst, (num) - (dst)->i); \
+ if ((dst)->icd.init) { \
+ for (_ut_i = (dst)->i; _ut_i < (unsigned)(num); ++_ut_i) { \
+ (dst)->icd.init(_utarray_eltptr(dst, _ut_i)); \
+ } \
+ } else { \
+ memset(_utarray_eltptr(dst, (dst)->i), 0, (dst)->icd.sz*((num) - (dst)->i)); \
+ } \
+ } \
+ (dst)->i = (num); \
+} while(0)
+
+#define utarray_concat(dst,src) do { \
+ utarray_inserta(dst, src, utarray_len(dst)); \
+} while(0)
+
+#define utarray_erase(a,pos,len) do { \
+ if ((a)->icd.dtor) { \
+ unsigned _ut_i; \
+ for (_ut_i = 0; _ut_i < (len); _ut_i++) { \
+ (a)->icd.dtor(utarray_eltptr(a, (pos) + _ut_i)); \
+ } \
+ } \
+ if ((a)->i > ((pos) + (len))) { \
+ memmove(_utarray_eltptr(a, pos), _utarray_eltptr(a, (pos) + (len)), \
+ ((a)->i - ((pos) + (len))) * (a)->icd.sz); \
+ } \
+ (a)->i -= (len); \
+} while(0)
+
+#define utarray_renew(a,u) do { \
+ if (a) utarray_clear(a); \
+ else utarray_new(a, u); \
+} while(0)
+
+#define utarray_clear(a) do { \
+ if ((a)->i > 0) { \
+ if ((a)->icd.dtor) { \
+ unsigned _ut_i; \
+ for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \
+ (a)->icd.dtor(_utarray_eltptr(a, _ut_i)); \
+ } \
+ } \
+ (a)->i = 0; \
+ } \
+} while(0)
+
+#define utarray_sort(a,cmp) do { \
+ qsort((a)->d, (a)->i, (a)->icd.sz, cmp); \
+} while(0)
+
+#define utarray_find(a,v,cmp) bsearch((v),(a)->d,(a)->i,(a)->icd.sz,cmp)
+
+#define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a,0)) : NULL)
+#define utarray_next(a,e) (((e)==NULL) ? utarray_front(a) : (((a)->i != utarray_eltidx(a,e)+1) ? _utarray_eltptr(a,utarray_eltidx(a,e)+1) : NULL))
+#define utarray_prev(a,e) (((e)==NULL) ? utarray_back(a) : ((utarray_eltidx(a,e) != 0) ? _utarray_eltptr(a,utarray_eltidx(a,e)-1) : NULL))
+#define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a,(a)->i-1)) : NULL)
+#define utarray_eltidx(a,e) (((char*)(e) - (a)->d) / (a)->icd.sz)
+
+/* last we pre-define a few icd for common utarrays of ints and strings */
+static void utarray_str_cpy(void *dst, const void *src) {
+ char *const *srcc = (char *const *)src;
+ char **dstc = (char**)dst;
+ if (*srcc == NULL) {
+ *dstc = NULL;
+ } else {
+ *dstc = (char*)malloc(strlen(*srcc) + 1);
+ if (*dstc == NULL) {
+ utarray_oom();
+ } else {
+ strcpy(*dstc, *srcc);
+ }
+ }
+}
+static void utarray_str_dtor(void *elt) {
+ char **eltc = (char**)elt;
+ if (*eltc != NULL) free(*eltc);
+}
+static const UT_icd ut_str_icd UTARRAY_UNUSED = {sizeof(char*),NULL,utarray_str_cpy,utarray_str_dtor};
+static const UT_icd ut_int_icd UTARRAY_UNUSED = {sizeof(int),NULL,NULL,NULL};
+static const UT_icd ut_ptr_icd UTARRAY_UNUSED = {sizeof(void*),NULL,NULL,NULL};
+
+
+#endif /* UTARRAY_H */
diff --git a/challenge-244/paulo-custodio/cpp/ch-1.cpp b/challenge-244/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..8e0b700eba
--- /dev/null
+++ b/challenge-244/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,66 @@
+/*
+Challenge 244
+
+Task 1: Count Smaller
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to calculate the number of integers smaller than the integer
+at each index.
+Example 1
+
+Input: @int = (8, 1, 2, 2, 3)
+Output: (4, 0, 1, 1, 3)
+
+For index = 0, count of elements less 8 is 4.
+For index = 1, count of elements less 1 is 0.
+For index = 2, count of elements less 2 is 1.
+For index = 3, count of elements less 2 is 1.
+For index = 4, count of elements less 3 is 3.
+
+Example 2
+
+Input: @int = (6, 5, 4, 8)
+Output: (2, 1, 0, 3)
+
+Example 3
+
+Input: @int = (2, 2, 2)
+Output: (0, 0, 0)
+*/
+
+#include <iostream>
+#include <vector>
+using namespace std;
+
+vector<int> calc_smaller(const vector<int>& nums) {
+ vector<int> smaller;
+
+ for (size_t i = 0; i < nums.size(); i++) {
+ int count = 0;
+ for (size_t j = 0; j < nums.size(); j++) {
+ if (nums[j] < nums[i])
+ count++;
+ }
+ smaller.push_back(count);
+ }
+ return smaller;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ cerr << "Usage: ch-1 n n n ..." << endl;
+ exit(EXIT_FAILURE);
+ }
+
+ vector<int> nums;
+ for (int i = 1; i < argc; i++)
+ nums.push_back(atoi(argv[i]));
+
+ vector<int> smaller = calc_smaller(nums);
+
+ for (size_t i = 0; i < smaller.size(); i++)
+ cout << smaller[i] << " ";
+ cout << endl;
+}
diff --git a/challenge-244/paulo-custodio/cpp/ch-2.cpp b/challenge-244/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..9bc5bcd97b
--- /dev/null
+++ b/challenge-244/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,150 @@
+/*
+Challenge 244
+
+Task 2: Group Hero
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers representing the strength.
+
+Write a script to return the sum of the powers of all possible combinations;
+power is defined as the square of the largest number in a sequence, multiplied
+by the smallest.
+
+Example 1
+
+Input: @nums = (2, 1, 4)
+Output: 141
+
+Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8
+Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1
+Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64
+Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4
+Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32
+Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16
+Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16
+
+Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141
+*/
+
+#include <algorithm>
+#include <cassert>
+#include <iostream>
+#include <vector>
+using namespace std;
+
+class Counter {
+public:
+ Counter(int k, int max);
+
+ const vector<int>& counters() const { return m_counters; }
+
+ bool increment(); // return false when wrapping arround
+ bool is_unique(); // return true if counters are all different and increasing
+ bool at_end() const { return m_at_end; }
+
+private:
+ int m_max;
+ vector<int> m_counters;
+ bool m_at_end;
+};
+
+Counter::Counter(int k, int max) {
+ m_max = max;
+ m_counters.resize(k);
+ m_at_end = false;
+}
+
+bool Counter::increment() {
+ if (m_at_end)
+ return false;
+
+ int i = static_cast<int>(m_counters.size()) - 1;
+ while (i >= 0) {
+ m_counters[i]++;
+ if (m_counters[i] < m_max)
+ return true;
+ else {
+ m_counters[i] = 0;
+ i--;
+ }
+ }
+
+ m_at_end = true;
+ return false;
+}
+
+bool Counter::is_unique() {
+ for (size_t i = 1; i < m_counters.size(); i++) {
+ if (m_counters[i] <= m_counters[i - 1])
+ return false;
+ }
+ return true;
+}
+
+class Combination : private Counter {
+public:
+ Combination(int k, const vector<int>& nums)
+ : Counter(k, static_cast<int>(nums.size())), m_k(k), m_nums(nums) {}
+
+ bool next(vector<int>& combo);
+
+private:
+ int m_k;
+ const vector<int>& m_nums;
+};
+
+bool Combination::next(vector<int>& combo) {
+ combo.clear();
+ if (at_end())
+ return false;
+
+ while (!is_unique()) {
+ if (!increment())
+ return false;
+ }
+
+ for (int i = 0; i < m_k; i++)
+ combo.push_back(m_nums[counters()[i]]);
+
+ increment();
+
+ return true;
+}
+
+int calc_power(const vector<int>& nums) {
+ assert(nums.size() > 0);
+ int mn = *min_element(nums.begin(), nums.end());
+ int mx = *max_element(nums.begin(), nums.end());
+ return mx * mx * mn;
+}
+
+int compute_power_k(int k, const vector<int>& nums) {
+ Combination combination(k, nums);
+ vector<int> combo;
+ int sum = 0;
+ while (combination.next(combo))
+ sum += calc_power(combo);
+ return sum;
+}
+
+int compute_power(const vector<int>& nums) {
+ int sum = 0;
+ for (int k = 1; k <= static_cast<int>(nums.size()); k++)
+ sum += compute_power_k(k, nums);
+ return sum;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ cerr << "Usage: ch-2 n n n ..." << endl;
+ exit(EXIT_FAILURE);
+ }
+
+ vector<int> nums;
+ for (int i = 1; i < argc; i++)
+ nums.push_back(atoi(argv[i]));
+
+ int sum = compute_power(nums);
+
+ cout << sum << endl;
+}
diff --git a/challenge-244/paulo-custodio/perl/ch-1.pl b/challenge-244/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..128c6313ff
--- /dev/null
+++ b/challenge-244/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+# Challenge 244
+#
+# Task 1: Count Smaller
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers.
+#
+# Write a script to calculate the number of integers smaller than the integer
+# at each index.
+# Example 1
+#
+# Input: @int = (8, 1, 2, 2, 3)
+# Output: (4, 0, 1, 1, 3)
+#
+# For index = 0, count of elements less 8 is 4.
+# For index = 1, count of elements less 1 is 0.
+# For index = 2, count of elements less 2 is 1.
+# For index = 3, count of elements less 2 is 1.
+# For index = 4, count of elements less 3 is 3.
+#
+# Example 2
+#
+# Input: @int = (6, 5, 4, 8)
+# Output: (2, 1, 0, 3)
+#
+# Example 3
+#
+# Input: @int = (2, 2, 2)
+# Output: (0, 0, 0)
+
+use Modern::Perl;
+
+@ARGV or die "Usage: $0 n n n...\n";
+
+my @nums = @ARGV;
+my @smaller;
+
+for my $i (0..$#nums) {
+ $smaller[$i] = 0;
+ for my $j (0..$#nums) {
+ $smaller[$i]++ if $nums[$j] < $nums[$i];
+ }
+}
+
+say "@smaller";
diff --git a/challenge-244/paulo-custodio/perl/ch-2.pl b/challenge-244/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..912e37952b
--- /dev/null
+++ b/challenge-244/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+# Challenge 244
+#
+# Task 2: Group Hero
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers representing the strength.
+#
+# Write a script to return the sum of the powers of all possible combinations;
+# power is defined as the square of the largest number in a sequence, multiplied
+# by the smallest.
+#
+# Example 1
+#
+# Input: @nums = (2, 1, 4)
+# Output: 141
+#
+# Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8
+# Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1
+# Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64
+# Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4
+# Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32
+# Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16
+# Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16
+#
+# Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141
+
+use Modern::Perl;
+use List::Util qw( min max );
+use Math::Combinatorics;
+
+@ARGV or die "Usage: $0 n n n...\n";
+
+sub calc_power {
+ my(@n) = @_;
+ return (max(@n)**2) * min(@n);
+}
+
+sub power {
+ my(@n) = @_;
+ my $sum = 0;
+ for my $k (1 .. @n) {
+ my $combinat = Math::Combinatorics->new(count => $k,
+ data => [@n]);
+ while(my @combo = $combinat->next_combination){
+ $sum += calc_power(@combo);
+ }
+ }
+ return $sum;
+}
+
+say power(@ARGV);
diff --git a/challenge-244/paulo-custodio/t/test-1.yaml b/challenge-244/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..8f51ec4f8a
--- /dev/null
+++ b/challenge-244/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 8 1 2 2 3
+ input:
+ output: 4 0 1 1 3
+- setup:
+ cleanup:
+ args: 6 5 4 8
+ input:
+ output: 2 1 0 3
+- setup:
+ cleanup:
+ args: 2 2 2
+ input:
+ output: 0 0 0
diff --git a/challenge-244/paulo-custodio/t/test-2.yaml b/challenge-244/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..139f5017a1
--- /dev/null
+++ b/challenge-244/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 2 1 4
+ input:
+ output: 141