/
ch-1.pl
88 lines (75 loc) · 2.37 KB
/
ch-1.pl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2024, Bob Lied
#=============================================================================
# ch-1.pl Perl Weekly Challenge 255 Task 1 Odd Character
#=============================================================================
# You are given two strings, $s and $t. The string $t is generated using
# the shuffled characters of the string $s with an additional character.
# Write a script to find the additional character in the string $t.
# Example 1 Input: $s = "Perl" $t = "Preel" Output: "e"
# Example 2 Input: $s = "Weekly" $t = "Weeakly" Output: "a"
# Example 3 Input: $s = "Box" $t = "Boxy" Output: "y"
#=============================================================================
use v5.38;
use builtin qw/true false/; no warnings "experimental::builtin";
use Getopt::Long;
my $Verbose = 0;
my $DoTest = 0;
GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
exit(!runTest()) if $DoTest;
say oddChar(@ARGV[0, 1]);
sub oddChar($s, $t)
{
my %freq;
$freq{$_}++ for split(//, $s);
$freq{$_}-- for split(//, $t);
my @remain = grep { $freq{$_} != 0 } keys %freq;
if ( @remain > 1 )
{
die "Too many differences $s => $t, [@remain]"
}
elsif ( @remain == 0 )
{
die "No difference between $s and $t"
}
elsif ( $freq{$remain[0]} != -1 )
{
die "Not exactly one diff for @remain"
}
return $remain[0];
}
sub oddChar2($s, $t)
{
die qq("$t" has wrong length compared to "$s")
if length($t) != length($s)+1;
for my $c ( split(//, $t) )
{
if ( ( my $i = index($s, $c) ) < 0 )
{
return $c;
}
else
{
substr($s, $i, 1) = "";
}
}
}
sub runTest
{
use Test2::V0;
is( oddChar("Perl", "Preel"), "e", "Example 1");
is( oddChar("Weekly", "Weeakly"), "a", "Example 2");
is( oddChar("Box", "Boxy"), "y", "Example 3");
like(
dies { oddChar("xyzzy","xyzzy") }, qr/No difference/,
"Dies if no difference");
like(
dies { oddChar("xyzzy","plover") }, qr/Too many/,
"Dies if too many differences");
like(
dies { oddChar("xyzzy","xyzzyAA") }, qr/exactly one/,
"Dies if more than one of diff");
done_testing;
}