moose
BAN USER
Coupla quick solutions:
n squared:
#!perl -l
use strict;
use warnings;
my @output;
my $y = 0;
my @lines = <DATA>;
for my $line (@lines) {
chomp $line;
for my $x (0 .. length ($line) - 1) {
$output[$y][$x] ||= '-';
if (substr($line, $x, 1) eq 'x') {
for my $i (0 .. length ($line) - 1) {
$output[$y][$i] = 'x';
}
for my $i (0 .. $#lines) {
$output[$i][$x] = 'x';
}
}
}
$y++;
}
print @$_ for @output;
__DATA__
-----
-----
---x-
x----
-----
Linear:
use strict;
use warnings;
my $y = 0;
my @lines = <DATA>;
my (%x, %y);
for my $line (@lines) {
chomp $line;
for my $x (0 .. length ($line)) {
if (substr($line, $x, 1) eq 'x') {
$x{$x}++;
$y{$y}++;
}
}
$y++;
}
for my $i (0 .. @lines - 1) {
for my $j (0 .. length($lines[0]) - 1) {
print $y{$i} || $x{$j} ? 'x' : '-';
}
print "\n";
}
__DATA__
-----
-----
---x-
x----
-----
Not sure what the hashmap has to do with this, but here's my stab:
- moose October 01, 2015