From 48cb0f7ba25f14244c608c027774fc15feddb11c Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 11 Mar 2023 22:43:15 +0000 Subject: Add Perl, C, C++ and Forth solutions --- challenge-017/paulo-custodio/perl/ch-2.pl | 2 +- challenge-017/paulo-custodio/python/ch-2.py | 2 +- challenge-017/paulo-custodio/t/test-2.yaml | 24 +++--- challenge-093/paulo-custodio/Makefile | 2 + challenge-093/paulo-custodio/c/ch-1.c | 10 ++- challenge-093/paulo-custodio/c/ch-2.c | 12 ++- challenge-098/paulo-custodio/Makefile | 2 + challenge-100/paulo-custodio/Makefile | 2 + challenge-200/paulo-custodio/Makefile | 2 + challenge-200/paulo-custodio/basic/ch-1.bas | 72 ++++++++++++++++ challenge-200/paulo-custodio/basic/ch-2.bas | 100 +++++++++++++++++++++++ challenge-200/paulo-custodio/c/ch-1.c | 78 ++++++++++++++++++ challenge-200/paulo-custodio/c/ch-2.c | 117 ++++++++++++++++++++++++++ challenge-200/paulo-custodio/cpp/ch-1.cpp | 68 ++++++++++++++++ challenge-200/paulo-custodio/cpp/ch-2.cpp | 113 ++++++++++++++++++++++++++ challenge-200/paulo-custodio/forth/ch-1.fs | 70 ++++++++++++++++ challenge-200/paulo-custodio/forth/ch-2.fs | 122 ++++++++++++++++++++++++++++ challenge-200/paulo-custodio/perl/ch-1.pl | 55 +++++++++++++ challenge-200/paulo-custodio/perl/ch-2.pl | 82 +++++++++++++++++++ challenge-200/paulo-custodio/t/test-1.yaml | 15 ++++ challenge-200/paulo-custodio/t/test-2.yaml | 12 +++ challenge-201/paulo-custodio/basic/ch-1.bas | 51 ++++++++++++ challenge-201/paulo-custodio/basic/ch-2.bas | 48 +++++++++++ challenge-201/paulo-custodio/c/ch-1.c | 66 +++++++++++++++ challenge-201/paulo-custodio/c/ch-2.c | 53 ++++++++++++ challenge-201/paulo-custodio/cpp/ch-1.cpp | 53 ++++++++++++ challenge-201/paulo-custodio/cpp/ch-2.cpp | 52 ++++++++++++ challenge-201/paulo-custodio/forth/ch-1.fs | 61 ++++++++++++++ challenge-201/paulo-custodio/forth/ch-2.fs | 47 +++++++++++ challenge-201/paulo-custodio/perl/ch-2.pl | 1 - challenge-202/paulo-custodio/c/ch-1.c | 10 ++- challenge-202/paulo-custodio/c/ch-2.c | 10 ++- challenge-203/paulo-custodio/c/ch-1.c | 10 ++- challenge-205/paulo-custodio/c/ch-1.c | 10 ++- challenge-205/paulo-custodio/c/ch-2.c | 10 ++- challenge-206/paulo-custodio/c/ch-1.c | 10 ++- challenge-206/paulo-custodio/c/ch-2.c | 14 +++- 37 files changed, 1441 insertions(+), 27 deletions(-) create mode 100644 challenge-093/paulo-custodio/Makefile create mode 100644 challenge-098/paulo-custodio/Makefile create mode 100644 challenge-100/paulo-custodio/Makefile create mode 100644 challenge-200/paulo-custodio/Makefile create mode 100644 challenge-200/paulo-custodio/basic/ch-1.bas create mode 100644 challenge-200/paulo-custodio/basic/ch-2.bas create mode 100644 challenge-200/paulo-custodio/c/ch-1.c create mode 100644 challenge-200/paulo-custodio/c/ch-2.c create mode 100644 challenge-200/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-200/paulo-custodio/cpp/ch-2.cpp create mode 100644 challenge-200/paulo-custodio/forth/ch-1.fs create mode 100644 challenge-200/paulo-custodio/forth/ch-2.fs create mode 100644 challenge-200/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-200/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-200/paulo-custodio/t/test-1.yaml create mode 100644 challenge-200/paulo-custodio/t/test-2.yaml create mode 100644 challenge-201/paulo-custodio/basic/ch-1.bas create mode 100644 challenge-201/paulo-custodio/basic/ch-2.bas create mode 100644 challenge-201/paulo-custodio/c/ch-1.c create mode 100644 challenge-201/paulo-custodio/c/ch-2.c create mode 100644 challenge-201/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-201/paulo-custodio/cpp/ch-2.cpp create mode 100644 challenge-201/paulo-custodio/forth/ch-1.fs create mode 100644 challenge-201/paulo-custodio/forth/ch-2.fs 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{^ (? $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 #include +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; +' 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 +#include +#include + +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; +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 +#include +#include + +#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 +#include + +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 nums) { + const char* sep = ""; + int nums_size = static_cast(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 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; +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 +#include + +#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 + ." )" CR +; + + +collect_args print_slices BYE + diff --git a/challenge-200/paulo-custodio/forth/ch-2.fs b/challenge-200/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..485e8f03ba --- /dev/null +++ b/challenge-200/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,122 @@ +#! /usr/bin/env gforth + +\ 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; +\ 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! + +1 CONSTANT A +2 CONSTANT B +4 CONSTANT C +8 CONSTANT D +16 CONSTANT E +32 CONSTANT F +64 CONSTANT G + +: | OR ; + +CREATE truth +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 | , + +: has_segment { n seg -- f } + n 10 MOD CELLS truth + @ seg AND seg = +; + +: num_to_str ( n -- addr size ) + 0 <# #S #> +; + +: digit_mask ( c -- mask ) + '0' - CELLS truth + @ +; + +: draw_horizontal { num size mask -- } + num size BOUNDS DO + I C@ digit_mask mask AND mask = IF ." #### " ELSE 6 SPACES THEN + 2 SPACES + LOOP + CR +; + +: draw_vertical { num size mask1 mask2 } + 2 0 DO + num size BOUNDS DO + I C@ digit_mask + DUP mask1 AND mask1 = IF '#' EMIT ELSE SPACE THEN + 4 SPACES + mask2 AND mask2 = IF '#' EMIT ELSE SPACE THEN + 2 SPACES + LOOP + CR + LOOP +; + +: draw_a ( num size -- ) + A draw_horizontal +; + +: draw_f_b ( num size -- ) + F B draw_vertical +; + +: draw_g ( num size -- ) + G draw_horizontal +; + +: draw_e_c ( num size -- ) + E C draw_vertical +; + +: draw_d ( num size -- ) + D draw_horizontal +; + +: draw_number ( n -- ) + num_to_str { num size } + num size draw_a + num size draw_f_b + num size draw_g + num size draw_e_c + num size draw_d +; + + +NEXT-ARG S>NUMBER? 0= THROW DROP +draw_number +BYE diff --git a/challenge-200/paulo-custodio/perl/ch-1.pl b/challenge-200/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..73363b02ac --- /dev/null +++ b/challenge-200/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl + +# 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. + +use Modern::Perl; + +sub is_arithmetic { + my(@in) = @_; + return 0 if @in < 3; + my $step = $in[1]-$in[0]; + for my $i (2..$#in) { + return 0 if $in[$i]-$in[$i-1] != $step; + } + return 1; +} + +sub slices { + my(@nums) = @_; + my @out; + for my $i (0 .. $#nums-2) { + for my $j ($i+2 .. $#nums) { + my @slice = @nums[$i..$j]; + if (is_arithmetic(@slice)) { + push @out, \@slice; + } + } + } + return @out; +} + +my @in = @ARGV; +my @out = slices(@in); +if (!@out) { + say "()"; +} +else { + say join(", ", map {"(".join(",", @$_).")"} @out); +} diff --git a/challenge-200/paulo-custodio/perl/ch-2.pl b/challenge-200/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1147f45cb4 --- /dev/null +++ b/challenge-200/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +# 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; +# 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! + +use Modern::Perl; + +my @truth = qw; + +sub draw_number { + my($num) = @_; + my @num = split(//, $num); + + # draw a + for my $digit (@num) { + if ($truth[$digit] =~ /a/) { print " #### "; } else { print " "; } + } + print "\n"; + # draw f, b + for (1..2) { + for my $digit (@num) { + if ($truth[$digit] =~ /f/) { print "#"; } else { print " "; } + print " "; + if ($truth[$digit] =~ /b/) { print "#"; } else { print " "; } + print " "; + } + print "\n"; + } + # draw g + for my $digit (@num) { + if ($truth[$digit] =~ /g/) { print " #### "; } else { print " "; } + } + print "\n"; + # draw e, c + for (1..2) { + for my $digit (@num) { + if ($truth[$digit] =~ /e/) { print "#"; } else { print " "; } + print " "; + if ($truth[$digit] =~ /c/) { print "#"; } else { print " "; } + print " "; + } + print "\n"; + } + # draw d + for my $digit (@num) { + if ($truth[$digit] =~ /d/) { print " #### "; } else { print " "; } + } + print "\n"; +} + +draw_number(shift || 0); + + diff --git a/challenge-200/paulo-custodio/t/test-1.yaml b/challenge-200/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..72be87085b --- /dev/null +++ b/challenge-200/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 3 4 + input: + output: (1,2,3), (1,2,3,4), (2,3,4) +- setup: + cleanup: + args: 2 + input: + output: () +- setup: + cleanup: + args: 1 2 4 6 7 + input: + output: (2,4,6) diff --git a/challenge-200/paulo-custodio/t/test-2.yaml b/challenge-200/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f2be1e9b7a --- /dev/null +++ b/challenge-200/paulo-custodio/t/test-2.yaml @@ -0,0 +1,12 @@ +- setup: + cleanup: + args: 1234567890 + input: + output: | + | #### #### #### #### #### #### #### #### + | # # # # # # # # # # # # # # + | # # # # # # # # # # # # # # + | #### #### #### #### #### #### #### + | # # # # # # # # # # # # # + | # # # # # # # # # # # # # + | #### #### #### #### #### #### diff --git a/challenge-201/paulo-custodio/basic/ch-1.bas b/challenge-201/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..bf4291e1e5 --- /dev/null +++ b/challenge-201/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,51 @@ +' Challenge 201 +' +' Task 1: Missing Numbers +' Submitted by: Mohammad S Anwar +' +' You are given an array of unique numbers. +' +' Write a script to find out all missing numbers in the range 0..$n where $n +' is the array size. +' +' Example 1 +' +' Input: @array = (0,1,3) +' Output: 2 +' +' The array size i.e. total element count is 3, so the range is 0..3. +' The missing number is 2 in the given array. +' +' Example 2 +' +' Input: @array = (0,1) +' Output: 2 +' +' The array size is 2, therefore the range is 0..2. +' The missing number is 2. + +sub collect_args(nums() as integer) + dim i as integer + i=0 + do while command(i+1)<>"" + redim preserve nums(i) as integer + nums(i)=val(command(i+1)) + i=i+1 + loop +end sub + +dim nums() as integer, got() as boolean, i as integer + +collect_args nums() +for i=0 to ubound(nums) + if nums(i)>ubound(got) then redim preserve got(nums(i)) + got(nums(i))=true +next +for i=0 to ubound(nums)+1 + if i>ubound(got) then + print i; + elseif not got(i) then + print i; + end if +next +print diff --git a/challenge-201/paulo-custodio/basic/ch-2.bas b/challenge-201/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..9f6a8290a4 --- /dev/null +++ b/challenge-201/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,48 @@ +' Challenge 201 +' +' Task 2: Penny Piles +' Submitted by: Robbie Hatley +' +' You are given an integer, $n > 0. +' +' Write a script to determine the number of ways of putting $n pennies in a row +' of piles of ascending heights from left to right. +' Example +' +' Input: $n = 5 +' Output: 7 +' +' Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +' +' 1 1 1 1 1 +' 1 1 1 2 +' 1 2 2 +' 1 1 3 +' 2 3 +' 1 4 +' 5 + +function make_piles1(prev as integer, n as integer) as integer + dim count as integer, i as integer, max as integer + + count=0 + if n=0 then + count=count+1 + elseif n>0 then + if prev<0 then + max=n + else + max=prev + endif + for i=1 to max + count=count+make_piles1(i, n-i) + next + end if + make_piles1=count +end function + +function make_piles(n as integer) as integer + make_piles=make_piles1(-1,n) +end function + +print make_piles(val(command(1))) diff --git a/challenge-201/paulo-custodio/c/ch-1.c b/challenge-201/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..2f2d1b1c64 --- /dev/null +++ b/challenge-201/paulo-custodio/c/ch-1.c @@ -0,0 +1,66 @@ +/* +Challenge 201 + +Task 1: Missing Numbers +Submitted by: Mohammad S Anwar + +You are given an array of unique numbers. + +Write a script to find out all missing numbers in the range 0..$n where $n +is the array size. + +Example 1 + +Input: @array = (0,1,3) +Output: 2 + +The array size i.e. total element count is 3, so the range is 0..3. +The missing number is 2 in the given array. + +Example 2 + +Input: @array = (0,1) +Output: 2 + +The array size is 2, therefore the range is 0..2. +The missing number is 2. +*/ + +#include +#include + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int compare(const void*a, const void* b) { + return *(int*)a - *(int*)b; +} + +void print_missing(int nums[], int nums_size) { + qsort(nums, nums_size, sizeof(int), compare); + for (int i = 0; i <= nums_size; i++) { // check 0..N + if (bsearch(&i, nums, nums_size, sizeof(int), compare) == NULL) + printf("%d ", i); + } + 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_missing(nums, argc); + + free(nums); +} diff --git a/challenge-201/paulo-custodio/c/ch-2.c b/challenge-201/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..b3587f47f5 --- /dev/null +++ b/challenge-201/paulo-custodio/c/ch-2.c @@ -0,0 +1,53 @@ +/* +Challenge 201 + +Task 2: Penny Piles +Submitted by: Robbie Hatley + +You are given an integer, $n > 0. + +Write a script to determine the number of ways of putting $n pennies in a row +of piles of ascending heights from left to right. +Example + +Input: $n = 5 +Output: 7 + +Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: + + 1 1 1 1 1 + 1 1 1 2 + 1 2 2 + 1 1 3 + 2 3 + 1 4 + 5 +*/ + +#include +#include + +int make_piles1(int prev, int n) { + int count = 0; + if (n == 0) + count++; + else if (n > 0) { + int max = (prev < 0) ? n : prev; + for (int i = 1; i <= max; i++) + count += make_piles1(i, n-i); + } + return count; +} + +int make_piles(int n) { + return make_piles1(-1, n); +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + fputs("usage: ch-2 N", stderr); + exit(EXIT_FAILURE); + } + + printf("%d\n", make_piles(atoi(argv[1]))); +} diff --git a/challenge-201/paulo-custodio/cpp/ch-1.cpp b/challenge-201/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..88557d44f7 --- /dev/null +++ b/challenge-201/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,53 @@ +/* +Challenge 201 + +Task 1: Missing Numbers +Submitted by: Mohammad S Anwar + +You are given an array of unique numbers. + +Write a script to find out all missing numbers in the range 0..$n where $n +is the array size. + +Example 1 + +Input: @array = (0,1,3) +Output: 2 + +The array size i.e. total element count is 3, so the range is 0..3. +The missing number is 2 in the given array. + +Example 2 + +Input: @array = (0,1) +Output: 2 + +The array size is 2, therefore the range is 0..2. +The missing number is 2. +*/ + +#include +#include +#include + +void print_missing(std::vector& nums) { + std::sort(nums.begin(), nums.end()); + for (size_t i = 0; i <= nums.size(); i++) { // check 0..N + if (!std::binary_search(nums.begin(), nums.end(), i)) + std::cout << i << " "; + } + 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 nums; + for (int i = 0; i < argc; i++) + nums.push_back(atoi(argv[i])); + print_missing(nums); +} diff --git a/challenge-201/paulo-custodio/cpp/ch-2.cpp b/challenge-201/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..9eb7e7a8e7 --- /dev/null +++ b/challenge-201/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,52 @@ +/* +Challenge 201 + +Task 2: Penny Piles +Submitted by: Robbie Hatley + +You are given an integer, $n > 0. + +Write a script to determine the number of ways of putting $n pennies in a row +of piles of ascending heights from left to right. +Example + +Input: $n = 5 +Output: 7 + +Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: + + 1 1 1 1 1 + 1 1 1 2 + 1 2 2 + 1 1 3 + 2 3 + 1 4 + 5 +*/ + +#include + +int make_piles1(int prev, int n) { + int count = 0; + if (n == 0) + count++; + else if (n > 0) { + int max = (prev < 0) ? n : prev; + for (int i = 1; i <= max; i++) + count += make_piles1(i, n-i); + } + return count; +} + +int make_piles(int n) { + return make_piles1(-1, n); +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "usage: ch-2 N" << std::endl; + exit(EXIT_FAILURE); + } + + std::cout << make_piles(atoi(argv[1])) << std::endl; +} diff --git a/challenge-201/paulo-custodio/forth/ch-1.fs b/challenge-201/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..343135c81e --- /dev/null +++ b/challenge-201/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,61 @@ +#! /usr/bin/env gforth + +\ Challenge 201 +\ +\ Task 1: Missing Numbers +\ Submitted by: Mohammad S Anwar +\ +\ You are given an array of unique numbers. +\ +\ Write a script to find out all missing numbers in the range 0..$n where $n +\ is the array size. +\ +\ Example 1 +\ +\ Input: @array = (0,1,3) +\ Output: 2 +\ +\ The array size i.e. total element count is 3, so the range is 0..3. +\ The missing number is 2 in the given array. +\ +\ Example 2 +\ +\ Input: @array = (0,1) +\ Output: 2 +\ +\ The array size is 2, therefore the range is 0..2. +\ The missing number is 2. + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +: nums[] ( i -- addr ) + CELLS nums + +; + +: find_nums { n -- f } + FALSE ( f ) + nums_size 0 DO + I nums[] @ n = IF + DROP TRUE LEAVE + THEN + LOOP +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: .missing ( -- ) + nums_size 1+ 0 DO + I find_nums 0= IF I . THEN + LOOP + CR +; + +collect_args .missing BYE diff --git a/challenge-201/paulo-custodio/forth/ch-2.fs b/challenge-201/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..cabb49b33e --- /dev/null +++ b/challenge-201/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,47 @@ +#! /usr/bin/env gforth + +\ Challenge 201 +\ +\ Task 2: Penny Piles +\ Submitted by: Robbie Hatley +\ +\ You are given an integer, $n > 0. +\ +\ Write a script to determine the number of ways of putting $n pennies in a row +\ of piles of ascending heights from left to right. +\ Example +\ +\ Input: $n = 5 +\ Output: 7 +\ +\ Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +\ +\ 1 1 1 1 1 +\ 1 1 1 2 +\ 1 2 2 +\ 1 1 3 +\ 2 3 +\ 1 4 +\ 5 + +: make_piles1 { prev n -- count } + 0 ( count ) + n 0= IF + 1+ \ count++ + ELSE n 0 > IF + prev 0 < IF n ELSE prev THEN 1+ + 1 DO + I n I - RECURSE + + + LOOP + THEN THEN +; + +: make_piles ( n ) + -1 SWAP make_piles1 +; + + +NEXT-ARG DUP 0= THROW 0 0 2SWAP >NUMBER 2DROP DROP \ get n +make_piles . CR +BYE diff --git a/challenge-201/paulo-custodio/perl/ch-2.pl b/challenge-201/paulo-custodio/perl/ch-2.pl index e4e9fef4b8..f7ce4d9e62 100644 --- a/challenge-201/paulo-custodio/perl/ch-2.pl +++ b/challenge-201/paulo-custodio/perl/ch-2.pl @@ -25,7 +25,6 @@ # 5 use Modern::Perl; -no warnings 'recursion'; sub make_piles1 { my($count, $prev, $n) = @_; diff --git a/challenge-202/paulo-custodio/c/ch-1.c b/challenge-202/paulo-custodio/c/ch-1.c index dc8f770f46..fc85e22ae3 100644 --- a/challenge-202/paulo-custodio/c/ch-1.c +++ b/challenge-202/paulo-custodio/c/ch-1.c @@ -34,6 +34,14 @@ Output: 1 #include #include +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + bool is_odd(int n) { return n%2==1; } @@ -53,7 +61,7 @@ int main(int argc, char* argv[]) { } int nums_size = argc; - int* nums = malloc(nums_size * sizeof(int)); + int* nums = check_mem(malloc(nums_size * sizeof(int))); for (int i = 0; i < nums_size; i++) nums[i] = atoi(argv[i]); diff --git a/challenge-202/paulo-custodio/c/ch-2.c b/challenge-202/paulo-custodio/c/ch-2.c index 09e61bdefe..424b418810 100644 --- a/challenge-202/paulo-custodio/c/ch-2.c +++ b/challenge-202/paulo-custodio/c/ch-2.c @@ -39,6 +39,14 @@ Output: 3, 3, 2, 1, 2, 3, 3 #include #include +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + int largest_valey(int heights[], int heights_size) { int l1 = 0, r1 = -1; // initial interval @@ -72,7 +80,7 @@ int main(int argc, char* argv[]) { } int heights_size = argc; - int* heights = malloc(heights_size * sizeof(int)); + int* heights = check_mem(malloc(heights_size * sizeof(int))); for (int i = 0; i < heights_size; i++) heights[i] = atoi(argv[i]); diff --git a/challenge-203/paulo-custodio/c/ch-1.c b/challenge-203/paulo-custodio/c/ch-1.c index d4926a7d1a..808555c55a 100644 --- a/challenge-203/paulo-custodio/c/ch-1.c +++ b/challenge-203/paulo-custodio/c/ch-1.c @@ -40,6 +40,14 @@ Output: 0 #include #include +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + int num_quadruplets(int nums[], int nums_size) { int count = 0; for (int a = 0; a < nums_size - 3; a++) @@ -53,7 +61,7 @@ int num_quadruplets(int nums[], int nums_size) { int main(int argc, char* argv[]) { argv++; argc--; - int* nums = malloc(argc*sizeof(int)); + int* nums = check_mem(malloc(argc*sizeof(int))); for (int i = 0; i < argc; i++) nums[i] = atoi(argv[i]); printf("%d\n", num_quadruplets(nums, argc)); diff --git a/challenge-205/paulo-custodio/c/ch-1.c b/challenge-205/paulo-custodio/c/ch-1.c index d5243c079c..45be7b56ee 100644 --- a/challenge-205/paulo-custodio/c/ch-1.c +++ b/challenge-205/paulo-custodio/c/ch-1.c @@ -32,6 +32,14 @@ First highest is 5. Second highest is 4. Third highest is 3. #include #include +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + int compare_desc(const void* a, const void* b) { return *(int*)b - *(int*)a; } @@ -42,7 +50,7 @@ int main(int argc, char* argv[]) { if (argc > 0) { int num_items = argc; - int* items = malloc(num_items * sizeof(int)); + int* items = check_mem(malloc(num_items * sizeof(int))); // parse input numbers for (int i = 0; i < num_items; i++) diff --git a/challenge-205/paulo-custodio/c/ch-2.c b/challenge-205/paulo-custodio/c/ch-2.c index 78c8f07ab1..8c2c84b9a0 100644 --- a/challenge-205/paulo-custodio/c/ch-2.c +++ b/challenge-205/paulo-custodio/c/ch-2.c @@ -32,13 +32,21 @@ The maximum result of 10 xor 5 = 15. #include #include +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + int main(int argc, char* argv[]) { argv++; argc--; int result = -1; if (argc > 0) { int num_items = argc; - int* items = malloc(num_items * sizeof(int)); + int* items = check_mem(malloc(num_items * sizeof(int))); // parse input numbers for (int i = 0; i < num_items; i++) diff --git a/challenge-206/paulo-custodio/c/ch-1.c b/challenge-206/paulo-custodio/c/ch-1.c index 10e7e64ece..9d583896b7 100644 --- a/challenge-206/paulo-custodio/c/ch-1.c +++ b/challenge-206/paulo-custodio/c/ch-1.c @@ -28,6 +28,14 @@ Output: 15 #include #include +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + int minutes(const char* str) { int hours = 0, minutes = 0; if (sscanf(str, "%d:%d", &hours, &minutes) == 2) @@ -48,7 +56,7 @@ int main(int argc, char* argv[]) { } int num_items = argc+1; - int* items = malloc(num_items * sizeof(int)); + int* items = check_mem(malloc(num_items * sizeof(int))); for (int i = 0; i < num_items-1; i++) items[i] = minutes(argv[i]); qsort(items, num_items-1, sizeof(int), compare); diff --git a/challenge-206/paulo-custodio/c/ch-2.c b/challenge-206/paulo-custodio/c/ch-2.c index 79e72feeb5..aa72e3f4b3 100644 --- a/challenge-206/paulo-custodio/c/ch-2.c +++ b/challenge-206/paulo-custodio/c/ch-2.c @@ -45,12 +45,20 @@ typedef struct { int* items; } IList; +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + IList* ilist_new(size_t count) { - IList* list = calloc(1, sizeof(IList)); + IList* list = check_mem(calloc(1, sizeof(IList))); assert(list); if (count) { list->count = count; - list->items = calloc(count, sizeof(int)); + list->items = check_mem(calloc(count, sizeof(int))); assert(list->items); } return list; @@ -63,7 +71,7 @@ void ilist_free(IList* list) { void ilist_push(IList* list, int elem) { list->count++; - list->items = realloc(list->items, list->count * sizeof(int)); + list->items = check_mem(realloc(list->items, list->count * sizeof(int))); assert(list->items); list->items[list->count - 1] = elem; } -- cgit