Sha256: fc3d2f28f7ab575098659f7622523ff6600a5b26bb561d57b2c0fc351d0f7555

Contents?: true

Size: 1.66 KB

Versions: 323

Compression:

Stored size: 1.66 KB

Contents

package Example;

use strict;
use warnings;

sub new {
    my ($class, $ocr) = @_;
    my $self = bless {} => $class;

    $self->_create_ocr_rows($ocr);
    $self->_create_list_table;

    return $self;
}

sub convert {
    my $self = shift;
    my @numbers;

    foreach my $ocr (@{$self->{ocr_rows}}) {
        push @numbers => $self->_convert_row($ocr);
    }

    return join "," => @numbers;
}

sub _convert_row {
    my ($self, $ocr) = @_;
    my @ocrs = $self->_list_ocr($ocr);
    my $number;

    foreach my $i (0..$#ocrs) {
        my $is_garbled = 1;
        foreach my $j (0..$#{$self->{list}}) {
            if ($ocrs[$i] eq $self->{list}->[$j]) {
                $number .= $j;
                $is_garbled = 0;
                last;
            }
        }
        $number .= '?' if $is_garbled;
    }

    return $number;
}

sub _list_ocr {
    my ($self, $ocr_str) = @_;
    my @lines = split '\n', $ocr_str;
    my @ocrs;
    my $len = length $lines[0];
    my $index = 0;

    while ($index < $len) {
        $ocrs[$index/3] = join "\n" => (
                map { substr($lines[$_], $index, 3) } 0..3
            );

        $index += 3;
    }
    return @ocrs;
}

sub _create_list_table {
    my $self = shift;
    my $all;
    chomp($all = <<'ALL');
 _     _  _     _  _  _  _  _ 
| |  | _| _||_||_ |_   ||_||_|
|_|  ||_  _|  | _||_|  ||_| _|
                              
ALL
    $self->{list} = [ $self->_list_ocr($all) ];
}

sub _create_ocr_rows {
    my ($self, $ocr_str) = @_;
    my @ocrs = split '\n', $ocr_str;
    my @indexes = grep { $_ % 4 == 0 } 0..$#ocrs;

    foreach my $i (@indexes) {
        push @{$self->{ocr_rows}} => join "\n", @ocrs[$i..($i+3)];
    }
}

1;

Version data entries

323 entries across 323 versions & 1 rubygems

Version Path
trackler-2.2.1.105 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.104 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.103 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.102 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.101 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.100 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.99 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.98 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.97 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.96 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.95 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.94 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.93 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.92 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.91 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.90 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.89 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.88 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.87 tracks/perl5/exercises/ocr-numbers/Example.pm
trackler-2.2.1.86 tracks/perl5/exercises/ocr-numbers/Example.pm