diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-10 21:44:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-10 21:44:57 +0000 |
| commit | f243f79ae0fc6e3f6748ced34c42cf51e13f6009 (patch) | |
| tree | 88baa4f9f0b1bf4f1de14946417014216a90c3a3 | |
| parent | 2dfb0bc1a3779d88d7fc6fa881ba0187b1b3f38a (diff) | |
| parent | 19250ef7f939b5b1ab6b4ef3b83d6d4bcd395e02 (diff) | |
| download | perlweeklychallenge-club-f243f79ae0fc6e3f6748ced34c42cf51e13f6009.tar.gz perlweeklychallenge-club-f243f79ae0fc6e3f6748ced34c42cf51e13f6009.tar.bz2 perlweeklychallenge-club-f243f79ae0fc6e3f6748ced34c42cf51e13f6009.zip | |
Merge pull request #7699 from mattneleigh/pwc207
new file: challenge-207/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-207/mattneleigh/perl/ch-1.pl | 125 | ||||
| -rwxr-xr-x | challenge-207/mattneleigh/perl/ch-2.pl | 70 |
2 files changed, 195 insertions, 0 deletions
diff --git a/challenge-207/mattneleigh/perl/ch-1.pl b/challenge-207/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..e4eb3f4478 --- /dev/null +++ b/challenge-207/mattneleigh/perl/ch-1.pl @@ -0,0 +1,125 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @keyboard_rows = ( + "qwertyuiop", + "asdfghjkl", + "zxcvbnm" +); +my @word_lists = ( + # Given cases + [ "Hello", "Alaska", "Dad", "Peace" ], + [ "OMG", "Bye" ], + + # Additional test cases + [ "Hello", "world", "woot", "hooray" ] +); + +print("\n"); +foreach my $word_list (@word_lists){ + printf( + "Input: \@words = (%s)\nOutput: (%s)\n\n", + list_to_quoted_string(@{$word_list}), + list_to_quoted_string( + single_row_words(\@keyboard_rows, $word_list) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine which words in a list can be typed using keys from only one row of +# a specified keyboard layout +# Takes two arguments: +# * A reference to a list of strings comprising the letters of each row of a +# keyboard (e.g. [ "qwertyuiop", asdfghjkl", "zxcvbnm" ] ) +# * A reference to a list of words to examine (e.g. [ "Hello", "world", "woot", +# "hooray" ] ) +# Returns: +# * A list of words from the second list that can be typed using only one row +# of the keyboard layout described in the first list (e.g. ( "woot") ) +# NOTE: Letter comparisons are done case-insensitively +################################################################################ +sub single_row_words{ + + # Compile regexes based on each row forming + # a character class with the keys in said + # row as members; we will match case- + # insensitively + my @keyboard_regexes = map( + qr/[$_]/i, + @{$ARG[0]} + ); + + return( + map( + # Run this block on every string form the + # second argument + { + my $ct = 0; + + # Check each string for the presence of + # characters from each keyboard row, and + # count up the matches + foreach my $row_regex (@keyboard_regexes){ + $ct++ + if($_ =~ /$row_regex/); + + last + if($ct > 1); + } + + # Pass this string along to the returned + # list only if exactly one keyboard row + # matched + ($ct != 1) ? () : $_; + + } + @{$ARG[1]} + ) + ); + +} + + + +################################################################################ +# Build a quoted, comma-separated string out of the contents of a list +# Takes one argument: +# * The list (e.g. ( 1, 2, 3, 4 ) +# Returns: +# * A quoted, comma-separated string containing the contents of the list (e.g. +# ""1", "2", "3", "4"") +################################################################################ +sub list_to_quoted_string{ + + return( + # (2) Join the quoted strings together + # with commas + join( + ", ", + # (1) Put quotes around each list member + map( + "\"".$_."\"", + @ARG + ) + ) + ); + +} + + + diff --git a/challenge-207/mattneleigh/perl/ch-2.pl b/challenge-207/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..8500e4ddf1 --- /dev/null +++ b/challenge-207/mattneleigh/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @citations_lists = ( + # Given cases + [ 10, 8, 5, 4, 3 ], + [ 25, 8, 5, 3, 3 ], + + # Additional test cases + [ 9, 7, 6, 2, 1 ], + [ 1, 1, 1, 1, 1, 1, 1 ] +); + +print("\n"); +foreach my $citations (@citations_lists){ + printf( + "Input: \@citations = (%s)\nOutput: %d\n\n", + join(", ", @{$citations}), + calculate_h_index(@{$citations}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + +################################################################################ +# Calculate the h-index for a list of citation counts; see +# https://en.wikipedia.org/wiki/H-index for more details about the significance +# of this number +# Takes one argument: +# * A list of citation counts, one per paper or journal article +# Returns: +# * The h-index of the collection of papers described by the citation count +# list +################################################################################ +sub calculate_h_index{ + + my $h_index = 1; + + # Sort the argument list in descending + # order + @ARG = sort({ $b <=> $a } @ARG); + + # Loop while $h_index is less than or equal + # to the current most-cited paper; this will + # overshoot by one in all cases + while($h_index <= $ARG[0]){ + # Increment the h-index and remove the most- + # cited paper from the list + $h_index++; + shift(@ARG); + } + + # Decrement and return + return(--$h_index); + +} + + + |
