You have a broken keyboard which sometimes type a character more than once.
You are given a string and actual typed string.
Write a script to find out if the actual typed string is meant for the given string.
ch-1.pl
#!/usr/bin/env perl
use v5.38;
use warnings;
use feature 'signatures';
no warnings 'experimental::signatures';
## no critic (Subroutines::ProhibitSubroutinePrototypes)
use Type::Params qw(compile);
use Types::Standard qw(Str);
=pod
=head1 NAME
ch-1.pl - Broken Keys
=head1 SYNOPSIS
perl ch-1.pl perl perrrl
perl ch-1.pl # runs the embedded tests
=head1 DESCRIPTION
Checks whether the typed string could have been produced from the intended name
when a key can be long-pressed, i.e. a character may appear repeated one or
more times consecutively.
=cut
my $ARGS_CHECK = compile( Str, Str );
sub is_long_pressed ($name, $typed) {
( $name, $typed ) = $ARGS_CHECK->( $name, $typed );
return 0 if length($typed) < length($name);
my @n = split //, $name;
my @t = split //, $typed;
my $i = 0;
my $j = 0;
while ( $j <= $#t ) {
if ( $i <= $#n && $n[$i] eq $t[$j] ) {
++$i;
++$j;
}
elsif ( $j > 0 && $t[$j] eq $t[ $j - 1 ] ) {
++$j;
}
else {
return 0;
}
}
return $i == @n ? 1 : 0;
}
sub _run_cli (@args) {
if ( !@args ) {
_run_tests();
return;
}
die "Usage: perl $0 \n" if @args != 2;
my ( $name, $typed ) = @args;
my $out = is_long_pressed( $name, $typed ) ? 'true' : 'false';
say qq{Input: \$name = "$name", \$typed = "$typed"};
say "Output: $out";
}
sub _run_tests {
require Test::More;
Test::More->import;
my @cases = (
{ label => 'Example 1', name => 'perl', typed => 'perrrl', expected => 1 },
{ label => 'Example 2', name => 'raku', typed => 'rrakuuuu', expected => 1 },
{ label => 'Example 3', name => 'python', typed => 'perl', expected => 0 },
{ label => 'Example 4', name => 'coffeescript', typed => 'cofffeescccript', expected => 1 },
);
Test::More::plan( tests => scalar @cases );
for my $case (@cases) {
my $got = is_long_pressed( $case->{name}, $case->{typed} ) ? 1 : 0;
Test::More::is( $got, $case->{expected}, $case->{label} );
}
}
_run_cli(@ARGV);
=pod
=head1 FUNCTIONS
=head2 is_long_pressed($name, $typed)
Returns a boolean indicating whether C<$typed> can be produced from C<$name>
using long presses.
=cut
ch-1.py
#!/usr/bin/env python3
"""Broken Keys - Perl Weekly Challenge 313 task 1."""
from __future__ import annotations
from collections.abc import Sequence
import sys
import unittest
def is_long_pressed(name: str, typed: str) -> bool:
"""Return True if typed could be produced from name via long-pressing keys."""
if len(typed) < len(name):
return False
i = 0
j = 0
while j < len(typed):
if i < len(name) and name[i] == typed[j]:
i += 1
j += 1
elif j > 0 and typed[j] == typed[j - 1]:
j += 1
else:
return False
return i == len(name)
class BrokenKeysExamples(unittest.TestCase):
"""Example-based tests from the specification."""
def test_example_1(self) -> None:
self.assertTrue(is_long_pressed("perl", "perrrl"))
def test_example_2(self) -> None:
self.assertTrue(is_long_pressed("raku", "rrakuuuu"))
def test_example_3(self) -> None:
self.assertFalse(is_long_pressed("python", "perl"))
def test_example_4(self) -> None:
self.assertTrue(is_long_pressed("coffeescript", "cofffeescccript"))
def main(argv: Sequence[str] | None = None) -> None:
"""Command-line interface."""
args = list(sys.argv[1:] if argv is None else argv)
if not args:
unittest.main(argv=[sys.argv[0]])
return
if len(args) != 2:
raise SystemExit("Usage: python3 ch-1.py ")
name, typed = args
result = "true" if is_long_pressed(name, typed) else "false"
print(f'Input: $name = "{name}", $typed = "{typed}"')
print(f"Output: {result}")
if __name__ == "__main__":
main()