Task1
We count the longest sequence of equal characters:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);
sub power_string{
my ($res,$temp) = (1,1);
my @chars = split '',$_[0];
foreach my $i(1..$#chars){
if($chars[$i] eq $chars[$i-1]) {$res = max($res,++$temp)}
else {$temp = 1}
}
$res
}
printf "%d\n",power_string("textbook");
printf "%d\n",power_string("aaaaa");
printf "%d\n",power_string("hoorayyy");
printf "%d\n",power_string("x");
printf "%d\n",power_string("aabcccddeeffffghijjk")
Task2
We simulate the rotations and check if both x and y are zero:
#!/usr/bin/env perl
use strict;
use warnings;
sub meeting_point{
my ($x,$y) = (0,0);
foreach my $c(split '',$_[0]){
if($c eq 'U') {++$y}
elsif($c eq 'R') {++$x}
elsif($c eq 'D') {--$y}
elsif($c eq 'L') {--$x}
}
$x == $y == 0
}
printf "%d\n",meeting_point("ULD");
printf "%d\n",meeting_point("ULDR");
printf "%d\n",meeting_point("UUURRRDDD");
printf "%d\n",meeting_point("UURRRDDLLL");
printf "%d\n",meeting_point("RRUULLDDRRUU")
No comments:
Post a Comment