Perl: King of Strings
Task 1: Keyboard Word
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: ()
Let's do this week in NodeJS. But wait ... this task is nothing more than just string processing or pattern matching, and since Perl is king when it comes to string processing, let's 'regexify' this task:
my $q_row = qr/[qwertyuiop]+/i;
my $a_row = qr/[asdfghjkl]+/i;
my $z_row = qr/[zxcvbnm]+/i;
for my $w (@words) {
if ($w =~ /^($q_row|$a_row|$z_row)$/i) {
say "$w";
}
}
That simple! Here's the solution in JS (albeit w/ a bit more boilerplate)
Happy hacking!