aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-242/paulo-custodio/forth/ch-1.fs8
-rw-r--r--challenge-244/paulo-custodio/forth/ch-1.fs79
-rw-r--r--challenge-244/paulo-custodio/forth/ch-2.fs145
-rw-r--r--challenge-245/paulo-custodio/Makefile2
-rw-r--r--challenge-245/paulo-custodio/c/ch-1.c62
-rw-r--r--challenge-245/paulo-custodio/c/ch-2.c79
-rw-r--r--challenge-245/paulo-custodio/c/utarray.h252
-rw-r--r--challenge-245/paulo-custodio/cpp/ch-1.cpp53
-rw-r--r--challenge-245/paulo-custodio/cpp/ch-2.cpp70
-rw-r--r--challenge-245/paulo-custodio/perl/ch-1.pl32
-rw-r--r--challenge-245/paulo-custodio/perl/ch-2.pl55
-rw-r--r--challenge-245/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-245/paulo-custodio/t/test-2.yaml15
13 files changed, 860 insertions, 2 deletions
diff --git a/challenge-242/paulo-custodio/forth/ch-1.fs b/challenge-242/paulo-custodio/forth/ch-1.fs
index 6de8f3a904..20fe605fd2 100644
--- a/challenge-242/paulo-custodio/forth/ch-1.fs
+++ b/challenge-242/paulo-custodio/forth/ch-1.fs
@@ -26,8 +26,12 @@
CREATE arr1 256 CELLS ALLOT
CREATE arr2 256 CELLS ALLOT
-: array_size ( arr-addr -- size-addr ) ;
-: array[] ( arr-addr i -- elem-addr ) 1+ CELLS + ;
+: array_size ( arr-addr -- size-addr )
+;
+
+: array[] ( arr-addr i -- elem-addr )
+ 1+ CELLS +
+;
: array_push_back ( arr-addr n -- )
{ arr n }
diff --git a/challenge-244/paulo-custodio/forth/ch-1.fs b/challenge-244/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..02010237d9
--- /dev/null
+++ b/challenge-244/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,79 @@
+#! /usr/bin/env gforth
+
+\ 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)
+
+CREATE nums 256 CELLS ALLOT
+CREATE smaller 256 CELLS ALLOT
+
+: array_size ( arr-addr -- size-addr )
+;
+
+: array[] ( arr-addr i -- elem-addr )
+ 1+ CELLS +
+;
+
+: array_push_back ( arr-addr n -- )
+ { arr n }
+ arr array_size @ ( size )
+ arr SWAP array[] ( elem-addr )
+ n SWAP !
+ arr array_size 1 SWAP +!
+;
+
+: .array ( arr -- )
+ { arr }
+ arr array_size @ 0 ?DO
+ arr I array[] @ .
+ LOOP
+;
+
+: collect_args ( -- )
+ BEGIN NEXT-ARG DUP WHILE
+ 0 0 2SWAP >NUMBER 2DROP DROP
+ nums SWAP array_push_back
+ REPEAT
+ 2DROP
+;
+
+: compute_smaller ( -- )
+ nums array_size @ 0 ?DO
+ 0 ( count )
+ nums array_size @ 0 ?DO
+ nums I array[] @ nums J array[] @ < IF 1+ THEN
+ LOOP
+ smaller SWAP array_push_back
+ LOOP
+;
+
+collect_args
+compute_smaller
+smaller .array
+BYE
diff --git a/challenge-244/paulo-custodio/forth/ch-2.fs b/challenge-244/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..ec3b067fc4
--- /dev/null
+++ b/challenge-244/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,145 @@
+#! /usr/bin/env gforth
+
+\ 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
+
+CREATE nums 256 CELLS ALLOT
+CREATE counters 256 CELLS ALLOT
+CREATE combo 256 CELLS ALLOT
+
+: array_size ( arr-addr -- size-addr )
+;
+
+: array_clear ( arr-addr -- )
+ 0 SWAP array_size !
+;
+
+: array[] ( arr-addr i -- elem-addr )
+ 1+ CELLS +
+;
+
+: array_push_back ( arr-addr n -- )
+ { arr n }
+ arr array_size @ ( size )
+ arr SWAP array[] ( elem-addr )
+ n SWAP !
+ arr array_size 1 SWAP +!
+;
+
+: array_min ( arr -- n )
+ { arr }
+ arr 0 array[] @ ( min )
+ arr array_size @ 0 ?DO
+ arr I array[] @ MIN
+ LOOP
+;
+
+: array_max ( arr -- n )
+ { arr }
+ arr 0 array[] @ ( min )
+ arr array_size @ 0 ?DO
+ arr I array[] @ MAX
+ LOOP
+;
+
+: .array ( arr -- )
+ { arr }
+ arr array_size @ 0 ?DO
+ arr I array[] @ .
+ LOOP
+;
+
+: collect_args ( -- )
+ BEGIN NEXT-ARG DUP WHILE
+ 0 0 2SWAP >NUMBER 2DROP DROP
+ nums SWAP array_push_back
+ REPEAT
+ 2DROP
+;
+
+: counters_init ( k -- )
+ counters array_clear
+ 0 ?DO
+ counters 0 array_push_back
+ LOOP
+;
+
+: counters_increment ( -- f ) \ return false when wrapping arround
+ counters array_size @ 1- { i }
+ BEGIN i 0 >= WHILE
+ counters i array[] 1 SWAP +!
+ counters i array[] @ nums array_size @ < IF TRUE EXIT THEN
+ 0 counters i array[] !
+ i 1- TO i
+ REPEAT
+ FALSE
+;
+
+: counters_unique ( -- f ) \ return true if counters are all different and increasing
+ TRUE
+ counters array_size @ 1 ?DO
+ counters I array[] @
+ counters I 1- array[] @
+ <= IF DROP FALSE LEAVE THEN
+ LOOP
+;
+
+: counters_select ( -- )
+ combo array_clear
+ counters array_size @ 0 ?DO
+ counters I array[] @
+ nums SWAP array[] @
+ combo SWAP array_push_back
+ LOOP
+;
+
+: array_calc_power ( arr - n )
+ { arr }
+ arr array_max DUP *
+ arr array_min *
+;
+
+: calc_power_k ( k -- n )
+ counters_init
+ 0 ( power )
+ BEGIN
+ counters_unique IF
+ counters_select combo array_calc_power +
+ THEN
+ counters_increment 0= IF EXIT THEN
+ AGAIN
+;
+
+: calc_power ( -- n )
+ 0 ( power )
+ nums array_size @ 1+ 1 ?DO
+ I calc_power_k +
+ LOOP
+;
+
+collect_args
+calc_power . CR
+BYE
diff --git a/challenge-245/paulo-custodio/Makefile b/challenge-245/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-245/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-245/paulo-custodio/c/ch-1.c b/challenge-245/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..e9e4ed3233
--- /dev/null
+++ b/challenge-245/paulo-custodio/c/ch-1.c
@@ -0,0 +1,62 @@
+/*
+Challenge 245
+
+Task 1: Sort Language
+Submitted by: Mohammad S Anwar
+
+You are given two array of languages and its popularity.
+
+Write a script to sort the language based on popularity.
+Example 1
+
+Input: @lang = ('perl', 'c', 'python')
+ @popularity = (2, 1, 3)
+Output: ('c', 'perl', 'python')
+
+Example 2
+
+Input: @lang = ('c++', 'haskell', 'java')
+ @popularity = (1, 3, 2)
+Output: ('c++', 'java', 'haskell')
+*/
+
+#include "utarray.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct {
+ const char* name;
+ int pop;
+} Elem;
+
+UT_icd ut_elem_icd = { sizeof(Elem), NULL, NULL, NULL };
+
+int compare(const void* a_, const void* b_) {
+ Elem* a = (Elem*)a_;
+ Elem* b = (Elem*)b_;
+ return a->pop - b->pop;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ fputs("Usage: ch-1 lang pop lang pop ...\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ UT_array* langs;
+ utarray_new(langs, &ut_elem_icd);
+ for (int i = 1; i + 1 < argc; i += 2) {
+ Elem elem;
+ elem.name = argv[i];
+ elem.pop = atoi(argv[i + 1]);
+ utarray_push_back(langs, &elem);
+ }
+
+ qsort(utarray_eltptr(langs, 0), utarray_len(langs), sizeof(Elem), compare);
+
+ for (size_t i = 0; i < utarray_len(langs); i++)
+ printf("%s ", ((Elem*)utarray_eltptr(langs, i))->name);
+ printf("\n");
+
+ utarray_free(langs);
+}
diff --git a/challenge-245/paulo-custodio/c/ch-2.c b/challenge-245/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..b8cb319ebf
--- /dev/null
+++ b/challenge-245/paulo-custodio/c/ch-2.c
@@ -0,0 +1,79 @@
+/*
+Challenge 245
+
+Task 2: Largest of Three
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers >= 0.
+
+Write a script to return the largest number formed by concatenating some of
+the given integers in any order which is also multiple of 3. Return -1 if
+none found.
+
+Example 1
+
+Input: @ints = (8, 1, 9)
+Output: 981
+
+981 % 3 == 0
+
+Example 2
+
+Input: @ints = (8, 6, 7, 1, 0)
+Output: 8760
+
+Example 3
+
+Input: @ints = (1)
+Output: -1
+*/
+
+#include "utarray.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int calc_largest(int prefix, UT_array* nums) {
+ int largest = -1;
+
+ // check current prefix
+ if (prefix != 0 && prefix % 3 == 0 && prefix > largest)
+ largest = prefix;
+
+ // check all compinations of each number in nums
+ UT_array* remain;
+ utarray_new(remain, &ut_int_icd);
+
+ for (size_t i = 0; i < utarray_len(nums); i++) {
+ // create list of numbers excluding n
+ int n = *(int*)utarray_eltptr(nums, i);
+ utarray_clear(remain);
+ utarray_concat(remain, nums);
+ utarray_erase(remain, i, 1);
+
+ int this_largest = calc_largest(prefix * 10 + n, remain);
+ if (this_largest > largest)
+ largest = this_largest;
+ }
+ utarray_free(remain);
+
+ return largest;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ fputs("Usage: ch-2 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 largest = calc_largest(0, nums);
+ printf("%d\n", largest);
+
+ utarray_free(nums);
+}
diff --git a/challenge-245/paulo-custodio/c/utarray.h b/challenge-245/paulo-custodio/c/utarray.h
new file mode 100644
index 0000000000..1fe8bc1c74
--- /dev/null
+++ b/challenge-245/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-245/paulo-custodio/cpp/ch-1.cpp b/challenge-245/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..989962d317
--- /dev/null
+++ b/challenge-245/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,53 @@
+/*
+Challenge 245
+
+Task 1: Sort Language
+Submitted by: Mohammad S Anwar
+
+You are given two array of languages and its popularity.
+
+Write a script to sort the language based on popularity.
+Example 1
+
+Input: @lang = ('perl', 'c', 'python')
+ @popularity = (2, 1, 3)
+Output: ('c', 'perl', 'python')
+
+Example 2
+
+Input: @lang = ('c++', 'haskell', 'java')
+ @popularity = (1, 3, 2)
+Output: ('c++', 'java', 'haskell')
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <string>
+#include <vector>
+using namespace std;
+
+struct Elem {
+ string name;
+ int pop;
+
+ Elem(const string& name_ = "", int pop_ = 0) :name(name_), pop(pop_) {}
+ bool operator<(const Elem& other) { return pop < other.pop ? true : false; }
+};
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ cerr << "Usage: ch-1 lang pop lang pop ..." << endl;
+ exit(EXIT_FAILURE);
+ }
+
+ vector<Elem> langs;
+ for (int i = 1; i + 1 < argc; i += 2) {
+ langs.emplace_back(argv[i], atoi(argv[i + 1]));
+ }
+
+ sort(langs.begin(), langs.end());
+
+ for (auto& lang : langs)
+ cout << lang.name << " ";
+ cout << endl;
+}
diff --git a/challenge-245/paulo-custodio/cpp/ch-2.cpp b/challenge-245/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..b2a8328812
--- /dev/null
+++ b/challenge-245/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,70 @@
+/*
+Challenge 245
+
+Task 2: Largest of Three
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers >= 0.
+
+Write a script to return the largest number formed by concatenating some of
+the given integers in any order which is also multiple of 3. Return -1 if
+none found.
+
+Example 1
+
+Input: @ints = (8, 1, 9)
+Output: 981
+
+981 % 3 == 0
+
+Example 2
+
+Input: @ints = (8, 6, 7, 1, 0)
+Output: 8760
+
+Example 3
+
+Input: @ints = (1)
+Output: -1
+*/
+
+#include <iostream>
+#include <vector>
+using namespace std;
+
+int calc_largest(int prefix, const vector<int>& nums) {
+ int largest = -1;
+
+ // check current prefix
+ if (prefix != 0 && prefix % 3 == 0 && prefix > largest)
+ largest = prefix;
+
+ // check all compinations of each number in nums
+ vector<int> remain;
+ for (size_t i = 0; i < nums.size(); i++) {
+ // create list of numbers excluding n
+ int n = nums[i];
+ remain = nums;
+ remain.erase(remain.begin() + i);
+
+ int this_largest = calc_largest(prefix * 10 + n, remain);
+ if (this_largest > largest)
+ largest = this_largest;
+ }
+
+ return largest;
+}
+
+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 largest = calc_largest(0, nums);
+ cout << largest << endl;
+}
diff --git a/challenge-245/paulo-custodio/perl/ch-1.pl b/challenge-245/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..8b2ba9944d
--- /dev/null
+++ b/challenge-245/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+# Challenge 245
+#
+# Task 1: Sort Language
+# Submitted by: Mohammad S Anwar
+#
+# You are given two array of languages and its popularity.
+#
+# Write a script to sort the language based on popularity.
+# Example 1
+#
+# Input: @lang = ('perl', 'c', 'python')
+# @popularity = (2, 1, 3)
+# Output: ('c', 'perl', 'python')
+#
+# Example 2
+#
+# Input: @lang = ('c++', 'haskell', 'java')
+# @popularity = (1, 3, 2)
+# Output: ('c++', 'java', 'haskell')
+
+use Modern::Perl;
+
+@ARGV or die "Usage: $0 lang pop lang pop ...\n";
+my @lang;
+while (@ARGV) {
+ my($lang, $pop) = splice(@ARGV, 0, 2);
+ push @lang, [$lang, $pop];
+}
+@lang = map {$_->[0]} sort {$a->[1] <=> $b->[1]} @lang;
+say "@lang";
diff --git a/challenge-245/paulo-custodio/perl/ch-2.pl b/challenge-245/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..b50b9abf45
--- /dev/null
+++ b/challenge-245/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+
+# Challenge 245
+#
+# Task 2: Largest of Three
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers >= 0.
+#
+# Write a script to return the largest number formed by concatenating some of
+# the given integers in any order which is also multiple of 3. Return -1 if
+# none found.
+#
+# Example 1
+#
+# Input: @ints = (8, 1, 9)
+# Output: 981
+#
+# 981 % 3 == 0
+#
+# Example 2
+#
+# Input: @ints = (8, 6, 7, 1, 0)
+# Output: 8760
+#
+# Example 3
+#
+# Input: @ints = (1)
+# Output: -1
+
+use Modern::Perl;
+
+sub largest {
+ my($prefix, @nums) = @_;
+
+ my $largest = -1;
+ $prefix =~ s/^0+(\d)/$1/;
+ if (length($prefix) > 0) {
+ if ($prefix % 3 == 0) {
+ $largest = $prefix if $prefix > $largest;
+ }
+ }
+
+ for my $i (0 .. $#nums) {
+ my $new = largest($prefix.$nums[$i],
+ @nums[0..$i-1], @nums[$i+1..$#nums]);
+ $largest = $new if $new > $largest;
+ }
+
+ return $largest;
+}
+
+@ARGV or die "Usage: $0 n n n...\n";
+my @nums = @ARGV;
+say largest('', @nums);
diff --git a/challenge-245/paulo-custodio/t/test-1.yaml b/challenge-245/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..45c8b7fde8
--- /dev/null
+++ b/challenge-245/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: perl 2 c 1 python 3
+ input:
+ output: c perl python
+- setup:
+ cleanup:
+ args: c++ 1 haskell 3 java 2
+ input:
+ output: c++ java haskell
diff --git a/challenge-245/paulo-custodio/t/test-2.yaml b/challenge-245/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..3c03e02b73
--- /dev/null
+++ b/challenge-245/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 8 1 9
+ input:
+ output: 981
+- setup:
+ cleanup:
+ args: 8 6 7 1 0
+ input:
+ output: 8760
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: -1