1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
}
|