aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-22 19:39:23 +0000
committerGitHub <noreply@github.com>2020-03-22 19:39:23 +0000
commit8d401c5ff657d7d2013cd87b06e94682c828fe80 (patch)
treebb347a0cfcf396a5b524c75b5f66959eaf88ef4c
parentdd5f1801000469ba8b3364f1b71f08e749467e8f (diff)
parentf657ed353824fd76455f6ffc2ec7a01f0120fc1d (diff)
downloadperlweeklychallenge-club-8d401c5ff657d7d2013cd87b06e94682c828fe80.tar.gz
perlweeklychallenge-club-8d401c5ff657d7d2013cd87b06e94682c828fe80.tar.bz2
perlweeklychallenge-club-8d401c5ff657d7d2013cd87b06e94682c828fe80.zip
Merge pull request #1442 from Cris-HD/branch-for-challenge-052
Added challenges
-rw-r--r--challenge-052/cristian-heredia/perl/ch-1.pl87
-rw-r--r--challenge-052/cristian-heredia/perl/ch-2.pl148
2 files changed, 235 insertions, 0 deletions
diff --git a/challenge-052/cristian-heredia/perl/ch-1.pl b/challenge-052/cristian-heredia/perl/ch-1.pl
new file mode 100644
index 0000000000..937d1febf3
--- /dev/null
+++ b/challenge-052/cristian-heredia/perl/ch-1.pl
@@ -0,0 +1,87 @@
+use strict;
+use warnings;
+
+#Stepping Numbers
+#Write a script to accept two numbers between 100 and 999. It should then print all Stepping Numbers between them.
+#
+#A number is called a stepping number if the adjacent digits have a #difference of 1. For example, 456 is a stepping number but 129 is not.
+
+#Variables
+my $lowRange = 100;
+my $highRange = 999;
+my $firstNumber;
+my $secondNumber;
+my $result = '';
+my $secondValor;
+my $thirdValor;
+my @aNumber;
+
+obtainFirstNumber();
+
+sub obtainFirstNumber {
+
+ print "Please, write a number between $lowRange and $highRange\n";
+ $firstNumber = <>;
+ $firstNumber=~ s/^\s+|\s+$//g;
+ if ($firstNumber >= $lowRange and $firstNumber <= $highRange and $firstNumber != $highRange) {
+ obtainSecondNumber();
+ }
+ else {
+ obtainFirstNumber();
+ }
+}
+
+sub obtainSecondNumber {
+
+ print "Please, write a number between $firstNumber and $highRange\n";
+ $secondNumber = <>;
+ $secondNumber=~ s/^\s+|\s+$//g;
+ if ($secondNumber > $firstNumber and $secondNumber <= $highRange) {
+ obtainSteppingNumbers();
+ }
+ else {
+ obtainSecondNumber();
+ }
+}
+
+sub obtainSteppingNumbers {
+
+ for (my $i=$firstNumber+'1'; $i < $secondNumber; $i++) {
+
+ @aNumber = split(//,$i);
+ $secondValor = $aNumber[0] + 1;
+ $thirdValor = $aNumber[0] + 2;
+
+ if ($secondValor == 10) {
+ $secondValor = '0';
+ }
+
+ if ($thirdValor == 10) {
+ $thirdValor = '0';
+ }
+ elsif ($thirdValor == 11) {
+ $thirdValor = '1';
+ }
+
+ if ($secondValor != $thirdValor) {
+ if ($aNumber[1] eq $secondValor and $aNumber[2] eq $thirdValor) {
+ $result = $result.$i."\n";
+ }
+ }
+ }
+ message();
+}
+
+sub message {
+ if ($result ne '') {
+ print "The stepping number for $firstNumber and $secondNumber:\n$result";
+ }
+ else {
+ print "There are not stepping numbers for $firstNumber and $secondNumber\n";
+ }
+}
+
+
+
+
+
diff --git a/challenge-052/cristian-heredia/perl/ch-2.pl b/challenge-052/cristian-heredia/perl/ch-2.pl
new file mode 100644
index 0000000000..b5460175d0
--- /dev/null
+++ b/challenge-052/cristian-heredia/perl/ch-2.pl
@@ -0,0 +1,148 @@
+use strict;
+use warnings;
+
+#Lucky Winner
+#Suppose there are following coins arranged on a table in a line in random order.
+#
+#£1, 50p, 1p, 10p, 5p, 20p, £2, 2p
+#
+#Suppose you are playing against the computer. Player can only pick one coin at a time from either ends. Find out the lucky winner, who has the larger amounts in total?
+
+#Variables
+my @values = ('£1 ', '50p ', '1p ', '10p ', '5p ', '20p ', '£2 ', '2p ');
+my @coins;
+my $choise;
+my $playerTotal = 0;
+my $pcTotal = 0;
+my $value;
+my $first;
+my $last;
+
+my $length = @values;
+
+mixArray();
+
+
+sub mixArray {
+
+ for ( 1 .. $length ) {
+ push @coins, splice @values, rand @values, 1;
+ }
+ playerTime();
+}
+
+sub playerTime {
+
+ if ($length != 0) {
+ print "The coins are arranged in the following:\n@coins\n";
+ print "Which end do you want to choose, left (l) or right (r)?\n";
+
+ $choise = <>;
+ $choise =~ s/^\s+|\s+$//g;
+
+ if ($choise eq 'left' or $choise eq 'l') {
+ $value = shift @coins;
+ $value =~ s/^\s+|\s+$//g;
+ removeLetter($value);
+ $playerTotal = $playerTotal + $value;
+ $length--;
+ pcTime();
+ }
+ elsif ($choise eq 'right' or $choise eq 'r') {
+ $value = pop @coins;
+ $value =~ s/^\s+|\s+$//g;
+ removeLetter($value);
+ $playerTotal = $playerTotal + $value;
+ $length--;
+ pcTime();
+ }
+ else {
+ playerTime();
+ }
+ }
+ else {
+ winner();
+ }
+}
+
+sub pcTime {
+
+ if ($length != 0) {
+
+ compareResults();
+ if ($choise eq 'l') {
+ $value = shift @coins;
+ $value =~ s/^\s+|\s+$//g;
+ removeLetter($value);
+ $pcTotal = $pcTotal + $value;
+ $length--;
+ playerTime();
+
+ }
+ elsif ($choise eq 'r') {
+ $value = pop @coins;
+ $value =~ s/^\s+|\s+$//g;
+ removeLetter($value);
+ $pcTotal = $pcTotal + $value;
+ $length--;
+ playerTime();
+ }
+ else {
+ pcTime();
+ }
+ }
+ else {
+ winner();
+ }
+}
+
+
+sub removeLetter {
+
+ $value = shift();
+
+ $value =~ s/£//g;
+ if ($value =~ /p/) {
+ $value =~ s/p//g;
+ if ($value =~ /\d\d/) {
+ $value = "0.".$value;
+ }
+ else {
+ $value = "0.0".$value;
+ }
+ }
+ return $value;
+}
+
+sub compareResults {
+
+ $first = $coins[0];
+ $first =~ s/^\s+|\s+$//g;
+ $first = removeLetter($first);
+
+ $last = $coins[$length-1];
+ $last =~ s/^\s+|\s+$//g;
+ $last = removeLetter($last);
+
+ if ($first > $last) {
+ $choise = 'l';
+ }
+ else {
+ $choise = 'r';
+ }
+ return $choise;
+}
+
+
+sub winner {
+ if ($playerTotal > $pcTotal) {
+ print "The winner is the user with a total of: $playerTotal\n";
+ }
+ elsif ($playerTotal < $pcTotal) {
+ print "The winner is the pc with a total of: $pcTotal\n";
+ }
+ else {
+ print "There is an draw between the user and the pc, with a result of: $playerTotal\n";
+ }
+}
+