aboutsummaryrefslogtreecommitdiff
path: root/challenge-096/athanasius/perl/Matrix.pm
blob: eecf63391ca19cc5a9b465ba772e7c6c62c3fe48 (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
#!perl

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

Perl Weekly Challenge 096, Task #2: Edit Distance

Matrix class for use in implementing the Wagner–Fischer algorithm for finding
the Levenshtein distance between two strings.

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

#--------------------------------------#
# Copyright © 2021 PerlMonk Athanasius #
#--------------------------------------#

#==============================================================================
package Matrix;
#==============================================================================

use strict;
use warnings;

#------------------------------------------------------------------------------
sub new                                                           # Constructor
#------------------------------------------------------------------------------
{
    my ($class, $word1, $word2) = @_;

    my  $cols = length( $word1 ) + 1;
    my  $rows = length( $word2 ) + 1;
    my  @matrix;

    for my $row (0 .. $rows)
    {
        $matrix[ $row ][ $_ ] = undef for 0 .. $cols;
    }

    $word2 = "#$word2";
    my $i  = 0;

    for my $row (1 .. $rows)
    {
        $matrix[ $row ][ 0 ] = substr $word2, $i, 1;
        $matrix[ $row ][ 1 ] = $i++;
    }

    $word1 = "#$word1";
    $i     = 0;

    for my $col (1 .. $cols)
    {
        $matrix[ 0 ][ $col ] = substr $word1, $i, 1;
        $matrix[ 1 ][ $col ] = $i++;
    }

    my %self = (
                   height =>  $rows,
                   width  =>  $cols,
                   matrix => \@matrix,
               );

    return bless \%self, $class;
}

#------------------------------------------------------------------------------
sub height                                              # Accessor: getter only
#------------------------------------------------------------------------------
{
    my ($self) = @_;

    return $self->{height};
}

#------------------------------------------------------------------------------
sub width                                               # Accessor: getter only
#------------------------------------------------------------------------------
{
    my ($self) = @_;

    return $self->{width};
}

#------------------------------------------------------------------------------
sub element                                       # Accessor: getter and setter
#------------------------------------------------------------------------------
{
    my ($self, $row, $col, $value) = @_;

    if (defined $value)
    {
        $self->{matrix}[ $row + 1 ][ $col + 1 ] = $value;       # Set
    }

    return $self->{matrix}[ $row + 1 ][ $col + 1 ];             # Get
}

#------------------------------------------------------------------------------
sub display
#------------------------------------------------------------------------------
{
    my ($self)  = @_;
    my  $height = $self->{height};
    my  $width  = $self->{width};

    # Pre-compute the maximum widths of individual columns

    my @widths = ( 1 );

    for my $col (1 .. $width)
    {
        my $max = 0;

        for my $row (1 .. $height)
        {
            my $current = $self->{matrix}[ $row ][ $col ];
            my $cur_len = length $current;
               $max     = $cur_len if $cur_len > $max;
        }

        $widths[$col] = $max;
    }

    # Draw a vertical separator

    my $line  = '+';
       $line .=  sprintf '-%s-+', '-'  x $widths[ $_ ] for 0 .. $width;
       $line .= "\n";

    # Draw the matrix

    my $display = $line;

    for my $row (0 .. $height)
    {
        $display .= '|';
        $display .=  sprintf ' %*s |', $widths[ $_ ],
                                       $self->{matrix}[ $row ][ $_ ] // ' '
                        for 0 .. $width;
        $display .= "\n" . $line;
    }

    return $display;
}

###############################################################################
1;
###############################################################################