diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-08 19:56:39 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-08 19:56:39 +0000 |
| commit | 8f93e9fc8bf12945d8b2ca9f95f72954f168c079 (patch) | |
| tree | c5409f1ad71b653930237b3e440065af2c5a1a19 | |
| parent | 29ed6fe487eded52298d199ece043e69119fbf02 (diff) | |
| parent | eb0c9ee78f1f917e4416def0ee8ec41f40702048 (diff) | |
| download | perlweeklychallenge-club-8f93e9fc8bf12945d8b2ca9f95f72954f168c079.tar.gz perlweeklychallenge-club-8f93e9fc8bf12945d8b2ca9f95f72954f168c079.tar.bz2 perlweeklychallenge-club-8f93e9fc8bf12945d8b2ca9f95f72954f168c079.zip | |
Merge pull request #7685 from boblied/master
Week 207
| -rw-r--r-- | challenge-207/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-207/bob-lied/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-207/bob-lied/perl/ch-1.pl | 60 | ||||
| -rw-r--r-- | challenge-207/bob-lied/perl/ch-2.pl | 59 |
4 files changed, 123 insertions, 3 deletions
diff --git a/challenge-207/bob-lied/README b/challenge-207/bob-lied/README index 4dcb1ff367..bb94b2f9bc 100644 --- a/challenge-207/bob-lied/README +++ b/challenge-207/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 206 by Bob Lied +Solutions to weekly challenge 207 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-206/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-206/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-207/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-207/bob-lied diff --git a/challenge-207/bob-lied/blog.txt b/challenge-207/bob-lied/blog.txt new file mode 100644 index 0000000000..79de48a164 --- /dev/null +++ b/challenge-207/bob-lied/blog.txt @@ -0,0 +1 @@ +https://dev.to/boblied/pwc-207-im-pickin-up-h-citatations-p0p diff --git a/challenge-207/bob-lied/perl/ch-1.pl b/challenge-207/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..6341985f6c --- /dev/null +++ b/challenge-207/bob-lied/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Week 207 Task 1 Keyboard Words +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of words. +# Write a script to print all the words in the given array that can be types +# using alphabet on only one row of the keyboard. +# Let us assume the keys are arranged as below: +# Row 1: qwertyuiop +# Row 2: asdfghjkl +# Row 3: zxcvbnm +# +# Example 1 Input: @words = ("Hello","Alaska","Dad","Peace") +# Output: ("Alaska","Dad") +# Example 2 Input: @array = ("OMG","Bye") +# Output: () +#============================================================================= + +use v5.36; + +no warnings "experimental::builtin"; +use builtin qw/trim/; + +use List::Util qw/any/; + + +use Getopt::Long; +my $DoTest = 0; + +GetOptions("test" => \$DoTest); +exit(!runTest()) if $DoTest; + +say "(", join(", ", map { qq("$_") } keyboardWord(\@ARGV)->@*), ")"; + + +sub isKeyboardWord($word) +{ +state @Keyboard = ( qr/[qwertyuiop]+/, qr/[asdfghjkl]+/, qr/[zxcvbnm]+/ ); + return any { $word =~ /\A$_\Z/ } @Keyboard; +} + +sub keyboardWord($list) +{ + my @result = grep { isKeyboardWord(lc trim $_) } $list->@*; + return \@result; +} + +sub runTest +{ + use Test2::V0; + + is( keyboardWord([qw(Hello Alaska Dad Peace)]), [qw(Alaska Dad)], "Example 1"); + is( keyboardWord([qw(OMG Bye)]), [qw()], "Example 2"); + + done_testing; +} + diff --git a/challenge-207/bob-lied/perl/ch-2.pl b/challenge-207/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..027d3aa90f --- /dev/null +++ b/challenge-207/bob-lied/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Week 207 Task 2 H-Index +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers containing citations a researcher has +# received for each paper. +# Write a script to compute the researcher’s H-Index. For more information +# please checkout the wikipedia page. https://en.wikipedia.org/wiki/H-index +# +# The H-Index is the largest number h such that h articles have at least h +# citations each. For example, if an author has five publications, with 9, 7, +# 6, 2, and 1 citations (ordered from greatest to least), then the author’s +# h-index is 3, because the author has three publications with 3 or more +# citations. However, the author does not have four publications with 4 or +# more citations. +# +# Example 1 Input: @citations = (10,8,5,4,3) Output: 4 +# Because the 4th publication has 4 citations and the 5th has only 3. +# Example 2 Input: @citations = (25,8,5,3,3) Output: 3 +# The H-Index is 3 because the fourth paper has only 3 citations. +# +# What this amounts to is finding the largest i in a sorted array such +# that x[i] >= i +#============================================================================= + +use v5.36; + +use List::Util qw/first/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say H_index( @ARGV ); + +sub H_index(@cite) +{ + my @sorted = sort { $b <=> $a } @cite; + my $h = first { $sorted[$_] < $_+1 } 0 .. $#sorted; + return $h; +} + +sub runTest +{ + use Test2::V0; + + is( H_index(10,8,5,4,3), 4, "Example 1"); + is( H_index(25,8,5,3,3), 3, "Example 2"); + is( H_index( 1,1,1,1,1), 1, "Example 2"); + + done_testing; +} + |
