|
| 1 | +#!/usr/bin/env perl |
| 2 | +# |
| 3 | +=head1 Task 1: Broken Keys |
| 4 | +
|
| 5 | +Submitted by: Mohammad Sajid Anwar |
| 6 | +
|
| 7 | +You have a broken keyboard which sometimes type a character more than once. |
| 8 | +
|
| 9 | +You are given a string and actual typed string. |
| 10 | +
|
| 11 | +Write a script to find out if the actual typed string is meant for the given |
| 12 | +string. |
| 13 | +
|
| 14 | +=head2 Example 1 |
| 15 | +
|
| 16 | + Input: $name = "perl", $typed = "perrrl" |
| 17 | + Output: true |
| 18 | +
|
| 19 | + Here "r" is pressed 3 times instead of 1 time. |
| 20 | +
|
| 21 | +=head2 Example 2 |
| 22 | +
|
| 23 | + Input: $name = "raku", $typed = "rrakuuuu" |
| 24 | + Output: true |
| 25 | +
|
| 26 | +=head2 Example 3 |
| 27 | +
|
| 28 | + Input: $name = "python", $typed = "perl" |
| 29 | + Output: false |
| 30 | +
|
| 31 | +=head2 Example 4 |
| 32 | +
|
| 33 | + Input: $name = "coffeescript", $typed = "cofffeescccript" |
| 34 | + Output: true |
| 35 | +
|
| 36 | +=cut |
| 37 | + |
| 38 | +use strict; |
| 39 | +use warnings; |
| 40 | +use Test2::V0 -no_srand => 1; |
| 41 | +use Data::Dumper; |
| 42 | + |
| 43 | +my $cases = [ |
| 44 | + [["perl", "perrrl"], 1, "Example 1"], |
| 45 | + [["raku", "rrakuuuu"], 1, "Example 2"], |
| 46 | + [["python", "perl"], 0, "Example 3"], |
| 47 | + [["coffeescript", "cofffeescccript"], 1, "Example 4"], |
| 48 | +]; |
| 49 | + |
| 50 | +sub broken_keys |
| 51 | +{ |
| 52 | + my $name = $_[0]->[0]; |
| 53 | + my $typed = $_[0]->[1]; |
| 54 | + |
| 55 | + my @name = split //, $name; |
| 56 | + my @typed = split //, $typed; |
| 57 | + |
| 58 | + my $name_idx = 0; |
| 59 | + for my $typed_idx (0 .. $#typed) { |
| 60 | + if (!defined $name[$name_idx] or $name[$name_idx] ne $typed[$typed_idx]) { |
| 61 | + next if $typed_idx > 0 && $typed[$typed_idx] eq $typed[$typed_idx - 1]; |
| 62 | + return 0; |
| 63 | + } else { |
| 64 | + $name_idx++; |
| 65 | + } |
| 66 | + } |
| 67 | + return 1; |
| 68 | +} |
| 69 | + |
| 70 | +for (@$cases) { |
| 71 | + is(broken_keys($_->[0]), $_->[1], $_->[2]); |
| 72 | +} |
| 73 | +done_testing(); |
| 74 | + |
| 75 | +exit 0; |
0 commit comments