diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-27 08:33:17 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-27 08:33:17 +0000 |
| commit | 1cbc3ca8a2f0e93581bb0229dff46c612242d308 (patch) | |
| tree | 74781dd16b6383ac86c8cb91f6da0d6eb3524405 /challenge-092 | |
| parent | 442056867fa2d0a2b188852f89ff9866a6377554 (diff) | |
| parent | 9e2a8b51d2bbd3f51edb60610f09d35c12c4b161 (diff) | |
| download | perlweeklychallenge-club-1cbc3ca8a2f0e93581bb0229dff46c612242d308.tar.gz perlweeklychallenge-club-1cbc3ca8a2f0e93581bb0229dff46c612242d308.tar.bz2 perlweeklychallenge-club-1cbc3ca8a2f0e93581bb0229dff46c612242d308.zip | |
Merge pull request #3070 from LubosKolouch/master
092 Task 1
Diffstat (limited to 'challenge-092')
| -rw-r--r-- | challenge-092/lubos-kolouch/perl/ch_1.pl | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-092/lubos-kolouch/perl/ch_1.pl b/challenge-092/lubos-kolouch/perl/ch_1.pl new file mode 100644 index 0000000000..829c48a29a --- /dev/null +++ b/challenge-092/lubos-kolouch/perl/ch_1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch_1.pl +# +# USAGE: ./ch_1.pl +# +# DESCRIPTION: Perl Weekly Challenge #092 +# Task 1 Isomorphic Strings +# +# AUTHOR: Lubos Kolouch +# VERSION: 1.0 +# CREATED: 12/25/2020 09:01:26 PM +#=============================================================================== + +use strict; +use warnings; +use Data::Dumper; + +sub is_isomorphic { + my $what = shift; + + my %char_map; + + my @arr1 = split //, $what->{'first'}; + my @arr2 = split //, $what->{'second'}; + + for my $i (0..scalar @arr1-1) { + $char_map{$arr2[$i]} //= $arr1[$i]; + $char_map{$arr1[$i]} //= $arr2[$i]; + + return 0 unless $char_map{$arr1[$i]} eq $arr2[$i]; + return 0 unless $char_map{$arr2[$i]} eq $arr1[$i]; + } + + return 1; +} + +use Test::More; + +is(is_isomorphic({first => 'abc', second => 'xyz'}), 1); +is(is_isomorphic({first => 'abb', second => 'xyy'}), 1); +is(is_isomorphic({first => 'sum', second => 'add'}), 0); + +done_testing; |
