diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-01-23 01:51:15 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-01-23 01:51:15 +0000 |
| commit | 3a0d3971fee4f0e88baa2cf83e7a4a33b24ca8ec (patch) | |
| tree | 8c67d01edbb6591029af2b3f44fba80427971172 | |
| parent | 297f41de92d136922e9c85c2f8075966d6ef80ed (diff) | |
| download | perlweeklychallenge-club-3a0d3971fee4f0e88baa2cf83e7a4a33b24ca8ec.tar.gz perlweeklychallenge-club-3a0d3971fee4f0e88baa2cf83e7a4a33b24ca8ec.tar.bz2 perlweeklychallenge-club-3a0d3971fee4f0e88baa2cf83e7a4a33b24ca8ec.zip | |
- Added solutions by Roger Bell_West.
- Added solutions by Dave Jacoby.
- Added solutions by David Ferrone.
- Added solutions by Luca Ferrari.
- Added solutions by Mark Anderson.
- Added solutions by W. Luis Mochan.
- Added solutions by Peter Campbell Smith.
- Added solutions by Mariano Spadaccini.
- Added solutions by Thomas Kohler.
- Added solutions by Bob Lied.
- Added solutions by Jorg Sommrey.
- Added solutions by Flavio Poletti.
- Added solutions by Pip Stuart.
- Added solutions by E. Choroba.
- Added solutions by Stephen G. Lynn.
- Added solutions by Matthew Neleigh.
- Added solutions by Robert Ransbottom.
- Added solutions by Athanasius.
- Added solutions by Simon Green.
- Added solutions by Cheok-Yin Fung.
- Added solutions by Tyler Wardhaugh.
- Added solutions by Jan Krnavek.
- Added solutions by Bruce Gray.
- Added solutions by James Smith.
- Added solutions by Robbie Hatley.
- Added solutions by Solathian.
- Added solutions by Arne Sommer.
- Added solutions by Carlos Oliveira.
- Added solutions by Marton Polgar.
- Added solutions by Adam Russell.
- Added solutions by Duncan C. White.
- Added solutions by Lars Balker.
- Added solutions by Colin Crain.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Robert DiCicco.
- Added solutions by Ulrich Rieke.
42 files changed, 4145 insertions, 2186 deletions
diff --git a/challenge-199/eric-cheung/python/ch-1.py b/challenge-199/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..5227a2c7b1 --- /dev/null +++ b/challenge-199/eric-cheung/python/ch-1.py @@ -0,0 +1,20 @@ +
+from itertools import combinations
+
+def get_GoodPairs_List(arrInput):
+
+ arrGoodPairsList = []
+
+ nIndxTuple = combinations(range(0, len(arrInput)), 2)
+
+ for nIndxLoop_01, nIndxLoop_02 in list(nIndxTuple):
+ if arrInput[nIndxLoop_01] == arrInput[nIndxLoop_02]:
+ arrGoodPairsList.append([nIndxLoop_01, nIndxLoop_02])
+
+ return arrGoodPairsList
+
+## arrInputList = [1, 2, 3, 1, 1, 3] ## Example 1
+## arrInputList = [1, 2, 3] ## Example 2
+arrInputList = [1, 1, 1, 1] ## Example 3
+
+print (len(get_GoodPairs_List(arrInputList)))
diff --git a/challenge-199/eric-cheung/python/ch-2.py b/challenge-199/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..2ed1c89d29 --- /dev/null +++ b/challenge-199/eric-cheung/python/ch-2.py @@ -0,0 +1,25 @@ +
+from itertools import combinations
+
+def get_GoodTriplets_List(arrInput, arrInputInt):
+
+ arrGoodPairsList = []
+
+ nIndxTuple = combinations(range(0, len(arrInput)), 3)
+
+ for nIndxLoop_01, nIndxLoop_02, nIndxLoop_03 in list(nIndxTuple):
+ if abs(arrInput[nIndxLoop_01] - arrInput[nIndxLoop_02]) <= arrInputInt[0] and abs(arrInput[nIndxLoop_02] - arrInput[nIndxLoop_03]) <= arrInputInt[1] and abs(arrInput[nIndxLoop_01] - arrInput[nIndxLoop_03]) <= arrInputInt[2]:
+ arrGoodPairsList.append([nIndxLoop_01, nIndxLoop_02, nIndxLoop_03])
+
+ return arrGoodPairsList
+
+
+## Example 1
+## arrInputList = [3, 0, 1, 1, 9, 7]
+## arrInputList_Int = [7, 2, 3]
+
+## Example 2
+arrInputList = [1, 1, 2, 2, 3]
+arrInputList_Int = [0, 0, 1]
+
+print (len(get_GoodTriplets_List(arrInputList, arrInputList_Int)))
diff --git a/challenge-200/colin-crain/blog.txt b/challenge-200/colin-crain/blog.txt new file mode 100644 index 0000000000..de69cd7720 --- /dev/null +++ b/challenge-200/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.com/2023/01/22/seven-segments-to-midnight diff --git a/challenge-200/colin-crain/perl/ch-1.pl b/challenge-200/colin-crain/perl/ch-1.pl new file mode 100755 index 0000000000..3afdc4e84e --- /dev/null +++ b/challenge-200/colin-crain/perl/ch-1.pl @@ -0,0 +1,78 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# arithmetic-sequences.pl
+#
+# 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.
+#
+#
+# method:
+#
+# we work from left to right. Starting at element [0], we look
+# ahead to element [1]. We set the $last difference as the
+# difference between elements [1] and [0]. We then look to
+# element 2. If the difference between it and [1] is the same
+# we have a sequence: store the slice between our starting and
+# ending positions as a nested array in @out.
+#
+# We then move through the rest of the array as long as the
+# difference between adjacent elements remains the same as the
+# last difference. If it is, again store the slice from the start to
+# current lookahead in @out as well.
+#
+# On failure move the start position up one and begin to search again.
+#
+# Stop two places from the end position, as that is the minimal space to
+# find a new sequence.
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+my @arr = @ARGV;
+@arr == 0 and @arr = (1,2,3,5,7,9,6,3,0,-3);
+
+my @out;
+my $last;
+my $curr;
+
+for (0..$#arr-2) {
+ $curr = $_+1;
+ $last = $arr[$_+1] - $arr[$_];
+ while ($arr[$curr+1] - $arr[$curr] == $last) {
+ push @out, [ @arr[$_..$curr+1] ];
+ last if ++$curr == $#arr;
+ }
+}
+
+say "$_->@*" for @out;
+
+
+
+
diff --git a/challenge-200/colin-crain/perl/ch-2.pl b/challenge-200/colin-crain/perl/ch-2.pl new file mode 100755 index 0000000000..b328ae1429 --- /dev/null +++ b/challenge-200/colin-crain/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# seven-seconds.pl
+#
+# 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:
+#
+# a
+# f b
+# g
+# e c
+# d
+#
+# 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!
+#
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+use open ':std', ':encoding(UTF-8)';
+
+## given encoding method
+my @truth = qw( abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg );
+
+## input
+my $num = shift @ARGV // 31415926;
+my @out;
+
+for my $d (split //, $num) {
+ $out[0] .= $truth[$d] =~ s/(a?)[bcdefg]*/ $1 ? ' ╺╸ ' : ' '/er;
+ $out[1] .= reverse $truth[$d] =~ s/a?(b?)[cde]*(f?)g?/
+ ($1 ? '┃' : ' ')
+ . ' '
+ . ($2 ? '┃' : ' ')/er;
+ $out[2] .= $truth[$d] =~ s/[abcdef]*(g?)/ $1 ? ' ━━ ' : ' '/er;
+ $out[3] .= reverse $truth[$d] =~ s/a?b?(c?)d?(e?)f?g?/
+ ($1 ? '┃' : ' ')
+ . ' '
+ . ($2 ? '┃' : ' ')/er;
+ $out[4] .= $truth[$d] =~ s/[abc]*(d?)[efg]*/$1 ? ' ╺╸ ' : ' '/er;
+
+ $out[$_] .= ' ' for (0..4);
+}
+
+## output
+for my $i (0..4) {
+ say $out[$i];
+}
+
+
+
+
diff --git a/challenge-200/eric-cheung/python/ch-1.py b/challenge-200/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..74f509b821 --- /dev/null +++ b/challenge-200/eric-cheung/python/ch-1.py @@ -0,0 +1,25 @@ +
+## arrInput = [1, 2, 3, 4] ## Example 1
+arrInput = [2] ## Example 2
+
+def IsArithmetic(arrSubInput):
+
+ if len(arrSubInput) < 3:
+ return False
+
+ nDiff = arrSubInput[1] - arrSubInput[0]
+
+ for nLoop in range(2, len(arrSubInput)):
+ if arrSubInput[nLoop] - arrSubInput[nLoop - 1] != nDiff:
+ return False
+
+ return True
+
+arrOutput = []
+
+for nLen in range(3, len(arrInput) + 1):
+ for nIndx in range(0, len(arrInput) - nLen + 1):
+ if IsArithmetic(arrInput[nIndx:nIndx + nLen]):
+ arrOutput.append(arrInput[nIndx:nIndx + nLen])
+
+print (arrOutput)
\ No newline at end of file diff --git a/challenge-200/eric-cheung/python/ch-2.py b/challenge-200/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..e6ca0422eb --- /dev/null +++ b/challenge-200/eric-cheung/python/ch-2.py @@ -0,0 +1,11 @@ +
+## Remarks
+## https://pypi.org/project/sevseg/
+## https://inventwithpython.com/bigbookpython/project64.html
+##
+
+import sevseg
+
+nInput = 200
+
+print (sevseg.getSevSegStr(nInput, 1))
diff --git a/challenge-200/lars-balker/perl/ch-2.pl b/challenge-200/lars-balker/perl/ch-2.pl new file mode 100755 index 0000000000..070ae1d18a --- /dev/null +++ b/challenge-200/lars-balker/perl/ch-2.pl @@ -0,0 +1,47 @@ +use v5.30; + +my @array = (1,2,3,4); +# Output: (1,2,3), (2,3,4), (1,2,3,4) + +my @res; +for my $length (3 .. @array) { + I: + for my $i (0 .. @array - $length) { + my $delta = $array[$i + 1] - $array[$i]; + my $j = 1; + while ($j + 1 < $length) { + next I if $array[$i + $j + 1] - $array[$i + $j] != $delta; + $j++; + } + push @res, [ @array[ $i .. $i + $length - 1 ] ]; + } +} +@res = ([]) unless @res; +say join ", ", map { "(" . (join ",", @$_) . ")" } @res; + + + + +my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg a cdefg abc abcdefg abcfg>; + +my @input = split //, "200"; + +my @out = ("aaaaaaa ", + "f b ", + "f b ", + "ggggggg ", + "e c ", + "e c ", + "ddddddd "); +my %symbol = qw/ a - b | c | d - e | f | g - /; + +for my $line (0..$#out) { + for my $digit (@input) { + my @show = split //, $truth[$digit]; + my $out = $out[$line]; + $out =~ s/$_/$symbol{$_}/g for @show; + $out =~ s/[a-g]/ /g; + print $out; + } + print "\n"; +} diff --git a/challenge-200/laurent-rosenfeld/blog.txt b/challenge-200/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..82acaae7a9 --- /dev/null +++ b/challenge-200/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/01/perl-weekly-challenge-200-arithmetic-slices-and-seven-segment-display.html diff --git a/challenge-200/laurent-rosenfeld/perl/ch-1.pl b/challenge-200/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..8fa13a071a --- /dev/null +++ b/challenge-200/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use feature "say"; + +sub find_slices { + my @in = @_; + my @out; + # return [] if @in < 3; + for my $i (0..$#in - 2) { + my $gap = $in[$i+1] - $in[$i]; + for my $j ($i+2..$#in) { + last if $in[$j] - $in[$j-1] != $gap; + push @out, [@in[$i..$j]]; + } + } + return @out ? @out : []; +} +for my $test ([<1 2 3 4>], [<2 5>], [<3 4 5 6 8>], + [<3 5 7 9>], [<2 5 9>]) { + printf "%-10s => ", "@$test"; + say map "(@$_) ", find_slices @$test; +} diff --git a/challenge-200/laurent-rosenfeld/perl/ch-2.pl b/challenge-200/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..99a8927fa4 --- /dev/null +++ b/challenge-200/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +use feature "say"; + +my %c; # ascii coding of digit's slices +$c{'h'} = "-" x 7; # Horizontal line +$c{'l'} = "| "; # Vertical bar, left +$c{'r'} = " |"; # Vertical bar, right +$c{'2'} = "| |"; # 2 vertical bars +$c{'n'} = " " x 7; # empty horizontal line + +my @nums = ( # Digit hoirizontal slices + [<h 2 2 n 2 2 h>], # 0 + [<n r r n r r n>], # 1 + [<h r r h l l h>], # 2 + [<h r r h r r h>], # 3 + [<n 2 2 h r r n>], # 4 + [<h l l h r r h>], # 5 + [<n l l h 2 2 h>], # 6 + [<h r r n r r n>], # 7 + [<h 2 2 h 2 2 h>], # 8 + [<h 2 2 h r r n>]); # 9 + + +sub display{ + my @digits = split //, shift; + for my $l (0..6) { + say join " ", map {$c{$nums[$_][$l]}} @digits; + } +} + +for my $test (<200 2023 01234 56789>) { + display $test; +} diff --git a/challenge-200/laurent-rosenfeld/raku/ch-1.raku b/challenge-200/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..704c39bea3 --- /dev/null +++ b/challenge-200/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,15 @@ +sub find_slices (@in) { + my @out; + return @out if @in.elems < 3; + for 0..@in.end - 2 -> $i { + my $gap = @in[$i+1] - @in[$i]; + for $i+2..@in.end -> $j { + last if @in[$j] - @in[$j-1] != $gap; + push @out, @in[$i..$j]; + } + } + return @out; +} +for <1 2 3 4>, <2 5>, <3 5 7 9>, <2 5 9> -> @test { + say (~@test).fmt("%-10s => "), find_slices @test; +} diff --git a/challenge-200/laurent-rosenfeld/raku/ch-2.raku b/challenge-200/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..55ce30bb4a --- /dev/null +++ b/challenge-200/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,29 @@ +my %c; # ascii coding of digit's slices +%c<h> = "-" x 7; # Horizontal line +%c<l> = "| "; # Vertical bar, left +%c<r> = " |"; # Vertical bar, right +%c<2> = "| |"; # 2 vertical bars +%c<n> = " " x 7; # empty horizontal line + +my @nums = # Digit horizontal slices + <h 2 2 n 2 2 h>, # 0 + <n r r n r r n>, # 1 + <h r r h l l h>, # 2 + <h r r h r r h>, # 3 + <n l l h 2 2 n>, # 4 + <h l l h r r h>, # 5 + <n l l h 2 2 h>, # 6 + <h r r n r r n>, # 7 + <h 2 2 h 2 2 h>, # 8 + <h 2 2 h r r n>; # 9 + +sub display ($num) { + my @digits = $num.comb; + for 0..6 -> $l { # Lines 0 to 6 iof the display + say join " ", map {%c{@nums[$_][$l]}}, @digits; + } +} + +for <200 2023 01234 56789> -> $test { + display $test; +} diff --git a/challenge-200/robert-dicicco/julia/ch-1.jl b/challenge-200/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..3725469509 --- /dev/null +++ b/challenge-200/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,127 @@ +#!/usr/bin/env julia + +#= + +AUTHOR: Robert DiCicco + +DATE: 01-18-2023 + +Challenge #200 Arithmetic Slices ( Julia ) + +=# + +using Printf + + + +lists = [[1,2,3,4],[2]] + +out = [] + + + +function GetIntervals(slice) + + if length(slice) < 3 + + return -1 + + end + + x = 1 + + + + while x < length(slice) + + if x > 1 + + interval = slice[x] - slice[x-1] + + push!(out, interval) + + end + + x += 1 + + end + + testval = out[1] + + for arrint in out + + if arrint != testval + + return -1 + + end + + end + + return 1 + +end + + + +for list in lists + + cnt = 1 + + @printf("Input: @array = %s\n", list) + + slice = Int64[] + + @printf("Output: ") + + while cnt < length(list)-1 + + push!(slice, list[cnt]) + + push!(slice, list[cnt + 1]) + + push!(slice, list[cnt + 2]) + + if length(slice) == 3 + + retval = GetIntervals(slice) + + if retval == 1 + + @printf("%s ",slice) + + end + + end + + cnt += 1 + + slice = Int64[] + + end + + ret = GetIntervals(list) + + ret == 1 ? (println(list); println(" ")) : println("()") + +end + + + +#= + +SAMPLE OUTPUT + +julia .\ArithmeticSlices.jl + +Input: @array = [1, 2, 3, 4] + +Output: [1, 2, 3] [2, 3, 4] [1, 2, 3, 4] + + + +Input: @array = [2] + +Output: () + +=# diff --git a/challenge-200/robert-dicicco/perl/ch-1.pl b/challenge-200/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..b9881772c3 --- /dev/null +++ b/challenge-200/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,125 @@ +#!/usr/bin/env perl + +=begin comment + +AUTHOR: Robert DiCicco + +DATE: 01-18-2023 + +Challenge #200 Arithmetic Slices ( Perl ) + +=cut + + + +use strict; + +use warnings; + + + +my @out = (); + + + +sub GetIntervals { + + my $slice = shift; + + return -1 if (scalar @$slice < 3); + + my $x = 0; + + + + while ( $x < scalar @$slice ) { + + if ( $x > 0 ) { + + my $interval = $slice->[$x] - $slice->[$x-1]; + + push(@out, $interval ); + + } + + $x++; + + } + + my $testval = $out[0]; + + for my $arrint (@out) { + + if ( $arrint != $testval ){ + + return -1; + + } + + } + + return 1; + +} + + + +my @lists = ([1,2,3,4],[2]); + + + +for my $list (@lists) { + + my $cnt = 0; + + print("Input: \@array = \[@$list\]\n"); + + my @slice = (); + + print("Output: "); + + while( $cnt < scalar @$list - 2) { + + @slice = @$list[$cnt..$cnt+2]; + + if (scalar @slice == 3) { + + my $retval = GetIntervals(\@slice); + + if ($retval == 1 ) { print("[@slice] ")}; + + } + + $cnt++; + + @slice = (); + + } + + my $ret = GetIntervals(\@$list); + + $ret == 1 ? print("[@$list]\n") : print("()\n"); + + print("\n"); + +} + + + +=begin + +SAMPLE OUTPUT + +perl .\ArithmeticSlices.pl + +Input: @array = [1 2 3 4] + +Output: [1 2 3] [2 3 4] [1 2 3 4] + + + +Input: @array = [2] + +Output: () + +=cut diff --git a/challenge-200/robert-dicicco/raku/ch-1.raku b/challenge-200/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..231803904f --- /dev/null +++ b/challenge-200/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,123 @@ +#/usr/bin/env raku + +=begin comment + +AUTHOR: Robert DiCicco + +DATE: 01-18-2023 + +Challenge #200 Arithmetic Slices ( Raku ) + +=end comment + +use v6; + + + +my @out = (); + +my @lists = ([1,2,3,4],[2]); + + + +sub GetIntervals(@slice) { + + return -1 if (@slice.elems < 3); + + my $x = 0; + + + + while ( $ |
