From 54d1683f44cc41d6b6240308cca6fd12cb373d34 Mon Sep 17 00:00:00 2001 From: Pip Date: Wed, 18 Jan 2023 21:26:36 -0600 Subject: Pip Stuart's submission for challenge-200. --- challenge-200/pip/perl/ch-1.pl | 35 ++++++++++++++++++++++++++++++++ challenge-200/pip/perl/ch-2.pl | 44 ++++++++++++++++++++++++++++++++++++++++ challenge-200/pip/raku/ch-1.raku | 35 ++++++++++++++++++++++++++++++++ challenge-200/pip/raku/ch-2.raku | 44 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 challenge-200/pip/perl/ch-1.pl create mode 100644 challenge-200/pip/perl/ch-2.pl create mode 100644 challenge-200/pip/raku/ch-1.raku create mode 100644 challenge-200/pip/raku/ch-2.raku diff --git a/challenge-200/pip/perl/ch-1.pl b/challenge-200/pip/perl/ch-1.pl new file mode 100644 index 0000000000..cef7ce9abb --- /dev/null +++ b/challenge-200/pip/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #1 of Week #200 - Pip Stuart +# Arithmetic Slices: 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. +# Example1: +# In-put: @array = (1,2,3,4) +# Output: (1,2,3), (2,3,4), (1,2,3,4) +# Example2: +# In-put: @array = (2) +# Output: () as no slice found. +# +# +use strict;use warnings;use utf8;use v5.10;my $d8VS='N1HLNEko'; +sub ASlc {my @aray=@_;my @slcz; + print '(' . sprintf("%-7s",join(',',@aray)) . ') => '; + for (my $i= 0;$i < @aray -2;$i++) { + if ( $aray[$i+1]-$aray[$i] == $aray[$i+2]-$aray[$i+1] ) { + push(@slcz,"($aray[$i],$aray[$i+1],$aray[$i+2])"); + my $msiz=2; # just loop from 3-leaves to longest trunk + while (@aray > $i+$msiz && $aray[$i+1]-$aray[$i] == $aray[$i+$msiz]-$aray[$i+$msiz-1]) { $msiz++; } + if ($msiz > 3) {my $slic='(' . $aray[$i]; + for (1..$msiz-1) { $slic .= ',' . $aray[$i+$_]; } + $slic .= ')'; + push(@slcz,$slic); + } } } + if (@slcz) { say join(', ',@slcz); } + else { say '() as no slice found.'; } + return(@slcz); +} +if (@ARGV) { + ASlc(@ARGV); +} else { + ASlc(1,2,3,4); # => (1,2,3), (1,2,3,4), (2,3,4) + ASlc(2 ); # => () as no slice found. +} diff --git a/challenge-200/pip/perl/ch-2.pl b/challenge-200/pip/perl/ch-2.pl new file mode 100644 index 0000000000..17cb5b284c --- /dev/null +++ b/challenge-200/pip/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #2 of Week #200 - Pip Stuart +# Seven Segment 200: A seven segment display is an electronic component, usually used to display digits. The segments are labeled 'a' through 'g' as shown: +# Seven Segment image with a starting top && bcdef wrapping around clockwise then g barring the middle. +# 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: +# ------- ------- ------- +# | | | | | +# | | | | | +# ------- # Note: 7-bit ASCII, 7-segment display, 7*7 text character cells +# | | | | | +# | | | | | +# ------- ------- ------- +# 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! +# Last date to submit the solution 23:59 (UK Time) Sunday 22nd January 2023. +# +# +use strict;use warnings;use utf8;use v5.10;my $d8VS='N1ILL1cG'; +sub Seg7 {my $iint=shift(@_);my @tout; + my @trth = qw; + my @digz = split('',$iint); + for my $digt (0..$#digz) { my $dtru=$trth[$digz[$digt]]; + my @char=();push(@char,' ' x 8) for (0..6); + if ($dtru =~ /a/) { substr($char[0],0,7,'-' x 7); } + if ($dtru =~ /b/) { substr($char[1],6,1,'|'); substr($char[2],6,1,'|'); } + if ($dtru =~ /c/) { substr($char[4],6,1,'|'); substr($char[5],6,1,'|'); } + if ($dtru =~ /d/) { substr($char[6],0,7,'-' x 7); } + if ($dtru =~ /e/) { substr($char[4],0,1,'|'); substr($char[5],0,1,'|'); } + if ($dtru =~ /f/) { substr($char[1],0,1,'|'); substr($char[2],0,1,'|'); } + if ($dtru =~ /g/) { substr($char[3],0,7,'-' x 7); } + for my $line (0..6) { $tout[$line] .= $char[$line]; } + } + say join("\n",@tout); + return($iint); +} +if (@ARGV) { + Seg7(@ARGV); +} else { + Seg7( 200 ); + Seg7(765432); +} diff --git a/challenge-200/pip/raku/ch-1.raku b/challenge-200/pip/raku/ch-1.raku new file mode 100644 index 0000000000..6f386dc15b --- /dev/null +++ b/challenge-200/pip/raku/ch-1.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #1 of Week #200 - Pip Stuart +# Arithmetic Slices: 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. +# Example1: +# In-put: @array = (1,2,3,4) +# Output: (1,2,3), (2,3,4), (1,2,3,4) +# Example2: +# In-put: @array = (2) +# Output: () as no slice found. +# Note: I've just started learning Raku about a week ago so this is one of my first Raku scripts. I expect my code will typically resemble my probably around +# intermediate skill-level of Perl5. Just getting stuff to work is cool but then trying to write proper Raku that diverges further is challenging; -Pip +use v6;my $d8VS='N1ILG09J'; +sub ASlc {my @aray=@_;my @slcz; + print '(' ~ sprintf("%-7s",join(',',@aray)) ~ ') => '; + loop (my Int $i= 0;$i < @aray.elems-2;$i++) { + if ( @aray[$i+1]-@aray[$i] == @aray[$i+2]-@aray[$i+1] ) { + @slcz.push( "(@aray[$i],@aray[$i+1],@aray[$i+2])" ); + my $msiz=2; # start with triplets then run to end of slice + while (@aray.elems > $i+$msiz && @aray[$i+1]-@aray[$i] == @aray[$i+$msiz]-@aray[$i+$msiz-1]) { $msiz++; } + if ($msiz > 3) {my $slic='(' ~ @aray[$i]; + for (1..$msiz-1) { $slic ~= ',' ~ @aray[$i+$_]; } + $slic ~= ')'; + @slcz.push($slic); + } } } + if (@slcz) { say join(', ',@slcz); } + else { say '() as no slice found.'; } + return(@slcz); +} +if (@*ARGS) { + ASlc(@*ARGS); +} else { + ASlc(1,2,3,4); # => (1,2,3), (1,2,3,4), (2,3,4) + ASlc(2 ); # => () as no slice found. +} diff --git a/challenge-200/pip/raku/ch-2.raku b/challenge-200/pip/raku/ch-2.raku new file mode 100644 index 0000000000..4800c39423 --- /dev/null +++ b/challenge-200/pip/raku/ch-2.raku @@ -0,0 +1,44 @@ +#!/usr/bin/env raku +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #2 of Week #200 - Pip Stuart +# Seven Segment 200: A seven segment display is an electronic component, usually used to display digits. The segments are labeled 'a' through 'g' as shown: +# Seven Segment image with a starting top && bcdef wrapping around clockwise then g barring the middle. +# 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: +# ------- ------- ------- +# | | | | | +# | | | | | +# ------- # Note: 7-bit ASCII, 7-segment display, 7*7 text character cells +# | | | | | +# | | | | | +# ------- ------- ------- +# 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! +# Last date to submit the solution 23:59 (UK Time) Sunday 22nd January 2023. +# Note: I've just started learning Raku about a week ago so this is one of my first Raku scripts. I expect my code will typically resemble my probably around +# intermediate skill-level of Perl5. Just getting stuff to work is cool but then trying to write proper Raku that diverges further is challenging; -Pip +use v6;my $d8VS='N1ILKwXF'; +sub Seg7 {my $iint=@_.shift;my Str @tout[7]; + my @trth = ; + my @digz = split('',$iint, :skip-empty); + for 0..@digz.elems-1 -> $dndx { my $dtru=@trth[@digz[$dndx]]; + my Str @char[7]=((' ' x 8) xx 7); + if ($dtru ~~ /a/) { @char[0].substr-rw(0,7) = '-' x 7; } + if ($dtru ~~ /b/) { @char[1].substr-rw(6,1) = '|'; @char[2].substr-rw(6,1) = '|'; } + if ($dtru ~~ /c/) { @char[4].substr-rw(6,1) = '|'; @char[5].substr-rw(6,1) = '|'; } + if ($dtru ~~ /d/) { @char[6].substr-rw(0,7) = '-' x 7; } + if ($dtru ~~ /e/) { @char[4].substr-rw(0,1) = '|'; @char[5].substr-rw(0,1) = '|'; } + if ($dtru ~~ /f/) { @char[1].substr-rw(0,1) = '|'; @char[2].substr-rw(0,1) = '|'; } + if ($dtru ~~ /g/) { @char[3].substr-rw(0,7) = '-' x 7; } + for 0..6 -> $line { @tout[$line] ~= @char[$line]; } + } + say join("\n",@tout); + return($iint); +} +if (@*ARGS) { + Seg7(@*ARGS); +} else { + Seg7( 200 ); + Seg7(765432); +} -- cgit