aboutsummaryrefslogtreecommitdiff
path: root/challenge-207
diff options
context:
space:
mode:
authorAli <adeadmarshal@gmail.com>2023-03-12 15:34:03 +0330
committerGitHub <noreply@github.com>2023-03-12 15:34:03 +0330
commitf03f41fc92c57f5d8e548b4ee32a31b78082b332 (patch)
tree20d0d64a5ba8fcfa0aced89f20a8cbbaea629340 /challenge-207
parentec8a291e57738084a11dd4870e651e462eab41f5 (diff)
parent33c228c577d6ca63cba6571081a8a43dec55471a (diff)
downloadperlweeklychallenge-club-f03f41fc92c57f5d8e548b4ee32a31b78082b332.tar.gz
perlweeklychallenge-club-f03f41fc92c57f5d8e548b4ee32a31b78082b332.tar.bz2
perlweeklychallenge-club-f03f41fc92c57f5d8e548b4ee32a31b78082b332.zip
Merge branch 'manwar:master' into TWC207
Diffstat (limited to 'challenge-207')
-rw-r--r--challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md121
-rw-r--r--challenge-207/pip/perl/ch-1.pl40
-rw-r--r--challenge-207/pip/perl/ch-2.pl26
-rw-r--r--challenge-207/pip/raku/ch-1.raku40
-rw-r--r--challenge-207/pip/raku/ch-2.raku26
5 files changed, 253 insertions, 0 deletions
diff --git a/challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md b/challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md
new file mode 100644
index 0000000000..cf5b28321d
--- /dev/null
+++ b/challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md
@@ -0,0 +1,121 @@
+# THE WEEKLY CHALLENGE - 207
+
+###
+
+https://theweeklychallenge.org/blog/perl-weekly-challenge-207/
+
+**March 11th, 2023**
+
+## Task 1 - How to Check if a Word is Typable with a Single Keyboard Row
+
+We all know the feeling of typing out a long word and suddenly realizing that you had to switch keyboard rows to do it. Well, now there's a way to check if a word can be typed using only one row of a standard QWERTY keyboard!
+
+This Perl code snippet uses a hash map to store the keyboard layout, then checks it against a given word to see if it can be typed using only one row:
+
+```perl
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+my @keyboard_rows = qw(qwertyuiop asdfghjkl zxcvbnm);
+my %keyboard_map;
+
+# Create a hash map to store the keyboard layout
+for my $i ( 0 .. 2 ) {
+ my $row = $keyboard_rows[$i];
+ $keyboard_map{$_} = $i + 1 for split( '', $row );
+}
+
+# Function to check if a word can be typed using only one row of the keyboard
+sub is_single_row_word {
+ my ($word) = @_;
+ my $row = $keyboard_map{ lc( substr( $word, 0, 1 ) ) };
+ for my $c ( split( '', lc($word) ) ) {
+ return 0 if $keyboard_map{$c} != $row;
+ }
+ return 1;
+}
+
+# Test example 1
+my @words1 = ( "Hello", "Alaska", "Dad", "Peace" );
+my @single_row_words1 = grep { is_single_row_word($_) } @words1;
+is_deeply( \@single_row_words1, [ "Alaska", "Dad" ], "Example 1" );
+
+# Test example 2
+my @words2 = ( "OMG", "Bye" );
+my @single_row_words2 = grep { is_single_row_word($_) } @words2;
+is_deeply( \@single_row_words2, [], "Example 2" );
+
+done_testing();
+```
+
+This code uses the `grep` function to filter a list of words and return the ones that can be typed using only one row of the keyboard. For example, the list `"Hello", "Alaska", "Dad", "Peace"` would return `"Alaska", "Dad"` as these are the only two words that can be typed with a single row.
+
+Now you can be sure that you're not typing words that require two different keyboard rows!
+
+## Task 2 - Unlocking the Mysteries of the H-Index with Perl
+
+Have you ever wanted to know your H-Index? It's not a mystery anymore! With the help of Perl, you can figure out your own H-Index in no time.
+
+An H-Index is the largest number h such that h articles have at least h citations each. This is used to measure the impact of a researcher's work.
+
+Here is how to calculate your H-Index using Perl.
+
+First, you need to use strict and warnings in your code.
+
+```perl
+use strict;
+use warnings;
+```
+
+Then, you should use feature 'say' to make your code more readable.
+
+```perl
+use feature 'say';
+```
+
+Next, you will need to include the Test::More module. This will allow you to run tests on your code.
+
+```perl
+use Test::More;
+```
+
+Now, you can create a subroutine to calculate your H-Index.
+
+```perl
+sub h_index {
+ my @citations = @_;
+ my $n = scalar @citations;
+ my $h = 0;
+ my @sorted_citations = sort { $b <=> $a } @citations;
+
+ for ( my $i = 0 ; $i < $n ; $i++ ) {
+ if ( $sorted_citations[$i] >= $i + 1 ) {
+ $h = $i + 1;
+ }
+ else {
+ last;
+ }
+ }
+ return $h;
+}
+```
+
+Finally, you can run tests on your code to make sure it is working correctly.
+
+```perl
+# Run the tests
+my @citations_1 = ( 10, 8, 5, 4, 3 );
+my $h_index_1 = h_index(@citations_1);
+is( $h_index_1, 4, "Test Example 1" );
+
+my @citations_2 = ( 25, 8, 5, 3, 3 );
+my $h_index_2 = h_index(@citations_2);
+is( $h_index_2, 3, "Test Example 2" );
+
+done_testing();
+```
+
+And there you have it! You can now calculate your own H-Index using Perl.
diff --git a/challenge-207/pip/perl/ch-1.pl b/challenge-207/pip/perl/ch-1.pl
new file mode 100644
index 0000000000..2bc64d9bd0
--- /dev/null
+++ b/challenge-207/pip/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #207 - Pip Stuart
+# Task1: Keyboard Word: Submitted by: Mohammad S Anwar; 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:
+# Row1: qwertyuiop
+# Row2: asdfghjkl
+# Row3: zxcvbnm
+# Example1:
+# In-put: @words = ("Hello","Alaska","Dad","Peace")
+# Output: ( "Alaska","Dad" )
+# Example2:
+# In-put: @words = ("OMG","Bye")
+# Output: ( )
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N38LGYRO';
+sub KbWd {my @wrdz = @_;my @rwdz = ();my @rowz = ({}, {}, {});
+ for (split('', 'qwertyuiop')) { $rowz[0]{$_} = 1; }
+ for (split('', 'asdfghjkl' )) { $rowz[1]{$_} = 1; }
+ for (split('', 'zxcvbnm' )) { $rowz[2]{$_} = 1; }
+ for (@wrdz) {my @lcwd = split('', lc($_));my $airf = 0;
+ for my $rown (0..2) {
+ if ( exists($rowz[$rown]{$lcwd[0]})) { $airf = 1;
+ for my $letr (@lcwd) { # loop thru all of @LowerCasedWorD
+ if (!exists($rowz[$rown]{$letr })) { $airf = 0; last; }
+ } # clear AllInRowFlag if any letter is not in the same row
+ }
+ }
+ if ($airf) { push(@rwdz, $_); }
+ }
+ printf( "(\"%s\") => ", join('","', @wrdz));
+ if (@rwdz) { printf("(\"%s\");\n" , join('","', @rwdz)); }
+ else { say "();"; }
+ return(@rwdz);
+}
+if (@ARGV) {
+ KbWd(@ARGV);
+} else {
+ KbWd("Hello","Alaska","Dad","Peace"); # => ("Alaska","Dad");
+ KbWd("OMG","Bye" ); # => ("");
+}
diff --git a/challenge-207/pip/perl/ch-2.pl b/challenge-207/pip/perl/ch-2.pl
new file mode 100644
index 0000000000..1e2e37c947
--- /dev/null
+++ b/challenge-207/pip/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #207 - Pip Stuart
+# Task2: H-Index: Submitted by: Mohammad S Anwar; 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.
+# 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.
+# Example1:
+# In-put: @citations = (10,8,5,4,3)
+# Output: 4 Because the 4th publication has 4 citations and the 5th has only 3.
+# Example2:
+# In-put: @citations = (25,8,5,3,3)
+# Output: 3 The H-Index is 3 because the fourth paper has only 3 citations.
+# Last date to submit the solution 23:59 (UK Time) Sunday 12th March 2023.
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N38LDAYS';
+sub HNdx {my @ctnz = sort { $b <=> $a } @_;my $hndx = 0;
+ while ($hndx < @ctnz && $ctnz[$hndx] > $hndx) { $hndx++; }
+ printf("(%-10s) => %d;\n", join(',', @ctnz), $hndx);
+ return($hndx);
+}
+if (@ARGV) {
+ HNdx(@ARGV);
+} else {
+ HNdx(10,8,5,4,3); # => 4;
+ HNdx(25,8,5,3,3); # => 3;
+}
diff --git a/challenge-207/pip/raku/ch-1.raku b/challenge-207/pip/raku/ch-1.raku
new file mode 100644
index 0000000000..6d9cb7a43f
--- /dev/null
+++ b/challenge-207/pip/raku/ch-1.raku
@@ -0,0 +1,40 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #207 - Pip Stuart
+# Task1: Keyboard Word: Submitted by: Mohammad S Anwar; 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:
+# Row1: qwertyuiop
+# Row2: asdfghjkl
+# Row3: zxcvbnm
+# Example1:
+# In-put: @words = ("Hello","Alaska","Dad","Peace")
+# Output: ( "Alaska","Dad" )
+# Example2:
+# In-put: @words = ("OMG","Bye")
+# Output: ( )
+use v6;my $d8VS='N39L7wLC';
+sub KbWd {my @wrdz = @_;my @rwdz = ();my @rowz = ({}, {}, {});
+ for (split('', 'qwertyuiop', :skip-empty)) { @rowz[0]{$_} = 1; }
+ for (split('', 'asdfghjkl' , :skip-empty)) { @rowz[1]{$_} = 1; }
+ for (split('', 'zxcvbnm' , :skip-empty)) { @rowz[2]{$_} = 1; }
+ for (@wrdz) {my @lcwd = split('', lc($_), :skip-empty);my $airf = 0;
+ for 0..2 -> $rown {
+ if ( @rowz[$rown]{@lcwd[0]}:exists ) { $airf = 1;
+ for @lcwd -> $letr { # loop thru all of @LowerCasedWorD
+ if (!(@rowz[$rown]{$letr }:exists)) { $airf = 0; last; }
+ } # clear AllInRowFlag if any letter is not in the same row
+ }
+ }
+ if ($airf) { push(@rwdz, $_); }
+ }
+ printf( "(\"%s\") => ", join('","', @wrdz));
+ if (@rwdz) { printf("(\"%s\");\n" , join('","', @rwdz)); }
+ else { say "();"; }
+ return(@rwdz);
+}
+if (@*ARGS) {
+ KbWd(@*ARGS);
+} else {
+ KbWd("Hello","Alaska","Dad","Peace"); # => ("Alaska","Dad");
+ KbWd("OMG","Bye" ); # => ("");
+}
diff --git a/challenge-207/pip/raku/ch-2.raku b/challenge-207/pip/raku/ch-2.raku
new file mode 100644
index 0000000000..6dc1ab005a
--- /dev/null
+++ b/challenge-207/pip/raku/ch-2.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #207 - Pip Stuart
+# Task2: H-Index: Submitted by: Mohammad S Anwar; 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.
+# 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.
+# Example1:
+# In-put: @citations = (10,8,5,4,3)
+# Output: 4 Because the 4th publication has 4 citations and the 5th has only 3.
+# Example2:
+# In-put: @citations = (25,8,5,3,3)
+# Output: 3 The H-Index is 3 because the fourth paper has only 3 citations.
+# Last date to submit the solution 23:59 (UK Time) Sunday 12th March 2023.
+use v6;my $d8VS='N39L81pR';
+sub HNdx {my @ctnz = sort -*, @_;my $hndx = 0;
+ while ($hndx < @ctnz.elems && @ctnz[$hndx] > $hndx) { $hndx++; }
+ printf("(%-10s) => %d;\n", join(',', @ctnz), $hndx);
+ return($hndx);
+}
+if (@*ARGS) {
+ HNdx(@*ARGS);
+} else {
+ HNdx(10,8,5,4,3); # => 4;
+ HNdx(25,8,5,3,3); # => 3;
+}