From a Reflection on The Weekly Challenge 092 Task 1

Update: Ben Bullock has provided script for my question.
See the comment section. Thanks Ben.

---------------------

Happy New Year!


Today's data is about

$ perl pwc092-1_isomorphic.pl '茫茫人海' '夜夜笙歌'
0

--

Seeing others' post, I have a short reflection on dated The Weekly Challenge #092 Task 1 (statements / recap ), but may lead to a hike towards a hill in Perl.

Task statement:

TASK #1 › Isomorphic Strings Submitted by: Mohammad S Anwar

You are given two strings $A and $B.

Write a script to check if the given strings are Isomorphic. Print 1 if they are otherwise 0.
Example 1:

Input: $A = "abc"; $B = "xyz"
Output: 1

Example 2:

Input: $A = "abb"; $B = "xyy"
Output: 1

Example 3:

Input: $A = "sum"; $B = "add"
Output: 0


My unsatisfactory code (distaste due to the two subroutines &verify_pattern and &learn_pattern are almost the same).

Suddenly today I want to try out whether the Unicode support is direct; sadly, no:

For a single character:

$ perl pwc092-1_isomorphic.pl '日' '月'
1
$ perl pwc092-1_isomorphic.pl '日' '奮'
1
$ perl pwc092-1_isomorphic.pl '日' '海'
1
$ perl pwc092-1_isomorphic.pl '日' '曰'
1
$ perl pwc092-1_isomorphic.pl '日' '年'
1

For doublets, it seems obedient too
$ perl pwc092-1_isomorphic.pl '月月' '夜夜'
1
$ perl pwc092-1_isomorphic.pl '天天' '日日'
1
$ perl pwc092-1_isomorphic.pl '晚晚' '日日'
1

BUT there are some "sensitive" characters, oh:

$ perl pwc092-1_isomorphic.pl '晚晚' '人人'
Use of uninitialized value $char[2] in join or string at pwc092-1_isomorphic.pl line 52.
Use of uninitialized value $char[5] in join or string at pwc092-1_isomorphic.pl line 52.
0

$ perl pwc092-1_isomorphic.pl '日日' '人人'
Use of uninitialized value $char[2] in join or string at pwc092-1_isomorphic.pl line 52.
Use of uninitialized value $char[5] in join or string at pwc092-1_isomorphic.pl line 52.
0

For doublet of doublets - out of control
$ perl pwc092-1_isomorphic.pl '日日月月' '月月日日'
1
$ perl pwc092-1_isomorphic.pl '日日月月' '月月週週'
0
$ perl pwc092-1_isomorphic.pl '日日月月' '月月周周'
0
$ perl pwc092-1_isomorphic.pl '日日月月' '月月天天'
0
$ perl pwc092-1_isomorphic.pl '日日月月' '月月夜夜'
0
$ perl pwc092-1_isomorphic.pl '日日月月' '日日夜夜'
0

visit some usual two-character terms:

$ perl pwc092-1_isomorphic.pl '天意' '如此'
1
$ perl pwc092-1_isomorphic.pl '天意' '海地'
1


$ perl pwc092-1_isomorphic.pl '天意' '弄人'
Use of uninitialized value $char[5] in join or string at pwc092-1_isomorphic.pl line 52.
0
$ perl pwc092-1_isomorphic.pl '天意' '人海'
Use of uninitialized value $char[5] in join or string at pwc092-1_isomorphic.pl line 52.
0


Tests for other non-ASCII characters:

$ perl pwc092-1_isomorphic.pl 'ß' 'β'
1
$ perl pwc092-1_isomorphic.pl 'ß' 'é'
1
$ perl pwc092-1_isomorphic.pl 'ß' 'a'
Use of uninitialized value $char[1] in join or string at pwc092-1_isomorphic.pl line 52.
1
$ perl pwc092-1_isomorphic.pl 'ß' 'b'
Use of uninitialized value $char[1] in join or string at pwc092-1_isomorphic.pl line 52.
1

$ perl pwc092-1_isomorphic.pl 'Béla' 'Eric'
Use of uninitialized value $char[4] in join or string at pwc092-1_isomorphic.pl line 52.
1
$ perl pwc092-1_isomorphic.pl 'Eric' 'Béla'
Use of uninitialized value $char[4] in join or string at pwc092-1_isomorphic.pl line 52.
0



I lose patience, but still curious. Would someone write a script that we can have the followings?

$ perl isomorphic.pl '茫茫人海' '夜夜笙歌'
1


My current platform is LinuxMint.

Push a new item into "to-learn" list.

4 Comments


https://gist.github.com/benkasminbullock/79836f8a979f5f2e84d2aa1a8f7b3fea

[ben@mikan] {17:30 05} ~ 514 $ perl ch-1.pl '茫茫人海' '夜夜笙歌'
茫茫人海 夜夜笙歌
1

The secret is to bang the rocks together:

use Unicode::UTF8 'decode_utf8';
binmode STDOUT, ":encoding(utf8)";
my $l = decode_utf8 ($ARGV[0]);
my $r = decode_utf8 ($ARGV[1]);
print "$l $r\n";
my %h = learn_pattern($l);
if (verify_pattern(\%h,$r)) {
print 1;
} else {
print 0;
}

Some people on reddit also answered your query:

https://www.reddit.com/r/perl/comments/koahhf/from_a_reflection_on_the_weekly_challenge_092/

The replies might sound a bit sarcastic but that is because it's actually quite a well-known problem.

Leave a comment

About C.-Y. Fung

user-pic This blog is inactive and replaced by https://e7-87-83.github.io/coding/blog.html ; but I post highly Perl-related posts here.