diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-06-12 05:17:44 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-06-12 05:17:44 +0100 |
| commit | e2bcd5b91f30ead0274fca78de01038b4432995b (patch) | |
| tree | ad9068d3c9efe097349ab136ebac3d16aaadbaa8 | |
| parent | b69ae3ee9ee4535d66c3c1cb5c4b8fb3712dd75c (diff) | |
| download | perlweeklychallenge-club-e2bcd5b91f30ead0274fca78de01038b4432995b.tar.gz perlweeklychallenge-club-e2bcd5b91f30ead0274fca78de01038b4432995b.tar.bz2 perlweeklychallenge-club-e2bcd5b91f30ead0274fca78de01038b4432995b.zip | |
- Added solutions by Roger Bell_West.
- Added solutions by Robert DiCicco.
- Added solutions by Ulrich Rieke.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Niels van Dijke.
- Added solutions by Simon Proctor.
- Added solutions by Mark Anderson.
- Added solutions by Peter Meszaros.
- Added solutions by W. Luis Mochan.
- Added solutions by David Ferrone.
- Added solutions by Thomas Kohler.
- Added solutions by Stephen G. Lynn.
- Added solutions by Peter Campbell Smith.
- Added solutions by E. Choroba.
- Added solutions by Robbie Hatley.
- Added solutions by Jorg Sommrey.
- Added solutions by Cheok-Yin Fung.
- Added solutions by Robert Ransbottom.
- Added solutions by Flavio Poletti.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Avery Adams.
- Added solutions by Bob Lied.
- Added solutions by Athanasius.
- Added solutions by Simon Green.
- Added solutions by Jan Krnavek.
- Added solutions by Lubos Kolouch.
- Added solutions by BarrOff.
- Added solutions by Solathian.
- Added solutions by Matthias Muth.
52 files changed, 4970 insertions, 3346 deletions
diff --git a/challenge-220/eric-cheung/python/ch-1.py b/challenge-220/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..5ee515aea5 --- /dev/null +++ b/challenge-220/eric-cheung/python/ch-1.py @@ -0,0 +1,17 @@ +
+## arrWordList = ["Perl", "Rust", "Raku"] ## Example 1
+arrWordList = ["love", "live", "leave"] ## Example 2
+
+arrOutputList = []
+
+for charLoop in arrWordList[0]:
+ bExist = True
+ for wordLoop in arrWordList[1:]:
+ if not charLoop in wordLoop.lower():
+ bExist = False
+ break
+
+ if bExist:
+ arrOutputList.append(charLoop)
+
+print (arrOutputList)
diff --git a/challenge-220/eric-cheung/python/ch-2.py b/challenge-220/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..9370992564 --- /dev/null +++ b/challenge-220/eric-cheung/python/ch-2.py @@ -0,0 +1,26 @@ +
+from itertools import permutations
+from math import sqrt
+
+def IsPerfectSqr (nInput):
+ dSqRoot = int(sqrt(nInput))
+ return (dSqRoot * dSqRoot == nInput)
+
+arrIntList = [1, 17, 8] ## Example 1
+## arrIntList = [2, 2, 2] ## Example 2
+
+arrOutputList = []
+
+arrPerm = set(permutations(arrIntList))
+
+for permLoop in list(arrPerm):
+ bIsSqrFul = True
+ for nIndx in range(0, len(permLoop) - 1):
+ if not IsPerfectSqr(permLoop[nIndx] + permLoop[nIndx + 1]):
+ bIsSqrFul = False
+ break
+
+ if bIsSqrFul and not permLoop in arrOutputList:
+ arrOutputList.append(permLoop)
+
+print (arrOutputList)
diff --git a/challenge-220/jeanluc2020/blog-1.txt b/challenge-220/jeanluc2020/blog.txt index 9df40ba86f..9df40ba86f 100644 --- a/challenge-220/jeanluc2020/blog-1.txt +++ b/challenge-220/jeanluc2020/blog.txt diff --git a/challenge-220/jeanluc2020/blog-2.txt b/challenge-220/jeanluc2020/blog1.txt index 4169e0e056..4169e0e056 100644 --- a/challenge-220/jeanluc2020/blog-2.txt +++ b/challenge-220/jeanluc2020/blog1.txt diff --git a/challenge-220/john-horner/README b/challenge-220/john-horner/README new file mode 100644 index 0000000000..c860d6f0be --- /dev/null +++ b/challenge-220/john-horner/README @@ -0,0 +1 @@ +Solutions by John Horner. diff --git a/challenge-220/laurent-rosenfeld/blog.txt b/challenge-220/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..7d399e9609 --- /dev/null +++ b/challenge-220/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/06/perl-weekly-challenge-220-common-characters.html diff --git a/challenge-220/laurent-rosenfeld/blog1.txt b/challenge-220/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..b29e61b761 --- /dev/null +++ b/challenge-220/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/06/perl-weekly-challenge-220-squareful-arrays.html diff --git a/challenge-220/laurent-rosenfeld/perl/ch-1.pl b/challenge-220/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..e311038510 --- /dev/null +++ b/challenge-220/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'say'; + +sub common_char { + my %histo; + my @in = map lc $_, @_; + for my $word (@in) { + my %unique = map { $_ => 1 } split //, $word; + $histo{$_}++ for keys %unique; + } + return sort grep { $histo{$_} == scalar @in } keys %histo; +} + +for my $test ([<Perl Rust Raku>], [<love live leave>]) { + printf "%-15s => ", "@$test"; + say join " ", common_char @$test; +} diff --git a/challenge-220/laurent-rosenfeld/raku/ch-1.raku b/challenge-220/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..306b69a061 --- /dev/null +++ b/challenge-220/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,8 @@ +sub common-char (@in) { + return sort keys ([∩] map {.lc.comb}, @in); +} + +for <Perl Rust Raku>, <love live leave> -> @test { + printf "%-15s => ", "@test[]"; + say common-char @test; +} diff --git a/challenge-220/laurent-rosenfeld/raku/ch-2.raku b/challenge-220/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..c79d803297 --- /dev/null +++ b/challenge-220/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,17 @@ +sub is-squareful (@in) { + for @in.rotor(2 => -1) -> @list { + my $sum = [+] @list; + return False if ($sum.sqrt.Int)² != $sum; + } + return True; +} +sub find-squareful (@in) { + my $result = SetHash.new; + for @in.permutations -> $perm { + $result{"($perm)"}++ if is-squareful $perm; + } + return join ", ", $result.keys; +} +for <1 17 8>, <17 1 8>, <2 2 2> -> @test { + say @test, " => ", find-squareful @test; +} diff --git a/challenge-220/perlboy1967/perl/ch1.pl b/challenge-220/perlboy1967/perl/ch-1.pl index 29be6dc082..29be6dc082 100755 --- a/challenge-220/perlboy1967/perl/ch1.pl +++ b/challenge-220/perlboy1967/perl/ch-1.pl diff --git a/challenge-220/perlboy1967/perl/ch2.pl b/challenge-220/perlboy1967/perl/ch-2.pl index c31e23c80e..c31e23c80e 100755 --- a/challenge-220/perlboy1967/perl/ch2.pl +++ b/challenge-220/perlboy1967/perl/ch-2.pl diff --git a/challenge-220/robert-dicicco/julia/ch-1.jl b/challenge-220/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..0c1ac773ed --- /dev/null +++ b/challenge-220/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,49 @@ +#!/usr/bin/env julia +#= +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-06-05 +Challenge 220 Task 1 common Characters ( Julia ) +------------------------------------------------ +=# + +using Printf + +words = [["Perl", "Rust", "Raku"], ["love", "live", "leave"]] +alphabet_hash = Dict() + +for wds in words + for c in 'a':'z' + alphabet_hash[c] = 0 + end + @printf("Input: @words = %s\n",wds) + for wd in wds + for ch in lowercase(wd) + alphabet_hash[ch] += 1 + end + end + @printf("Output: ") + # Print the dictionary + for (key, value) in alphabet_hash + if value >= 3 + @printf("%s ",key) + end + end + println("\n") +end + +#= +------------------------------------------------ +julia .\CommonChars.jl + +Input: @words = ["Perl", "Rust", "Raku"] +Output: r + +Input: @words = ["love", "live", "leave"] +Output: e v l +------------------------------------------------ +=# + + + + diff --git a/challenge-220/robert-dicicco/julia/ch-2.jl b/challenge-220/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..4bbd962ba8 --- /dev/null +++ b/challenge-220/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,59 @@ +#!/usr/bin/env julia +#= +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-06-08 +Challenge 220 Task 2 Squareful ( Julia ) +------------------------------------------ +=# +using Printf +using Combinatorics + +myints = [[1,8,17],[1,8,17,19]] + +p=[] + +function IsPerfectSquare(number_to_test) + root = floor(sqrt(number_to_test)) + if ((root ^ 2 ) == number_to_test) + return true; + end + return false; +end + +for intsub in myints + @printf("Input: @ints = %s\n",intsub) + ln = length(intsub) + @printf("Output: ") + global p = collect(permutations(intsub)) + ln = length(intsub) + for perm in p + tv = 1 + flag = 0 + while tv < ln + if IsPerfectSquare(perm[tv] + perm[tv + 1]) + flag += 1 + else + flag = 0 + end + tv += 1 + end + if flag == ln - 1 + @printf("%s ",perm) + end + end + println("\n") +end + +#= +------------------------------------------ +SAMPLE OUTPUT +julia .\Squareful.jl + +Input: @ints = [1, 8, 17] +Output: [1, 8, 17] [17, 8, 1] + +Input: @ints = [1, 8, 17, 19] +Output: [1, 8, 17, 19] [19, 17, 8, 1] +------------------------------------------ +=# diff --git a/challenge-220/robert-dicicco/perl/ch-1.pl b/challenge-220/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..67cde25402 --- /dev/null +++ b/challenge-220/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +=begin comment +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-06-05 +Challenge 220 Task 1 common Characters ( Perl ) +------------------------------------------------ +=cut +use strict; +use warnings; +use feature 'say'; + +my @words = (["Perl", "Rust", "Raku"], ["love", "live", "leave"]); + +my $cnt = 0; +my $str = ''; +my %found = (); +my $arrlen = 0; + +for (my $wd = 0; $wd < scalar @words; $wd ++ ) { + $arrlen = scalar @{$words[$wd]}; + $str = join('', "a".."z"); + $cnt = 0; + + my $arrelem = 0; + print("Input: \@words = "); + while($arrelem < $arrlen){ + print("$words[$wd][$arrelem] "); + $arrelem++; + } + print("\n"); + + while ($cnt < length($str)) { + my $ch = substr($str, $cnt++, 1); + if ((lc($words[$wd][0]) =~ /$ch/) and (lc($words[$wd][1]) =~ /$ch/) and (lc($words[$wd][2]) =~ /$ch/)) { + $found{$ch} = 1; + } + } + + my @arr = (); + print("Output: "); + for my $key (keys %found) { + push(@arr,$key); + } + my $str = join(",",sort(@arr)); + print("\($str\)\n\n"); + %found = (); +} + +=begin comment +------------------------------------------------ +SAMPLE OUTPUT +perl .\CommonChars.pl + +Input: @words = Perl Rust Raku +Output: (r) + +Input: @words = love live leave +Output: (e,l,v) +------------------------------------------------ +=cut + diff --git a/challenge-220/robert-dicicco/perl/ch-2.pl b/challenge-220/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..1b7bdc94b7 --- /dev/null +++ b/challenge-220/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!#!/usr/bin/env perl +=begin comment +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-0-6-07 +Challenge 220 Task 2 Squareful ( Perl ) +------------------------------------------ +=cut +use strict; +use warnings; +use feature 'say'; +use Algorithm::Permute qw/permute/; + +my @ints = (1,8,17); +my $flag = 0; +my $elems = $#ints; + +sub IsPerfectSquare { + my $number_to_test = shift; + my $root = int(sqrt($number_to_test)); + if (($root ** 2 ) == $number_to_test) { + return 1; + } + return undef; +} + +say "Input: \@ints = @ints"; + +# print all permutations of @ints +print("Output: "); +my $p = Algorithm::Permute->new(\@ints); +while ( my @perm = $p->next ) { + for (my $tv = 0; $tv < $#perm; $tv++) { + if (IsPerfectSquare($perm[$tv] + $perm[$tv+1])) { + $flag += 1; + } else { + $flag = 0; + } + } + + if ($flag == $elems) { + print("(@perm) "); + } + $flag = 0; +} + +print("\n"); + +=begin comment +------------------------------------------ +SAMPLE OUTPUT +perl .\Squareful.pl + +Input: @ints = 1 8 17 +Output: (17 8 1) (1 8 17) + +Input: @ints = 1 8 17 19 +Output: (19 17 8 1) (1 8 17 19) +------------------------------------------ +=cut diff --git a/challenge-220/robert-dicicco/python/ch-1.py b/challenge-220/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..632e169c46 --- /dev/null +++ b/challenge-220/robert-dicicco/python/ch-1.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# ------------------------------------------------ +# AUTHOR: Robert DiCicco +# DATE : 2023-06-05 +# Challenge 220 Task 1 common Characters ( Python ) +# ------------------------------------------------ + +words = [["Perl", "Rust", "Raku"], ["love", "live", "leave"]] + +alphabet_hash = {} + +for wds in words: + for char in range(ord('a'), ord('z')+1): + alphabet_hash[chr(char)] = 0 + + print("Input: @words = ",wds) + for wd in wds: + wd = wd.lower() + for ch in wd: + alphabet_hash[ch] += 1 + + print("Output: ",end= " ") + for i in alphabet_hash: + if alphabet_hash[i] >= 3: + print(i,end=" ") + print("\n") + +# ------------------------------------------------ +# SAMPLE OUTPUT +# python .\CommonChars.py + +# Input: @words = ['Perl', 'Rust', 'Raku'] +# Output: r + +# Input: @words = ['love', 'live', 'leave'] +# Output: e l v + +# ------------------------------------------------ diff --git a/challenge-220/robert-dicicco/python/ch-2.py b/challenge-220/robert-dicicco/python/ch-2.py new file mode 100644 index 0000000000..550194cb55 --- /dev/null +++ b/challenge-220/robert-dicicco/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# ------------------------------------------ +# AUTHOR: Robert DiCicco +# DATE : 2023-06-00 +# Challenge 220 Task 2 Squareful ( Python ) +# ------------------------------------------ +import math +from itertools import permutations + + +myints = [[1,8,17],[1,8,17,19]] + +def IsPerfectSquare(number_to_test) : + root = math.floor(math.sqrt(number_to_test)) + if ((root ** 2 ) == number_to_test) : + return True; + return False; + +for intsub in myints: + print("Input: @ints = ",intsub) + print("OutPut: ",end="") + perm = permutations(intsub) + for i in list(perm): + ln = len(i) - 1 + tv = 0 + flag = 0 + while tv < ln : + if IsPerfectSquare(i[tv] + i[tv + 1]) : + flag += 1 + else : + flag = 0 + tv += 1 + if flag == ln - 1 : + print(i,end=" ") + print("\n") + +#------------------------------------------ +# SAMPLE OUTPUT +# python .\Squareful.py + +# Input: @ints = [1, 8, 17] +# OutPut: (1, 17, 8) (17, 1, 8) + +# Input: @ints = [1, 8, 17, 19] +# OutPut: (1, 19, 17, 8) (19, 1, 8, 17) +#------------------------------------------ + + + diff --git a/challenge-220/robert-dicicco/raku/ch-1.raku b/challenge-220/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..4180a77bd3 --- /dev/null +++ b/challenge-220/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,59 @@ +#!/usr/bin/env raku +=begin comment +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-06-05 +Challenge 220 Task 1 common Characters ( Raku ) +------------------------------------------------ +=end comment + +use v6; + +my @words = <Perl Rust Raku>, <love live leave>; + +my $str = join('', "a".."z"); + +for (@words) -> @wds { + say "Input: \@words = ", @wds; + my $sz = @wds.elems; # number of words in each entry + my $cnt = 0; + my %found = (); # hash to hold results + my @out = (); # array to hold output; + while $cnt < $sz { # for each one of the word entries + my $strcnt = 0; + while $strcnt < $str.chars { + my $ch = substr($str, $strcnt++,1); + my $arrch = lc(join('',@wds[$cnt])); + if $arrch.contains($ch) { # check to see if entry holds character + if defined %found{$ch} { # bump its count + %found{$ch}++ + } else { + %found{$ch} = 1; # add it to the hash array + } + } + } + $cnt++; # get next word + } + for %found.kv -> $k, $v { # for keys, values in hash + @out.push: $k if $v == $sz; # push the key to @out array if in all words + } + say "Output: ",@out.sort; # and say the results + say " "; +} + +=begin comment +------------------------------------------------ +SAMPLE OUTPUT +raku .\CommonChars.rk + +Input: @words = (Perl Rust Raku) +Output: (r) + +Input: @words = (love live leave) +Output: (e l v) + +------------------------------------------------ +=end comment + + + diff --git a/challenge-220/robert-dicicco/raku/ch-2.raku b/challenge-220/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..4010124aa4 --- /dev/null +++ b/challenge-220/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,43 @@ +#!/usr/bin/env raku +use v6; +=begin comment +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-06-08 +Challenge 220 Task 2 Squareful ( Raku ) +------------------------------------------ +=end comment + +my @ints = <1 8 17>,<1 8 17 19>; + +sub IsPerfectSquare($number_to_test) { + my $root = $number_to_test.sqrt.Int; + if (($root ** 2 ) == $number_to_test) { + return True; + } + return False; +} + +for (@ints) -> @intsub { + say "Input: \@ints = ",@intsub; + print("Output: "); + for (@intsub.permutations) -> @perm { + my $tv = 0; + my $flag = 0; + while $tv < @intsub.elems - 1 { + if (IsPerfectSquare(@perm[$tv] + @perm[$tv+1])) { + $flag += 1; + } else { + $flag = 0; + } + $tv++; + } + if ($flag == (@intsub.elems - 1)) { + print("(" ~ @perm ~ ") "); + } + } + print("\n\n"); +} + + + diff --git a/challenge-220/robert-dicicco/ruby/ch-1.rb b/challenge-220/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..d825f52ca7 --- /dev/null +++ b/challenge-220/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,51 @@ +#!/usr/bin/env ruby +=begin +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-06-05 +Challenge 220 Task 1 common Characters ( Ruby ) +------------------------------------------------ +=end + +words = [["Perl", "Rust", "Raku"], ["love", "live", "leave"]]; + +str = ('a' .. 'z') + +alphabet_hash = {} + +words.each do |wds| + str.each do |letter| + alphabet_hash[letter] = 0 + end + puts("Input: @words = #{wds}") + wds.each do |wd| |
