diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-03-08 23:32:11 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-03-08 23:32:11 +0000 |
| commit | a0ea2c78c8ac2bb33fb79ab151f86b3beb143647 (patch) | |
| tree | 41cbffecd69bd6774b09657b5702f620154cf9cb | |
| parent | bc577f09f07be6abd72291c3e9a6642e4336d9c9 (diff) | |
| download | perlweeklychallenge-club-a0ea2c78c8ac2bb33fb79ab151f86b3beb143647.tar.gz perlweeklychallenge-club-a0ea2c78c8ac2bb33fb79ab151f86b3beb143647.tar.bz2 perlweeklychallenge-club-a0ea2c78c8ac2bb33fb79ab151f86b3beb143647.zip | |
- Added solutions by Avery Adams.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Mark Anderson.
- Added solutions by Luca Ferrari.
- Added solutions by Peter Campbell Smith.
- Added solutions by W. Luis Mochan.
- Added solutions by Paulo Custodio.
- Added solutions by Cheok-Yin Fung.
- Added solutions by E. Choroba.
- Added solutions by Bob Lied.
- Added solutions by Robbie Hatley.
- Added solutions by Matthias Muth.
- Added solutions by Lubos Kolouch.
- Added solutions by Solathian.
- Added solutions by Duncan C. White.
- Added solutions by Kjetil Skotheim.
- Added solutions by Marton Polgar.
- Added solutions by David Ferrone.
- Added solutions by Mariano Spadaccini.
- Added solutions by Robert DiCicco.
- Added solutions by Ulrich Rieke.
- Added solutions by Laurent Rosenfeld.
48 files changed, 4604 insertions, 3195 deletions
diff --git a/challenge-207/avery-adams/perl/ch-1.pl b/challenge-207/avery-adams/perl/ch-1.pl new file mode 100755 index 0000000000..cca2172ef8 --- /dev/null +++ b/challenge-207/avery-adams/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl + +use strict; +use v5.10; + +my @list; + +foreach (@ARGV) { + if ($_ =~ /^[qwertyuiopQWERTYUIOP]*$/) {push @list, $_}; + if ($_ =~ /^[asdfghjklASDFGHJKL]*$/) {push @list, $_}; + if ($_ =~ /^[zxcvbnmZXCVBNM]*$/) {push @list, $_}; +} +if (scalar @list) { + foreach (@list) { + say $_; + } +} else { + say "I didn't find anything on one row of keys." +} diff --git a/challenge-207/avery-adams/perl/ch-2.pl b/challenge-207/avery-adams/perl/ch-2.pl new file mode 100755 index 0000000000..6693cb1f29 --- /dev/null +++ b/challenge-207/avery-adams/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +use strict; +use v5.10; +use List::Util "uniqint"; + +my @list = @ARGV; +my $hindex; + +sort { $b <=> $a } @list; + +for (my $index = 0; $index <= scalar @list; $index++) { + if ($index + 1 >= $list[$index]) {$hindex = $list[$index]; last}; +} +$hindex ? say $hindex : say "This researcher has no H-Index.";
\ No newline at end of file diff --git a/challenge-207/eric-cheung/python/ch-1.py b/challenge-207/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..57ddd9e817 --- /dev/null +++ b/challenge-207/eric-cheung/python/ch-1.py @@ -0,0 +1,30 @@ +
+arrKeyBoardRow = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
+
+arrInputWords = ["Hello", "Alaska", "Dad", "Peace"] ## Example 1
+## arrInputWords = ["OMG", "Bye"] ## Example 2
+
+arrKeyBoardChar = []
+for arrLoop in arrKeyBoardRow:
+ arrKeyBoardChar.append(list(arrLoop))
+
+arrOutputWords = []
+
+for arrLoop in arrInputWords:
+
+ strLoopLower = arrLoop.lower()
+
+ for nIndx in range(0, len(arrKeyBoardChar)):
+ if strLoopLower[0] in arrKeyBoardChar[nIndx]:
+ break
+
+ bSameRow = True
+ for nLoop in range(1, len(arrLoop)):
+ if strLoopLower[nLoop] not in arrKeyBoardChar[nIndx]:
+ bSameRow = False
+ break
+
+ if bSameRow:
+ arrOutputWords.append(arrLoop)
+
+print (arrOutputWords)
diff --git a/challenge-207/eric-cheung/python/ch-2.py b/challenge-207/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..b2da387f6b --- /dev/null +++ b/challenge-207/eric-cheung/python/ch-2.py @@ -0,0 +1,32 @@ +
+## https://gist.github.com/restrepo/c5f8f9fd5504a3f93ae34dd10a5dd6b0
+
+def hIndex(arrCitation):
+
+ ### https://github.com/kamyu104/LeetCode/blob/master/Python/h-index.py
+ ### :type arrCitation: List[int]
+ ### :rtype: int
+
+ ### Given an array of citations (each citation is a non-negative integer)
+ ### of a researcher, write a function to compute the researcher's h-index.
+ ###
+ ### According to the definition of h-index on Wikipedia:
+ ### "A scientist has index h if h of his/her N papers have
+ ### at least h citations each, and the other N − h papers have
+ ### no more than h citations each."
+ ###
+ ### For example, given arrCitation = [3, 0, 6, 1, 5],
+ ### which means the researcher has 5 papers in total
+ ### and each of them had received 3, 0, 6, 1, 5 citations respectively.
+ ### Since the researcher has 3 papers with at least 3 citations each and
+ ### the remaining two with no more than 3 citations each, his h-index is 3.
+ ###
+ ### Note: If there are several possible values for h, the maximum one is taken as the h-index.
+
+ return sum(nLoop >= nIndx + 1 for nIndx, nLoop in enumerate(sorted(list(arrCitation), reverse = True)))
+
+
+## arrInputCitation = [10, 8, 5, 4, 3] ## Example 1
+arrInputCitation = [25, 8, 5, 3, 3] ## Example 2
+
+print (hIndex(arrInputCitation))
diff --git a/challenge-207/laurent-rosenfeld/blog.txt b/challenge-207/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..ee125d75ae --- /dev/null +++ b/challenge-207/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/03/perl-weekly-challenge-207-keyboard-word-and-h-index.html diff --git a/challenge-207/laurent-rosenfeld/perl/ch-1.pl b/challenge-207/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..76b6ac806c --- /dev/null +++ b/challenge-207/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +use feature "say"; + +my @rows; +push @rows, {map {$_ => 1} split //, $_} + for "qwertyuiop", "asdfghjkl", "zxcvbnm"; + +for my $test ([<Hello Alaska Dad Peace>], [<OMG Bye>], + [<Power Fad Finish Tower Quit True Omit>]) { + say join " ", find_kb_word(@$test); +} + +sub find_kb_word { + my @out; + for my $word (@_) { + for my $row (@rows) { + my $eligible = 1; + push @out, $word and last + unless grep {not exists $row->{$_}} + split //, lc $word; + } + } + return @out ? @out : "()"; +} diff --git a/challenge-207/laurent-rosenfeld/perl/ch-2.pl b/challenge-207/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..d782914006 --- /dev/null +++ b/challenge-207/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; +use feature "say"; + +sub h_index { + my @ordered = sort { $b <=> $a } @_; + for my $i (0..$#ordered) { + return $i if $i+1 > $ordered[$i]; + } + # If we get here, then all papers qualify + return scalar @ordered; +} +for my $test ([<10 8 5 4 3>], [<25 8 5 3 3>], [<12 10 9 5 11>]) { + printf "%-15s => %d\n", "@$test", h_index @$test; +} diff --git a/challenge-207/laurent-rosenfeld/raku/ch-1.raku b/challenge-207/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..c5f6d464c2 --- /dev/null +++ b/challenge-207/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,18 @@ +my @rows; +push @rows, %(map { $_ => True }, $_.comb) + for "qwertyuiop", "asdfghjkl", "zxcvbnm"; + +sub find-kb-word (@in) { + my @out; + for @in -> $word { + for @rows -> %row { + push @out, $word and next + if %row{all $word.lc.comb}:exists; + } + } + return @out; +} +for <Hello Alaska Dad Peace>, <OMG Bye>, + <Power Fad Finish Tower Quit True Omit> -> @test { + say find-kb-word @test; +} diff --git a/challenge-207/laurent-rosenfeld/raku/ch-2.raku b/challenge-207/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..60099cddef --- /dev/null +++ b/challenge-207/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,11 @@ +sub h-index (@citations) { + my @ordered = @citations.sort.reverse; + for 0..@ordered.end -> $i { + return $i if $i+1 > @ordered[$i]; + } + # If we get here, then all papers qualify + return @ordered.elems; +} +for <10 8 5 4 3>, <25 8 5 3 3>, <12 10 9 5 11> -> @test { + say "@test[]".fmt("%-15s => "), h-index @test; +} diff --git a/challenge-207/robert-dicicco/julia/ch-1.jl b/challenge-207/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..d7f7f7071b --- /dev/null +++ b/challenge-207/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,55 @@ +#!/usr/bin/env julia +#= +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-06 +Challenge 207 Keyboard Word ( Julia ) +-------------------------------------- +=# +using Printf + +rows = ["qwertyuiop", "asdfghjkl", "zxcvbnm"] +words = ["Hello", "Alaska", "Dad", "Peace"], ["OMG", "Bye"] +flag = 0 + +function CheckLetters(w) + ln = length(w) + for j in 1:3 + flag = 0 + for x in 1:ln + if (occursin(w[x], rows[j])) + else + flag = 1 + end + end + if (flag == 0) + @printf("\t%s\n",w) + flag = 0 + end + end +end + +for wds in words + @printf("Input: @words = %s\n",wds) + println("Output:") + ln = length(wds) + for j in 1:ln + CheckLetters(lowercase(wds[j])) + end + println(" ") +end + +#= +-------------------------------------- +SAMPLE OUTPUT +julia .\KeyboardWord.jl +Input: @words = ["Hello", "Alaska", "Dad", "Peace"] +Output: + alaska + dad + +Input: @words = ["OMG", "Bye"] +Output: + +-------------------------------------- +=# diff --git a/challenge-207/robert-dicicco/julia/ch-2.jl b/challenge-207/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..e5f96509b4 --- /dev/null +++ b/challenge-207/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,44 @@ +#!/usr/bin/env julia +#= +---------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-07 +Challenge 207 H-Index ( Julia ) +---------------------------------- +=# +using Printf + +citations = [10,8,5,4,3],[25,8,5,3,3] + +function CalcIndex(c) + ln = length(c) + offset = ln + pos = ln + while offset >= 1 + if c[offset] >= pos + @printf("Output: %d\n", pos) + println(" ") + return + else + offset -= 1 + pos -= 1 + end + end +end + +for c in citations + @printf("Input: @citations = %s\n",c) + CalcIndex(c) +end + +#= +---------------------------------- +SAMPLE OUTPUT +julia .\HIndex.jl +Input: @citations = [10, 8, 5, 4, 3] +Output: 4 + +Input: @citations = [25, 8, 5, 3, 3] +Output: 3 +---------------------------------- +=# diff --git a/challenge-207/robert-dicicco/perl/ch-1.pl b/challenge-207/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..4f74af30e4 --- /dev/null +++ b/challenge-207/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +=begin comment +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-06 +Challenge 207 Keyboard Word ( Perl ) +-------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; + +my @rows = ("qwertyuiop", "asdfghjkl", "zxcvbnm"); +my @words = ("Hello Alaska Dad Peace", "OMG Bye"); + +sub do_nothing { + return; +} + +sub CheckLetters { + my $w = shift; + $w = lc($w); + my $flag = 0; + my $ln = length($w); + for my $ndx (0..2) { + for my $c (0..$ln-1) { + my $z = substr($w,$c,1); + if($rows[$ndx] =~ /$z/) { + do_nothing; + } else { + $flag = 1; + } + } + if ($flag == 0) { + say "\t$w"; + } + $flag = 0; + } +} + +for my $wds (@words) { + say "Input: \@array = [$wds]"; + say "Output: "; + my @split_words = split(" ",$wds); + foreach (@split_words) { + CheckLetters($_); + } + say " "; +} + +=begin comment +-------------------------------------- +SAMPLE OUTPUT +perl .\KeyboardWord.pl +Input: @array = [Hello Alaska Dad Peace] +Output: + alaska + dad + +Input: @array = [OMG Bye] +Output: + +-------------------------------------- +=cut + + diff --git a/challenge-207/robert-dicicco/perl/ch-2.pl b/challenge-207/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..5f4afcee9c --- /dev/null +++ b/challenge-207/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +=begin comment +---------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-07 +Challenge 207 H-Index ( Perl ) +================================== +=cut +use strict; +use warnings; +use feature "say"; + +my @citations = ([10,8,5,4,3],[25,8,5,3,3]); + +sub CalcIndex { + my $arr = shift; + my $ln = scalar(@$arr); + my $offset = $ln; + $offset = $ln-1; + my $pos = $ln; + while($offset >= 0) { + if(@$arr[$offset] >= $pos){ + say "Output: $pos\n"; + return; + } else { + $offset--; + $pos--; + } + } +} + +for my $c (@citations) { + say "Input: \@citations = [@$c]"; + CalcIndex($c); +} + +=begin comment +================================== +SAMPLE OUTPUT +perl .\HIndex.pl +Input: @citations = [10 8 5 4 3] +Output: 4 + +Input: @citations = [25 8 5 3 3] +Output: 3 +================================== +=cut diff --git a/challenge-207/robert-dicicco/python/ch-1.py b/challenge-207/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..850c89b0ce --- /dev/null +++ b/challenge-207/robert-dicicco/python/ch-1.py @@ -0,0 +1,47 @@ +#/usr/bin/env python +''' +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-06 +Challenge 207 Keyboard Word ( Python ) +-------------------------------------- +''' + +rows = ["qwertyuiop", "asdfghjkl", "zxcvbnm"] +words = ["Hello", "Alaska", "Dad", "Peace"], ["OMG", "Bye"] +flag = 0 + +def CheckLetters(w): + ln = len(w) + for j in range(3): + flag = 0 + for x in range(ln): + if (w[x] in rows[j]): + pass + else: + flag = 1 + if flag == 0 : + print("\t",w) + flag = 0 + +for wds in words: + print("Input: @words = ",wds) + print("Output: ") + ln = len(wds) + for j in range(0,ln): + CheckLetters(wds[j].lower()) + print(" ") + +''' +-------------------------------------- +SAMPLE OUTPUT +python .\KeyboardWord.py +Input: @words = ['Hello', 'Alaska', 'Dad', 'Peace'] +Output: + alaska + dad + +Input: @words = ['OMG', 'Bye'] +Output: + +''' diff --git a/challenge-207/robert-dicicco/python/ch-2.py b/challenge-207/robert-dicicco/python/ch-2.py new file mode 100644 index 0000000000..4327ca5d04 --- /dev/null +++ b/challenge-207/robert-dicicco/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +''' +---------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-07 +Challenge 207 H-Index ( Python ) +================================== +''' +citations = [10,8,5,4,3],[25,8,5,3,3] + +def CalcIndex(c): + ln = len(c) + offset = ln - 1 + pos = ln + while offset >= 0 : + if c[offset] >= pos : + print("Output: ",pos,"\n") + return + else : + offset -= 1 + pos -= 1 + +for c in citations: + print("Input: @citations =",c) + CalcIndex(c) + +''' +---------------------------------- +SAMPLE OUTPUT +python .\HIndex.py +Input: @citations = [10, 8, 5, 4, 3] +Output: 4 + +Input: @citations = [25, 8, 5, 3, 3] +Output: 3 +---------------------------------- +''' diff --git a/challenge-207/robert-dicicco/raku/ch-1.raku b/challenge-207/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..ee2093a26c --- /dev/null +++ b/challenge-207/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,58 @@ +#!/usr/bin/env raku +#`( +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-06 +Challenge 207 Keyboard Word ( Raku ) +-------------------------------------- +) + +use v6; + +my @rows = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]; +my @words = [["Hello Alaska Dad Peace"], ["OMG Bye"]]; + +sub CheckLetters($tw is copy) { + my $flag = 0; + $tw=$tw.lc; + for 0..2 -> $ndx { + for 0..$tw.chars-1 -> $c { + my $z = substr($tw,$c,1); + (@rows[$ndx].contains($z)) ?? (next) !! ($flag = 1); + } + if $flag == 0 { say "\t$tw" }; + $flag = 0; + } +} + +sub GetWords(@wds) { + my $ln = @wds.elems; + my @tw = @wds.split(' '); + for 0..$ln-1 -> $i { + my $test_word = @tw[$i]; + CheckLetters($test_word); + } + say " "; +} + +for (@words) -> @w { + say "Input: \@words = ",@w; + say "Output: "; + my @test_words = @w.split(' '); + GetWords(@test_words); +} + +#`( +-------------------------------------- +SAMPLE OUTPUT + raku .\KeyboardWord.rk +Input: @words = [Hello Alaska Dad Peace] +Output: + alaska + dad + +Input: @words = [OMG Bye] +Output: + +-------------------------------------- +) diff --git a/challenge-207/robert-dicicco/raku/ch-2.raku b/challenge-207/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..754c3266d5 --- /dev/null +++ b/challenge-207/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,47 @@ +#!/usr/bin/env raku +#`( +---------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-07 +Challenge 207 H-Index ( Raku) +================================== +) +use v6; + +my @citations = ([10,8,5,4,3],[25,8,5,3,3]); + +sub CalcIndex(@c) { + my $ln = @c.elems; + my $offset = $ln; + $offset = $ln-1; + my $pos = $ln; + while $offset >= 0 { + if @c[$offset] >= $pos { + say "Output: $pos\n"; + return; + } else { + $offset--; + $pos--; + } + } + +} + +for (@citations) -> @c { + say "Input: \@citations = ",@c; + CalcIndex(@c); +} + +#`( +---------------------------------- +SAMPLE OUTPUT + raku .\HIndex.rk +Input: @citations = [10 8 5 4 3] +Output: 4 + +Input: @citations = [25 8 5 3 3] +Output: 3 +---------------------------------- +) + + diff --git a/challenge-207/robert-dicicco/ruby/ch-1.rb b/challenge-207/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..c709241a3c --- /dev/null +++ b/challenge-207/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +=begin +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-06 +Challenge 207 Keyboard Word ( Ruby ) +-------------------------------------- +=end + +$rows = ["qwertyuiop", "asdfghjkl", "zxcvbnm"] +words = [["Hello Alaska Dad Peace"], ["OMG Bye"]] +$flag = 0 + +def CheckLetters(w) + w = w.downcase # convert to lc + ln = w.length + (0..2).each do |str| + $flag = 0 + (0..ln-1).each do |x| + unless $rows[str].include? w[x] + $flag += 1 + end + end + if ($flag == 0) + puts("\t#{w}") + end + end + end + +def GetWords(tws) + tws.each do |tw| + tw.delete! '[]\"' + #puts("#{tw}") + CheckLetters(tw) + end + puts(" ") +end + +words.each do |wds| + puts("Input: @words = #{wds}") + puts("Output: ") + test_words = wds.to_s.split + GetWords(test_words) +end +=begin +------------------------------------------ +SAMPLE OUTPUT +ruby .\KeyboardWord.rb +Input: @words = ["Hello Alaska Dad Peace"] +Output: + alaska + dad + +Input: @words = ["OMG Bye"] +Output: +------------------------------------------- +=end + + |
