aboutsummaryrefslogtreecommitdiff
path: root/challenge-331/athanasius/perl/ch-2.pl
blob: 599bc6865383c4fea34ab86cd4f2f239b912b040 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!perl

################################################################################
=comment

Perl Weekly Challenge 331
=========================

TASK #2
-------
*Buddy Strings*

Submitted by: Mohammad Sajid Anwar

You are given two strings, source and target.

Write a script to find out if the given strings are Buddy Strings.

  If swapping of a letter in one string make them same as the other then they
  are `Buddy Strings`.

Example 1

  Input: $source = "fuck"
         $target = "fcuk"
  Output: true

  The swapping of 'u' with 'c' makes it buddy strings.

Example 2

  Input: $source = "love"
         $target = "love"
  Output: false

Example 3

  Input: $source = "fodo"
         $target = "food"
  Output: true

Example 4

  Input: $source = "feed"
         $target = "feed"
  Output: true

=cut
################################################################################

#--------------------------------------#
# Copyright © 2025 PerlMonk Athanasius #
#--------------------------------------#

#===============================================================================
=comment

Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. Two strings are entered on the command-line.

=cut
#===============================================================================

use v5.32;        # Enables strictures
use warnings;
use Const::Fast;
use Devel::Assert qw( on );
use Test::More;

const my $USAGE => <<END;
Usage:
  perl $0 <source> <target>
  perl $0

    <source>    First string
    <target>    Second string
END

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 331, Task #2: Buddy Strings (Perl)\n\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
    my $argc = scalar @ARGV;

    if    ($argc == 0)
    {
        run_tests();
    }
    elsif ($argc == 2)
    {
        my ($source, $target) =  @ARGV;

        print qq[Input:  \$source = "$source"\n];
        print qq[        \$target = "$target"\n];

        my $buddies = are_buddies( $source, $target );

        printf "Output: %s\n", $buddies ? 'true' : 'false';
    }
    else
    {
        error( "Expected 0 or 2 command-line arguments, found $argc" );
    }
}

#-------------------------------------------------------------------------------
sub are_buddies
#-------------------------------------------------------------------------------
{
    my ($source, $target) = @_;
    my  $result = '';

    if (same_chars( $source, $target ))
    {
        my $diffs = count_diffs( $source, $target );

        $result = $diffs == 0 ? has_duplicates( $source ) :
                  $diffs == 2;
    }

    return $result;
}

#-------------------------------------------------------------------------------
sub same_chars
#-------------------------------------------------------------------------------
{
    my ($source, $target) = @_;
    my  $result = '';

    if (length $source == length $target)
    {
        my $source_norm = join '', sort split '', $source;
        my $target_norm = join '', sort split '', $target;

        $result = $source_norm eq $target_norm;
    }

    return $result;
}

#-------------------------------------------------------------------------------
sub count_diffs
#-------------------------------------------------------------------------------
{
    my ($source, $target) = @_;

    assert length $source == length $target;

    my @source_chars = split '', $source;
    my @target_chars = split '', $target;
    my $differences  = 0;

    for my $i (0 .. $#source_chars)
    {
        ++$differences if $source_chars[ $i ] ne $target_chars[ $i ];
    }

    return $differences;
}

#-------------------------------------------------------------------------------
sub has_duplicates
#-------------------------------------------------------------------------------
{
    my ($string) = @_;
    my  $result  = '';
    my  %char_count;
      ++$char_count{ $_ } for split '', $string;

    for my $count (values %char_count)
    {
        if ($count > 1)
        {
            $result = 1;
            last;
        }
    }

    return $result;
}

#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
    print "Running the test suite\n";

    while (my $line = <DATA>)
    {
        chomp $line;

        my  ($test_name, $source, $target, $expected) = split / \| /x, $line;

        for ($test_name, $source, $target, $expected)
        {
            s/ ^ \s+   //x;
            s/   \s+ $ //x;
        }

        my $buddies = are_buddies( $source, $target ) ? 'true' : 'false';

        is $buddies, $expected, $test_name;
    }

    done_testing;
}

#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
    my ($message) = @_;

    die "ERROR: $message\n$USAGE";
}

################################################################################

__DATA__
Example 1   |fuck|fcuk |true
Example 2   |love|love |false
Example 3   |fodo|food |true
Example 4   |feed|feed |true
Diff lengths|Perl|Pearl|false