diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-07-05 10:00:52 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-07-05 10:00:52 +0100 |
| commit | b94a1cefc2b94d78be10d40abba67f812fc2b4d0 (patch) | |
| tree | 3497945950b1755dd4d9226374ec5065a2935bb1 | |
| parent | 87a6047a60f9e12271645370808b9624df18a58c (diff) | |
| parent | e8b6a242f32124fc0740d3bbe7f6888d53e2daee (diff) | |
| download | perlweeklychallenge-club-b94a1cefc2b94d78be10d40abba67f812fc2b4d0.tar.gz perlweeklychallenge-club-b94a1cefc2b94d78be10d40abba67f812fc2b4d0.tar.bz2 perlweeklychallenge-club-b94a1cefc2b94d78be10d40abba67f812fc2b4d0.zip | |
Merge remote-tracking branch 'upstream/master'
51 files changed, 4524 insertions, 1803 deletions
diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index 2bc0715c09..6d50c9592d 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -22,6 +22,7 @@ our %LANG = ( d => 'd', forth => 'fs', lua => 'lua', + pascal => 'pas', perl => 'pl', python => 'py', ); @@ -158,6 +159,10 @@ sub build { if (/^lua$/) { return "$LUA $prog"; } + if (/^pascal$/) { + run("fpc -o$exe $prog") if (!-f $exe || -M $exe > -M $prog); + return $exe; + } if (/^perl$/) { return "perl $prog"; } diff --git a/challenge-115/paulo-custodio/c/ch-1.c b/challenge-115/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..37f4b39cc3 --- /dev/null +++ b/challenge-115/paulo-custodio/c/ch-1.c @@ -0,0 +1,117 @@ +/* +Challenge 115 + +TASK #1 - String Chain +Submitted by: Mohammad S Anwar +You are given an array of strings. + +Write a script to find out if the given strings can be chained to form a +circle. Print 1 if found otherwise 0. + +A string $S can be put before another string $T in circle if the last +character of $S is same as first character of $T. + +Examples: +Input: @S = ("abc", "dea", "cd") +Output: 1 as we can form circle e.g. "abc", "cd", "dea". + +Input: @S = ("ade", "cbd", "fgh") +Output: 0 as we can't form circle. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include "utlist.h" + +typedef struct Word { + char* word; + struct Word* next, * prev; +} Word; + +void* check_mem(void* p) { + if (!p) { + fputs("out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +void words_push_back(Word** phead, const char* word) { + Word* node = check_mem(malloc(sizeof(Word))); + node->word = check_mem(strdup(word)); + DL_APPEND(*phead, node); +} + +void words_delete(Word** phead, const char* word) { + Word* node, * temp; + DL_FOREACH_SAFE(*phead, node, temp) { + if (strcmp(node->word, word) == 0) { + DL_DELETE(*phead, node); + free(node->word); + free(node); + } + } +} + +Word* words_clone(Word** phead) { + Word* node, * temp, * new_words = NULL; + DL_FOREACH_SAFE(*phead, node, temp) { + words_push_back(&new_words, node->word); + } + return new_words; +} + +void words_free(Word** phead) { + while (*phead) { + Word* node = *phead; + DL_DELETE(*phead, node); + free(node->word); + free(node); + } +} + +bool is_chain(char* word1, char* word2) { + if (word1[strlen(word1) - 1] == word2[0]) + return true; + else + return false; +} + +bool found_solution = false; + +bool is_circle(Word* pending, Word* path) { + if (found_solution) return true; + if (pending == NULL) { + if (!found_solution) found_solution = is_chain(path->prev->word, path->word); + } + else { + Word* node, * temp; + DL_FOREACH_SAFE(pending, node, temp) { + if (!found_solution) { + if (path == NULL || is_chain(path->prev->word, node->word)) { + Word* new_pending = words_clone(&pending); + words_delete(&new_pending, node->word); + Word* new_path = words_clone(&path); + words_push_back(&new_path, node->word); + + if (!found_solution) found_solution = is_circle(new_pending, new_path); + + words_free(&new_pending); + words_free(&new_path); + } + } + } + } + return found_solution; +} + +int main(int argc, char* argv[]) { + Word* words = NULL; + for (int i = 1; i < argc; i++) + words_push_back(&words, argv[i]); + bool found = is_circle(words, NULL); + printf("%d\n", found ? 1 : 0); + words_free(&words); +} diff --git a/challenge-115/paulo-custodio/c/ch-2.c b/challenge-115/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..07e4ab153c --- /dev/null +++ b/challenge-115/paulo-custodio/c/ch-2.c @@ -0,0 +1,76 @@ +/* +Challenge 115 + +TASK #2 - Largest Multiple +Submitted by: Mohammad S Anwar +You are given a list of positive integers (0-9), single digit. + +Write a script to find the largest multiple of 2 that can be formed from the +list. + +Examples +Input: @N = (1, 0, 2, 6) +Output: 6210 + +Input: @N = (1, 4, 2, 8) +Output: 8412 + +Input: @N = (4, 1, 7, 6) +Output: 7614 +*/ + +#include <stdio.h> +#include <stdlib.h> + +void* check_mem(void* p) { + if (!p) { + fputs("out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int reverse_cmp(const void* a, const void* b) { + return -(*(int*)a - *(int*)b); +} + +int largest_even(int nums_size, int* nums) { + // smallest even number is last digit + int last_digit = 10; + for (int i = 0; i < nums_size; i++) + if ((nums[i] % 2) == 0 && nums[i] < last_digit) + last_digit = nums[i]; + if (last_digit == 10) return 0; + + // remove last_digit from list + for (int i = 0; i < nums_size; i++) { + if (nums[i] == last_digit) { + nums[i] = nums[nums_size - 1]; + nums_size--; + break; + } + } + + // sort remaining digits in descending order + qsort(nums, nums_size, sizeof(int), reverse_cmp); + + int result = 0; + for (int i = 0; i < nums_size; i++) + result = result * 10 + nums[i]; + result = result * 10 + last_digit; + + return result; +} + +int main(int argc, char* argv[]) { + if (argc < 2) return EXIT_FAILURE; + + int nums_size = argc - 1; + int* nums = (int*)check_mem(malloc(nums_size * sizeof(int))); + for (int i = 0; i < nums_size; i++) + nums[i] = atoi(argv[1 + i]); + + printf("%d\n", largest_even(nums_size, nums)); + + free(nums); +} diff --git a/challenge-115/paulo-custodio/c/utlist.h b/challenge-115/paulo-custodio/c/utlist.h new file mode 100644 index 0000000000..1979448a7b --- /dev/null +++ b/challenge-115/paulo-custodio/c/utlist.h @@ -0,0 +1,1073 @@ +/* +Copyright (c) 2007-2021, Troy D. Hanson http://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. +*/ + +#ifndef UTLIST_H +#define UTLIST_H + +#define UTLIST_VERSION 2.3.0 + +#include <assert.h> + +/* + * This file contains macros to manipulate singly and doubly-linked lists. + * + * 1. LL_ macros: singly-linked lists. + * 2. DL_ macros: doubly-linked lists. + * 3. CDL_ macros: circular doubly-linked lists. + * + * To use singly-linked lists, your structure must have a "next" pointer. + * To use doubly-linked lists, your structure must "prev" and "next" pointers. + * Either way, the pointer to the head of the list must be initialized to NULL. + * + * ----------------.EXAMPLE ------------------------- + * struct item { + * int id; + * struct item *prev, *next; + * } + * + * struct item *list = NULL: + * + * int main() { + * struct item *item; + * ... allocate and populate item ... + * DL_APPEND(list, item); + * } + * -------------------------------------------------- + * + * For doubly-linked lists, the append and delete macros are O(1) + * For singly-linked lists, append and delete are O(n) but prepend is O(1) + * The sort macro is O(n log(n)) for all types of single/double/circular lists. + */ + +/* These macros use decltype or the earlier __typeof GNU extension. + As decltype is only available in newer compilers (VS2010 or gcc 4.3+ + when compiling c++ source) this code uses whatever method is needed + or, for VS2008 where neither is available, uses casting workarounds. */ +#if !defined(LDECLTYPE) && !defined(NO_DECLTYPE) +#if defined(_MSC_VER) /* MS compiler */ +#if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ +#define LDECLTYPE(x) decltype(x) +#else /* VS2008 or older (or VS2010 in C mode) */ +#define NO_DECLTYPE +#endif +#elif defined(__BORLANDC__) || defined(__ICCARM__) || defined(__LCC__) || defined(__WATCOMC__) +#define NO_DECLTYPE +#else /* GNU, Sun and other compilers */ +#define LDECLTYPE(x) __typeof(x) +#endif +#endif + +/* for VS2008 we use some workarounds to get around the lack of decltype, + * namely, we always reassign our tmp variable to the list head if we need + * to dereference its prev/next pointers, and save/restore the real head.*/ +#ifdef NO_DECLTYPE +#define IF_NO_DECLTYPE(x) x +#define LDECLTYPE(x) char* +#define UTLIST_SV(elt,list) _tmp = (char*)(list); {char **_alias = (char**)&(list); *_alias = (elt); } +#define UTLIST_NEXT(elt,list,next) ((char*)((list)->next)) +#define UTLIST_NEXTASGN(elt,list,to,next) { char **_alias = (char**)&((list)->next); *_alias=(char*)(to); } +/* #define UTLIST_PREV(elt,list,prev) ((char*)((list)->prev)) */ +#define UTLIST_PREVASGN(elt,list,to,prev) { char **_alias = (char**)&((list)->prev); *_alias=(char*)(to); } +#define UTLIST_RS(list) { char **_alias = (char**)&(list); *_alias=_tmp; } +#define UTLIST_CASTASGN(a,b) { char **_alias = (char**)&(a); *_alias=(char*)(b); } +#else +#define IF_NO_DECLTYPE(x) +#define UTLIST_SV(elt,list) +#define UTLIST_NEXT(elt,list,next) ((elt)->next) +#define UTLIST_NEXTASGN(elt,list,to,next) ((elt)->next)=(to) +/* #define UTLIST_PREV(elt,list,prev) ((elt)->prev) */ +#define UTLIST_PREVASGN(elt,list,to,prev) ((elt)->prev)=(to) +#define UTLIST_RS(list) +#define UTLIST_CASTASGN(a,b) (a)=(b) +#endif + +/****************************************************************************** + * The sort macro is an adaptation of Simon Tatham's O(n log(n)) mergesort * + * Unwieldy variable names used here to avoid shadowing passed-in variables. * + *****************************************************************************/ +#define LL_SORT(list, cmp) \ + LL_SORT2(list, cmp, next) + +#define LL_SORT2(list, cmp, next) \ +do { \ + LDECLTYPE(list) _ls_p; \ + LDECLTYPE(list) _ls_q; \ + LDECLTYPE(list) _ls_e; \ + LDECLTYPE(list) _ls_tail; \ + IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \ + int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ + if (list) { \ + _ls_insize = 1; \ + _ls_looping = 1; \ + while (_ls_looping) { \ + UTLIST_CASTASGN(_ls_p,list); \ + (list) = NULL; \ + _ls_tail = NULL; \ + _ls_nmerges = 0; \ + while (_ls_p) { \ + _ls_nmerges++; \ + _ls_q = _ls_p; \ + _ls_psize = 0; \ + for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \ + _ls_psize++; \ + UTLIST_SV(_ls_q,list); _ls_q = UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); \ + if (!_ls_q) break; \ + } \ + _ls_qsize = _ls_insize; \ + while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \ + if (_ls_psize == 0) { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } else if (_ls_qsize == 0 || !_ls_q) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else if (cmp(_ls_p,_ls_q) <= 0) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } \ + if (_ls_tail) { \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \ + } else { \ + UTLIST_CASTASGN(list,_ls_e); \ + } \ + _ls_tail = _ls_e; \ + } \ + _ls_p = _ls_q; \ + } \ + if (_ls_tail) { \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,NULL,next); UTLIST_RS(list); \ + } \ + if (_ls_nmerges <= 1) { \ + _ls_looping=0; \ + } \ + _ls_insize *= 2; \ + } \ + } \ +} while (0) + + +#define DL_SORT(list, cmp) \ + DL_SORT2(list, cmp, prev, next) + +#define DL_SORT2(list, cmp, prev, next) \ +do { \ + LDECLTYPE(list) _ls_p; \ + LDECLTYPE(list) _ls_q; \ + LDECLTYPE(list) _ls_e; \ + LDECLTYPE(list) _ls_tail; \ + IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \ + int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ + if (list) { \ + _ls_insize = 1; \ + _ls_looping = 1; \ + while (_ls_looping) { \ + UTLIST_CASTASGN(_ls_p,list); \ + (list) = NULL; \ + _ls_tail = NULL; \ + _ls_nmerges = 0; \ + while (_ls_p) { \ + _ls_nmerges++; \ + _ls_q = _ls_p; \ + _ls_psize = 0; \ + for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \ + _ls_psize++; \ + UTLIST_SV(_ls_q,list); _ls_q = UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); \ + if (!_ls_q) break; \ + } \ + _ls_qsize = _ls_insize; \ + while ((_ls_psize > 0) || ((_ls_qsize > 0) && _ls_q)) { \ + if (_ls_psize == 0) { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } else if ((_ls_qsize == 0) || (!_ls_q)) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else if (cmp(_ls_p,_ls_q) <= 0) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } \ + if (_ls_tail) { \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \ + } else { \ + UTLIST_CASTASGN(list,_ls_e); \ + } \ + UTLIST_SV(_ls_e,list); UTLIST_PREVASGN(_ls_e,list,_ls_tail,prev); UTLIST_RS(list); \ + _ls_tail = _ls_e; \ + } \ + _ls_p = _ls_q; \ + } \ + UTLIST_CASTASGN((list)->prev, _ls_tail); \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,NULL,next); UTLIST_RS(list); \ + if (_ls_nmerges <= 1) { \ + _ls_looping=0; \ + } \ + _ls_insize *= 2; \ + } \ + } \ +} while (0) + +#define CDL_SORT(list, cmp) \ + CDL_SORT2(list, cmp, prev, next) + +#define CDL_SORT2(list, cmp, prev, next) \ +do { \ + LDECLTYPE(list) _ls_p; \ + LDECLTYPE(list) _ls_q; \ + LDECLTYPE(list) _ls_e; \ + LDECLTYPE(list) _ls_tail; \ + LDECLTYPE(list) _ls_oldhead; \ + LDECLTYPE(list) _tmp; \ + int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ + if (list) { \ + _ls_insize = 1; \ + _ls_looping = 1; \ + while (_ls_looping) { \ + UTLIST_CASTASGN(_ls_p,list); |
