diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-11 23:20:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-11 23:20:09 +0000 |
| commit | 993b5cfec02444f8ab158507ea64f42bcd3d175b (patch) | |
| tree | 3678bcde9af7c2fb9a2fcb9f8cd5deefd17f59c2 /challenge-200 | |
| parent | 51465a4186e14e5f9ae26e7f5b20dbb9f5ea8aae (diff) | |
| parent | 0ad64a5e5a740655432a36b2af0c35760289cf89 (diff) | |
| download | perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.gz perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.bz2 perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.zip | |
Merge pull request #7709 from pauloscustodio/master
Add Perl, C, C++ and Forth solutions
Diffstat (limited to 'challenge-200')
| -rw-r--r-- | challenge-200/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/basic/ch-1.bas | 72 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/basic/ch-2.bas | 100 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/c/ch-1.c | 78 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/c/ch-2.c | 117 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/cpp/ch-1.cpp | 68 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/cpp/ch-2.cpp | 113 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/forth/ch-1.fs | 70 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/forth/ch-2.fs | 122 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/perl/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/perl/ch-2.pl | 82 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/t/test-2.yaml | 12 |
13 files changed, 906 insertions, 0 deletions
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 + ." )" 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<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! + +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<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! + +use Modern::Perl; + +my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>; + +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: | + | #### #### #### #### #### #### #### #### + | # # # # # # # # # # # # # # + | # # # # # # # # # # # # # # + | #### #### #### #### #### #### #### + | # # # # # # # # # # # # # + | # # # # # # # # # # # # # + | #### #### #### #### #### #### |
