Last active
August 29, 2015 14:10
-
-
Save JacAbreu/71bb12d296b78b88d2b6 to your computer and use it in GitHub Desktop.
Problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Linux && Mac) | |
wget -O - http://install.perlbrew.pl | bash | |
source ~/perl5/perlbrew/etc/bashrc | |
perlbrew available | |
perlbrew install perl-5.20.1 | |
perlbrew list | |
perlbrew switch perl-5.20.1 | |
Windows -> http://strawberryperl.com/ ou http://www.activestate.com/activeperl | |
perl fizzbuzz.pl | |
#!/usr/bin/env perl | |
use 5.20.1; | |
use warnings; | |
sub main { | |
my $number; | |
say "Write fizzbuzz limit "; | |
chomp($number = <STDIN>); | |
fizzbuzz($number); | |
fizzbuzz_exclusive($number); | |
} | |
sub fizzbuzz_exclusive { | |
my $limit = shift; | |
say "\n####fizzbuzz_exclusive####"; | |
foreach my $n (1..$limit) { | |
($n % 3 == 0) ? say "fizz" : (($n % 5 == 0) ? say 'buzz' : say $n); | |
} | |
} | |
sub fizzbuzz { | |
say "\n####fizzbuzz####"; | |
my $limit = shift; | |
foreach my $n (1..$limit) { | |
if ( $n % 15 == 0 ) { say "fizzbuzz"; } | |
elsif ( $n % 3 == 0) { say "fizz";} | |
elsif ( $n % 5 == 0) { say "buzz"; } | |
else { say $n }; | |
} | |
} | |
main(); | |
exit(); | |
echo -n "aabbbccccbaa" | perl rle_perl.pl | |
#!/usr/bin/env perl | |
use 5.20.1; | |
use warnings; | |
sub main { | |
my $word; | |
say "Write something "; | |
$word = <STDIN>; | |
rle( $word ); | |
} | |
sub rle { | |
my $word = shift; | |
if ( defined $word ) { | |
my @array = split(//, $word) ; | |
my $qtd = 0; | |
my $prev_character = ''; | |
for my $character ( @array ) { | |
if ( $prev_character eq $character ) { | |
$qtd = $qtd + 1; | |
} | |
else { | |
if ( $qtd > 1 ) { | |
print $prev_character . $qtd; | |
} | |
elsif ( $qtd == 1 ) { | |
print $prev_character; | |
} | |
$prev_character = $character; | |
$qtd = 1; | |
} | |
} | |
if ( $qtd > 1 ) { | |
say $prev_character . $qtd; | |
} | |
elsif ( $qtd == 1 ) { | |
say $prev_character; | |
} | |
} | |
} | |
main(); | |
exit(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment