diff options
37 files changed, 1441 insertions, 27 deletions
diff --git a/challenge-017/paulo-custodio/perl/ch-2.pl b/challenge-017/paulo-custodio/perl/ch-2.pl index 18f0e2c89e..e4cc41a84d 100644 --- a/challenge-017/paulo-custodio/perl/ch-2.pl +++ b/challenge-017/paulo-custodio/perl/ch-2.pl @@ -20,7 +20,7 @@ use Modern::Perl; -my $url = shift; +my $url = <>; my $word = qr{ [a-z_] [a-z_0-9+.-]* }ix; my $path = qr{ (?: $word | / )+ }ix; $url =~ m{^ (?<scheme> $word ) [:] diff --git a/challenge-017/paulo-custodio/python/ch-2.py b/challenge-017/paulo-custodio/python/ch-2.py index 7493986e88..3fda361c71 100644 --- a/challenge-017/paulo-custodio/python/ch-2.py +++ b/challenge-017/paulo-custodio/python/ch-2.py @@ -21,7 +21,7 @@ import sys import re -url = sys.argv[1] +url = sys.stdin.read().rstrip() word = r"(?i:[a-z_][a-z_0-9+.-]*)" pathre = r"(?:"+word+r"|/)+" diff --git a/challenge-017/paulo-custodio/t/test-2.yaml b/challenge-017/paulo-custodio/t/test-2.yaml index a1b82bbc9b..5225b60a3c 100644 --- a/challenge-017/paulo-custodio/t/test-2.yaml +++ b/challenge-017/paulo-custodio/t/test-2.yaml @@ -1,7 +1,7 @@ - setup: cleanup: - args: jdbc://user:password@localhost:3306/pwc?profile=true#h1 - input: + args: + input: jdbc://user:password@localhost:3306/pwc?profile=true#h1 output: | scheme: jdbc userinfo: user:password @@ -12,8 +12,8 @@ fragment: h1 - setup: cleanup: - args: jdbc://localhost:3306/pwc?profile=true#h1 - input: + args: + input: jdbc://localhost:3306/pwc?profile=true#h1 output: | scheme: jdbc userinfo: @@ -24,8 +24,8 @@ fragment: h1 - setup: cleanup: - args: jdbc://localhost/pwc?profile=true#h1 - input: + args: + input: jdbc://localhost/pwc?profile=true#h1 output: | scheme: jdbc userinfo: @@ -36,8 +36,8 @@ fragment: h1 - setup: cleanup: - args: jdbc:/pwc?profile=true#h1 - input: + args: + input: jdbc:/pwc?profile=true#h1 output: | scheme: jdbc userinfo: @@ -48,8 +48,8 @@ fragment: h1 - setup: cleanup: - args: jdbc:/pwc?profile=true - input: + args: + input: jdbc:/pwc?profile=true output: | scheme: jdbc userinfo: @@ -60,8 +60,8 @@ fragment: - setup: cleanup: - args: jdbc:/pwc - input: + args: + input: jdbc:/pwc output: | scheme: jdbc userinfo: diff --git a/challenge-093/paulo-custodio/Makefile b/challenge-093/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-093/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-093/paulo-custodio/c/ch-1.c b/challenge-093/paulo-custodio/c/ch-1.c index b796bf97eb..0145df5b10 100644 --- a/challenge-093/paulo-custodio/c/ch-1.c +++ b/challenge-093/paulo-custodio/c/ch-1.c @@ -32,6 +32,14 @@ Output: 3 #include <stdio.h> #include <stdlib.h> +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + typedef struct Point { int x, y; } Point; @@ -85,7 +93,7 @@ int main(int argc, char* argv[]) { // allocate memory for the point array and read it from argv[] points_size = (argc-1)/2; - points = calloc(points_size, sizeof(Point)); + points = check_mem(calloc(points_size, sizeof(Point))); assert(points); for (size_t i = 0; i < points_size; i++) { points[i].x = atoi(argv[1+2*i]); diff --git a/challenge-093/paulo-custodio/c/ch-2.c b/challenge-093/paulo-custodio/c/ch-2.c index 29f56f4f22..578ab7c199 100644 --- a/challenge-093/paulo-custodio/c/ch-2.c +++ b/challenge-093/paulo-custodio/c/ch-2.c @@ -47,9 +47,17 @@ typedef struct Tree { char** lines; size_t num_lines; +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + // create and delete a tree Tree* tree_new() { - Tree* node = calloc(1, sizeof(Tree)); + Tree* node = check_mem(calloc(1, sizeof(Tree))); assert(node); return node; } @@ -64,7 +72,7 @@ void tree_delete(Tree* node) { void lines_read() { char line[MAXLINE]; while (fgets(line, sizeof(line), stdin)) { - lines = realloc(lines, (num_lines + 1) * sizeof(char*)); + lines = check_mem(realloc(lines, (num_lines + 1) * sizeof(char*))); assert(lines); lines[num_lines] = strdup(line); assert(lines[num_lines]); diff --git a/challenge-098/paulo-custodio/Makefile b/challenge-098/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-098/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-100/paulo-custodio/Makefile b/challenge-100/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-100/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-200/paulo-custodio/Makefile b/challenge-200/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-200/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-200/paulo-custodio/basic/ch-1.bas b/challenge-200/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..ae410376dd --- /dev/null +++ b/challenge-200/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,72 @@ +' Challenge 200 +' +' Task 1: Arithmetic Slices +' Submitted by: Mohammad S Anwar +' You are given an array of integers. +' +' Write a script to find out all Arithmetic Slices for the given array of integers. +' +' An integer array is called arithmetic if it has at least 3 elements and the +' differences between any three consecutive elements are the same. +' +' +' Example 1 +' Input: @array = (1,2,3,4) +' Output: (1,2,3), (2,3,4), (1,2,3,4) +' Example 2 +' Input: @array = (2) +' Output: () as no slice found. + +function is_arithmetic(nums() as integer, i1 as integer, i2 as integer) as boolean + dim st as integer, i as integer + + is_arithmetic=true + if i2-i1+1<3 then + is_arithmetic=false + else + st=nums(i1+1)-nums(i1) + for i=i1+2 to i2 + if nums(i)-nums(i-1)<>st then + is_arithmetic=false + exit for + end if + next + end if +end function + +sub slices(nums() as integer) + dim sep as string, i as integer, j as integer, k as integer + + sep="" + print "("; + for i=0 to ubound(nums)-2 + for j=i+2 to ubound(nums) + if is_arithmetic(nums(), i, j) then + print sep; + sep="), (" + for k=i to j + print trim(str(nums(k))); + if k<>j then print ","; + next + end if + next + next + print ")" +end sub + +sub collect_args(nums() as integer) + dim i as integer + + i=0 + do while command(i+1)<>"" + redim preserve nums(i) + nums(i)=val(command(i+1)) + i=i+1 + loop +end sub + + +dim nums() as integer +collect_args nums() +slices nums() + diff --git a/challenge-200/paulo-custodio/basic/ch-2.bas b/challenge-200/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..f02f84a487 --- /dev/null +++ b/challenge-200/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,100 @@ +' Challenge 200 +' +' Task 2: Seven Segment 200 +' Submitted by: Ryan J Thompson +' A seven segment display is an electronic component, usually used to display +' digits. The segments are labeled 'a' through 'g' as shown: +' +' +' Seven Segment +' +' +' The encoding of each digit can thus be represented compactly as a truth table: +' +' my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>; +' For example, $truth[1] = ‘bc’. The digit 1 would have segments ‘b’ and ‘c’ enabled. +' +' Write a program that accepts any decimal number and draws that number as a +' horizontal sequence of ASCII seven segment displays, similar to the following: +' +' +' ------- ------- ------- +' | | | | | +' | | | | | +' ------- +' | | | | | +' | | | | | +' ------- ------- ------- +' To qualify as a seven segment display, each segment must be drawn (or not drawn) +' according to your @truth table. +' +' The number "200" was of course chosen to celebrate our 200th week! + +const a as integer = 1 +const b as integer = 2 +const c as integer = 4 +const d as integer = 8 +const e as integer = 16 +const f as integer = 32 +const g as integer = 64 + +dim shared truth(9) as integer +truth(0)=a+b+c+d+e+f +truth(1)=b+c +truth(2)=a+b+d+e+g +truth(3)=a+b+c+d+g +truth(4)=b+c+f+g +truth(5)=a+c+d+f+g +truth(6)=a+c+d+e+f+g +truth(7)=a+b+c +truth(8)=a+b+c+d+e+f+g +truth(9)=a+b+c+f+g + +sub draw_number(n as integer) + dim s as string, i as integer, j as integer, digit as integer + + s=trim(str(n)) + + ' draw a + for i=1 to len(s) + digit=val(mid(s,i,1)) + if (truth(digit) and a)=a then print " #### "; else print " "; + next + print + ' draw f, b + for j=1 to 2 + for i=1 to len(s) + digit=val(mid(s,i,1)) + if (truth(digit) and f)=f then print "#"; else print " "; + print " "; + if (truth(digit) and b)=b then print "#"; else print " "; + print " "; + next + print + next + ' draw g + for i=1 to len(s) + digit=val(mid(s,i,1)) + if (truth(digit) and g)=g then print " #### "; else print " "; + next + print + ' draw e, c + for j=1 to 2 + for i=1 to len(s) + digit=val(mid(s,i,1)) + if (truth(digit) and e)=e then print "#"; else print " "; + print " "; + if (truth(digit) and c)=c then print "#"; else print " "; + print " "; + next + print + next + ' draw d + for i=1 to len(s) + digit=val(mid(s,i,1)) + if (truth(digit) and d)=d then print " #### "; else print " "; + next + print +end sub + +draw_number(val(command(1))) diff --git a/challenge-200/paulo-custodio/c/ch-1.c b/challenge-200/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..b4ccf0fa84 --- /dev/null +++ b/challenge-200/paulo-custodio/c/ch-1.c @@ -0,0 +1,78 @@ +/* +Challenge 200 + +Task 1: Arithmetic Slices +Submitted by: Mohammad S Anwar +You are given an array of integers. + +Write a script to find out all Arithmetic Slices for the given array of integers. + +An integer array is called arithmetic if it has at least 3 elements and the +differences between any three consecutive elements are the same. + + +Example 1 +Input: @array = (1,2,3,4) +Output: (1,2,3), (2,3,4), (1,2,3,4) +Example 2 +Input: @array = (2) +Output: () as no slice found. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +bool is_arithmetic(int nums[], int nums_size) { + if (nums_size < 3) + return false; + int step = nums[1] - nums[0]; + for (int i = 2; i < nums_size; i++) { + if (nums[i] - nums[i-1] != step) + return false; + } + return true; +} + +void print_slices(int nums[], int nums_size) { + const char* sep = ""; + + printf("("); + for (int i = 0; i < nums_size-2; i++) { + for (int j = i+2; j < nums_size; j++) { + if (is_arithmetic(nums+i, j-i+1)) { + printf("%s", sep); + sep = "), ("; + for (int k = i; k <= j; k++) { + printf("%d", nums[k]); + if (k != j) + printf(","); + } + } + } + } + printf(")\n"); +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + fputs("usage: ch-1 nums...", stderr); + exit(EXIT_FAILURE); + } + + int* nums = check_mem(malloc(argc*sizeof(int))); + for (int i = 0; i < argc; i++) + nums[i] = atoi(argv[i]); + print_slices(nums, argc); + + free(nums); +} diff --git a/challenge-200/paulo-custodio/c/ch-2.c b/challenge-200/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..0fb239f00d --- /dev/null +++ b/challenge-200/paulo-custodio/c/ch-2.c @@ -0,0 +1,117 @@ +/* +Challenge 200 + +Task 2: Seven Segment 200 +Submitted by: Ryan J Thompson +A seven segment display is an electronic component, usually used to display +digits. The segments are labeled 'a' through 'g' as shown: + + +Seven Segment + + +The encoding of each digit can thus be represented compactly as a truth table: + +my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>; +For example, $truth[1] = ‘bc’. The digit 1 would have segments ‘b’ and ‘c’ enabled. + +Write a program that accepts any decimal number and draws that number as a +horizontal sequence of ASCII seven segment displays, similar to the following: + + +------- ------- ------- + | | | | | + | | | | | +------- +| | | | | +| | | | | +------- ------- ------- +To qualify as a seven segment display, each segment must be drawn (or not drawn) +according to your @truth table. + +The number "200" was of course chosen to celebrate our 200th week! +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define MAX_BUFFER 256 + +#define A 1 +#define B 2 +#define C 4 +#define D 8 +#define E 16 +#define F 32 +#define G 64 + +int truth[10] = { + A|B|C|D|E|F, B|C, A|B|D|E|G, A|B|C|D|G, B|C|F|G, + A|C|D|F|G, A|C|D|E|F|G, A|B|C, A|B|C|D|E|F|G, A|B|C|F|G +}; + +void draw_number(int n) { + char s[MAX_BUFFER]; + sprintf(s, "%d", n); + + // draw a + for (int i = 0; i < strlen(s); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & A)==A) + printf(" #### "); + else + printf(" "); + } + printf("\n"); + // draw f, b + for (int j = 0; j < 2; j++) { + for (int i = 0; i < strlen(s); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & F)==F) printf("#"); else printf(" "); + printf(" "); + if ((truth[digit] & B)==B) printf("#"); else printf(" "); + printf(" "); + } + printf("\n"); + } + // draw g + for (int i = 0; i < strlen(s); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & G)==G) + printf(" #### "); + else + printf(" "); + } + printf("\n"); + // draw e, c + for (int j = 0; j < 2; j++) { + for (int i = 0; i < strlen(s); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & E)==E) printf("#"); else printf(" "); + printf(" "); + if ((truth[digit] & C)==C) printf("#"); else printf(" "); + printf(" "); + } + printf("\n"); + } + // draw d + for (int i = 0; i < strlen(s); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & D)==D) + printf(" #### "); + else + printf(" "); + } + printf("\n"); +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + fputs("usage: ch-2 num", stderr); + exit(EXIT_FAILURE); + } + + draw_number(atoi(argv[0])); +} diff --git a/challenge-200/paulo-custodio/cpp/ch-1.cpp b/challenge-200/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..46948327c7 --- /dev/null +++ b/challenge-200/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,68 @@ +/* +Challenge 200 + +Task 1: Arithmetic Slices +Submitted by: Mohammad S Anwar +You are given an array of integers. + +Write a script to find out all Arithmetic Slices for the given array of integers. + +An integer array is called arithmetic if it has at least 3 elements and the +differences between any three consecutive elements are the same. + + +Example 1 +Input: @array = (1,2,3,4) +Output: (1,2,3), (2,3,4), (1,2,3,4) +Example 2 +Input: @array = (2) +Output: () as no slice found. +*/ + +#include <iostream> +#include <vector> + +bool is_arithmetic(int nums[], int nums_size) { + if (nums_size < 3) + return false; + int step = nums[1] - nums[0]; + for (int i = 2; i < nums_size; i++) { + if (nums[i] - nums[i-1] != step) + return false; + } + return true; +} + +void print_slices(std::vector<int> nums) { + const char* sep = ""; + int nums_size = static_cast<int>(nums.size()); + + std::cout << "("; + for (int i = 0; i < nums_size-2; i++) { + for (int j = i+2; j < nums_size; j++) { + if (is_arithmetic(&nums[i], j-i+1)) { + std::cout << sep; + sep = "), ("; + for (int k = i; k <= j; k++) { + std::cout << nums[k]; + if (k != j) + std::cout << ","; + } + } + } + } + std::cout << ")" << std::endl; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr << "usage: ch-1 nums..." << std::endl; + exit(EXIT_FAILURE); + } + + std::vector<int> nums; + for (int i = 0; i < argc; i++) + nums.push_back(atoi(argv[i])); + print_slices(nums); +} diff --git a/challenge-200/paulo-custodio/cpp/ch-2.cpp b/challenge-200/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..d7ae53b788 --- /dev/null +++ b/challenge-200/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,113 @@ +/* +Challenge 200 + +Task 2: Seven Segment 200 +Submitted by: Ryan J Thompson +A seven segment display is an electronic component, usually used to display +digits. The segments are labeled 'a' through 'g' as shown: + + +Seven Segment + + +The encoding of each digit can thus be represented compactly as a truth table: + +my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>; +For example, $truth[1] = ‘bc’. The digit 1 would have segments ‘b’ and ‘c’ enabled. + +Write a program that accepts any decimal number and draws that number as a +horizontal sequence of ASCII seven segment displays, similar to the following: + + +------- ------- ------- + | | | | | + | | | | | +------- +| | | | | +| | | | | +------- ------- ------- +To qualify as a seven segment display, each segment must be drawn (or not drawn) +according to your @truth table. + +The number "200" was of course chosen to celebrate our 200th week! +*/ + +#include <iostream> +#include <string> + +#define A 1 +#define B 2 +#define C 4 +#define D 8 +#define E 16 +#define F 32 +#define G 64 + +int truth[10] = { + A|B|C|D|E|F, B|C, A|B|D|E|G, A|B|C|D|G, B|C|F|G, + A|C|D|F|G, A|C|D|E|F|G, A|B|C, A|B|C|D|E|F|G, A|B|C|F|G +}; + +void draw_number(int n) { + std::string s = std::to_string(n); + + // draw a + for (size_t i = 0; i < s.size(); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & A)==A) + std::cout << " #### "; + else + std::cout << " "; + } + std::cout << std::endl; + // draw f, b + for (int j = 0; j < 2; j++) { + for (size_t i = 0; i < s.size(); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & F)==F) std::cout << "#"; else std::cout << " "; + std::cout << " "; + if ((truth[digit] & B)==B) std::cout << "#"; else std::cout << " "; + std::cout << " "; + } + std::cout << std::endl; + } + // draw g + for (size_t i = 0; i < s.size(); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & G)==G) + std::cout << " #### "; + else + std::cout << " "; + } + std::cout << std::endl; + // draw e, c + for (int j = 0; j < 2; j++) { + for (size_t i = 0; i < s.size(); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & E)==E) std::cout << "#"; else std::cout << " "; + std::cout << " "; + if ((truth[digit] & C)==C) std::cout << "#"; else std::cout << " "; + std::cout << " "; + } + std::cout << std::endl; + } + // draw d + for (size_t i = 0; i < s.size(); i++) { + int digit = s[i] - '0'; + if ((truth[digit] & D)==D) + std::cout << " #### "; + else + std::cout << " "; + } + std::cout << std::endl; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr << "usage: ch-2 num" << std::endl; + exit(EXIT_FAILURE); + } + + draw_number(atoi(argv[0])); +} diff --git a/challenge-200/paulo-custodio/forth/ch-1.fs b/challenge-200/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..d01f35cbdb --- /dev/null +++ b/challenge-200/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,70 @@ +#! /usr/bin/env gforth + +\ Challenge 200 +\ +\ Task 1: Arithmetic Slices +\ Submitted by: Mohammad S Anwar +\ You are given an array of integers. +\ +\ Write a script to find out all Arithmetic Slices for the given array of integers. +\ +\ An integer array is called arithmetic if it has at least 3 elements and the +\ differences between any three consecutive elements are the same. +\ +\ +\ Example 1 +\ Input: @array = (1,2,3,4) +\ Output: (1,2,3), (2,3,4), (1,2,3,4) +\ Example 2 +\ Input: @array = (2) +\ Output: () as no slice found. + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +: nums[] ( i -- addr ) + CELLS nums + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: is_arithmetic { idx num -- f } + idx 1+ nums[] @ idx nums[] @ - { step } + TRUE ( f ) + num 1 DO + idx I + nums[] @ idx I + 1- nums[] @ - step <> IF + DROP FALSE LEAVE + THEN + LOOP +; + +: print_slices ( -- ) + FALSE { sep } + + ." (" + nums_size 2 > IF + nums_size 2 - 0 DO + nums_size I 2 + DO + J I J - 1+ is_arithmetic IF + sep IF ." ), (" THEN TRUE TO sep + I 1+ J DO + I nums[] @ . + I J <> IF ." ," THEN + LOOP + THEN + LOOP + LOOP + THEN + . |
