diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-12 23:30:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-12 23:30:56 +0100 |
| commit | 37e6174ce5338a76a760c4a10e93b6daa503f1ec (patch) | |
| tree | 40bbcd39587839024c495a1e4c790ab2ffff63fd | |
| parent | 00db36ded945c571e93e885c48b9736138eaf138 (diff) | |
| parent | 4c198f0dd7b3af743ee4a68374c9e0bcb73b984a (diff) | |
| download | perlweeklychallenge-club-37e6174ce5338a76a760c4a10e93b6daa503f1ec.tar.gz perlweeklychallenge-club-37e6174ce5338a76a760c4a10e93b6daa503f1ec.tar.bz2 perlweeklychallenge-club-37e6174ce5338a76a760c4a10e93b6daa503f1ec.zip | |
Merge pull request #10602 from mgo1977/branch-for-challenge-282
mgo1977 - PWC 282
| -rw-r--r-- | challenge-282/mgo1977/perl/ch-1.pl | 80 | ||||
| -rw-r--r-- | challenge-282/mgo1977/perl/ch-2.pl | 73 |
2 files changed, 153 insertions, 0 deletions
diff --git a/challenge-282/mgo1977/perl/ch-1.pl b/challenge-282/mgo1977/perl/ch-1.pl new file mode 100644 index 0000000000..2cb15f6c6a --- /dev/null +++ b/challenge-282/mgo1977/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/bin/perl -w + + +use Data::Dump qw(dump); + + +# Task 1: Good Integer +# Submitted by: Mohammad Sajid Anwar +# 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" + + +testMe(\&process, 'Example1', 12344456, "444"); +testMe(\&process, 'Example2', 1233334, -1); +testMe(\&process, 'Example3', 10020003, "000"); + +sub testMe { + my $processor = shift; + my $name = shift; + my $input1 = shift; + my $expectedValue = shift; + + my $got = &$processor($input1); + + if ( $got eq $expectedValue ) { + print "[OK ] $name\n"; + } + else { + print "[ERR] $name :: got='$got', expectedValue='$expectedValue'\n"; + } + +} + + +sub process { + my $input1 = shift; + + my @chars = split(//, $input1); + + my $currentChar; + my $count = 0; + + foreach my $char ( @chars ) { + + if ( (not defined $currentChar) || ($currentChar ne $char) ) { + + if ( $count==3 ) { + return $currentChar x $count; + } + + $currentChar = $char; + $count = 1; + } + else { + ++$count; + } + + + + } + + if ( $count==3 ) { + return $currentChar x $count; + } + + return -1; +} diff --git a/challenge-282/mgo1977/perl/ch-2.pl b/challenge-282/mgo1977/perl/ch-2.pl new file mode 100644 index 0000000000..003b7d3cda --- /dev/null +++ b/challenge-282/mgo1977/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/bin/perl -w + + +# Task 2: Changing Keys +# Submitted by: Mohammad Sajid Anwar +# 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 + + +testMe(\&process, 'Example1', "pPeERrLl", 3); +testMe(\&process, 'Example2', "rRr", 0); +testMe(\&process, 'Example3', "GoO", 1); + +sub testMe { + my $processor = shift; + my $name = shift; + my $input1 = shift; + my $expectedValue = shift; + + my $got = &$processor($input1); + + if ( $got == $expectedValue ) { + print "[OK ] $name\n"; + } + else { + print "[ERR] $name :: got=$got, expectedValue=$expectedValue\n"; + } + +} + +sub process { + my $input1 = shift; + + my @chars = split(//, $input1); + + my $len = @chars; + + my $changes = 0; + + for(my $i=0; $i<($len-1); ++$i) { + + my $thisChar = lc($chars[$i]); + my $nextChar = lc($chars[$i+1]); + + if ( $thisChar ne $nextChar ) { + ++$changes; + } + + } + + return $changes; + +} + |
