diff options
| author | DevSanti12 <santiagoleyva2013@gmail.com> | 2024-08-17 15:20:16 -0500 |
|---|---|---|
| committer | DevSanti12 <santiagoleyva2013@gmail.com> | 2024-08-17 15:20:16 -0500 |
| commit | e013979f2b4aa3a6d417f42ad62896ffa3124167 (patch) | |
| tree | 535dd3195142e3d5d1a7b0aa543bbbd7d3a5c28e | |
| parent | 90b65e67814b30a06427740b70766744b2c8c792 (diff) | |
| download | perlweeklychallenge-club-e013979f2b4aa3a6d417f42ad62896ffa3124167.tar.gz perlweeklychallenge-club-e013979f2b4aa3a6d417f42ad62896ffa3124167.tar.bz2 perlweeklychallenge-club-e013979f2b4aa3a6d417f42ad62896ffa3124167.zip | |
Added ch01 and ch02 for #282
| -rw-r--r-- | challenge-282/santiago-leyva/perl/ch-01.pl | 50 | ||||
| -rw-r--r-- | challenge-282/santiago-leyva/perl/ch-02.pl | 50 |
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-282/santiago-leyva/perl/ch-01.pl b/challenge-282/santiago-leyva/perl/ch-01.pl new file mode 100644 index 0000000000..f06e7b0ad7 --- /dev/null +++ b/challenge-282/santiago-leyva/perl/ch-01.pl @@ -0,0 +1,50 @@ +=begin + +You are given a positive integer, $int, having 3 or more digits. + +Write a script to return the Good Integer in the given integer or -1 if none found. + +A good integer is exactly three consecutive matching digits. + +Example 1 +Input: $int = 12344456 +Output: "444" +Example 2 +Input: $int = 1233334 +Output: -1 +Example 3 +Input: $int = 10020003 +Output: "000" + +=cut + +my @nums = (12344456,1233334,10020003); + +foreach(@nums){ + my $good = Findgoodinteger($_); + print $good."\n"; +} + +sub Findgoodinteger { + my $number = shift; + my @num_list = split("",$number); + #print join("",@num_list)."\n"; + my $result .= @num_list[0]; + for(my $i=0; $i<(scalar @num_list);$i++){ + + if(length($result) == 3 and ($num_list[$i+1] != $num_list[$i])){ + return $result; + } + #print "$num_list[$i] == $num_list[$i+1] $result \n"; + if($num_list[$i] == $num_list[$i+1]){ + $result .= $num_list[$i]; + }else{ + $result = ''; + $result .= $num_list[$i+1]; + } + + + } + return -1; + +}
\ No newline at end of file diff --git a/challenge-282/santiago-leyva/perl/ch-02.pl b/challenge-282/santiago-leyva/perl/ch-02.pl new file mode 100644 index 0000000000..2e1c4909c2 --- /dev/null +++ b/challenge-282/santiago-leyva/perl/ch-02.pl @@ -0,0 +1,50 @@ +=begin + +You are given an alphabetic string, $str, as typed by user. + +Write a script to find the number of times user had to change the key to type the given string. +Changing key is defined as using a key different from the last used key. The shift and caps lock keys won’t be counted. + +Example 1 +Input: $str = 'pPeERrLl' +Ouput: 3 + +p -> P : 0 key change +P -> e : 1 key change +e -> E : 0 key change +E -> R : 1 key change +R -> r : 0 key change +r -> L : 1 key change +L -> l : 0 key change +Example 2 +Input: $str = 'rRr' +Ouput: 0 +Example 3 +Input: $str = 'GoO' +Ouput: 1 + +=cut + +my @strings = ('pPeERrLl','rRr','GoO','hHannah'); + +foreach(@strings){ + my $key_change = findKeyChange($_)."\n"; + print $key_change; +} + +sub findKeyChange { + my $_string = shift; + my @lower_string = split("",$_string); + @lower_string = map{ + lc $_; + }@lower_string; + my $change = -1; + + for(my $i=0;$i<(scalar @lower_string);$i++){ + if($lower_string[$i] ne $lower_string[$i+1]){ + $change++; + } + } + + return $change; +}
\ No newline at end of file |
