rsFalse has asked for the wisdom of the Perl Monks concerning the following question:
Test case #2:C1 B3 E0 81 B3 F0 80 81 B3 F8 80 80 81 B3 FC 80 80 80 81 B3 80 C1 B3 E0 81 B3 F0 80 81 B3 F8 80 80 81 B3 FC 80 80 80 81 B3 E0 AF B5
Output for TC #1:FF 00 FF D1 81 F0 80 91 81 00 D1 81 FF F0 80 91 81 FF FF 00 D1 81 F0 80 91 81
Output for TC #2:73 73 73 73 73 73 73 73 73 73 BF5
My code which solves two tests correctly, but it is slow for bigger tests (can't see them), and maybe it is incorrect(?):441 441 0 441 0 441 441
#!/usr/bin/perl use warnings; use strict; $\ = $/; my $debug = 0; $_ = do { local $/; <> }; @_ = split; $_ = join '', map { sprintf "%08b", eval "0x$_" } @_, 'FF'; my @data = reverse <DATA>; chomp @data; y/x/./ for @data; my @rxs = join '|', @data; my @r; my @R; while( /\G(?:@rxs|.{8})/g ){ my $c = $&; $debug and print "c:$c"; $c =~ /@rxs/ or do { push @R, [ @r ] if @r > 2; @r = (); $debug and print " -F"; next; }; my $x = $c =~ s/.{8}/ ($&) =~ s!^1+0!!r /ger; $debug and print "x:",$x; push @r, ~~ reverse $x; } my @acc; my @ACC; for my $R (@R){ @acc = (); for my $r ( @{ $R } ){ my $acc = ''; while( $r =~ /.{1,4}/g ){ $acc .= ( 0 .. 9, 'A' .. 'F' )[ eval "0b" . reverse $& ]; } $acc = reverse $acc; $acc =~ s/^0+\B//; push @acc, $acc; } push @ACC, "@acc"; } print for @ACC; __DATA__ 0xxxxxxx 110xxxxx10xxxxxx 1110xxxx10xxxxxx10xxxxxx 11110xxx10xxxxxx10xxxxxx10xxxxxx 111110xx10xxxxxx10xxxxxx10xxxxxx10xxxxxx 1111110x10xxxxxx10xxxxxx10xxxxxx10xxxxxx10xxxxxx
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: contest problem about UTF-8
by ikegami (Patriarch) on Oct 07, 2017 at 01:15 UTC | |
by Anonymous Monk on Oct 07, 2017 at 23:22 UTC | |
by ikegami (Patriarch) on Oct 08, 2017 at 02:09 UTC | |
|
Re: contest problem about UTF-8
by LanX (Saint) on Oct 07, 2017 at 00:55 UTC | |
by rsFalse (Chaplain) on Oct 07, 2017 at 11:51 UTC |