aboutsummaryrefslogtreecommitdiff
path: root/challenge-072
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-09 07:53:11 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-09 07:53:11 +0100
commit5fa26e361804a0c9e0d98fcc2151f0a74bce848d (patch)
treed375a100d06473d97dfa71c265e8f7561442c49f /challenge-072
parent7f3dde6f89d1d0df575807e0ec56a07a191cd7bb (diff)
downloadperlweeklychallenge-club-5fa26e361804a0c9e0d98fcc2151f0a74bce848d.tar.gz
perlweeklychallenge-club-5fa26e361804a0c9e0d98fcc2151f0a74bce848d.tar.bz2
perlweeklychallenge-club-5fa26e361804a0c9e0d98fcc2151f0a74bce848d.zip
- Added solutions by Cristina Heredia.
Diffstat (limited to 'challenge-072')
-rw-r--r--challenge-072/cristian-heredia/perl/ch-1.pl69
-rw-r--r--challenge-072/cristian-heredia/perl/ch-2.pl81
2 files changed, 150 insertions, 0 deletions
diff --git a/challenge-072/cristian-heredia/perl/ch-1.pl b/challenge-072/cristian-heredia/perl/ch-1.pl
new file mode 100644
index 0000000000..59a3a39d1f
--- /dev/null
+++ b/challenge-072/cristian-heredia/perl/ch-1.pl
@@ -0,0 +1,69 @@
+use strict;
+use warnings;
+
+#You are given a positive integer $N (<= 10).
+#
+#Write a script to print number of trailing zeroes in $N!.
+#
+#Example 1
+#Input: $N = 10
+#Output: 2 as $N! = 3628800 has 2 trailing zeroes
+#
+#Example 2
+#Input: $N = 7
+#Output: 1 as $N! = 5040 has 1 trailing zero
+#
+#Example 3
+#Input: $N = 4
+#Output: 0 as $N! = 24 has 0 trailing zero
+
+
+#variables
+my $N;
+my $result = 1;
+my $count = 0;
+my $sentence;
+
+
+requestNumber();
+
+sub requestNumber {
+
+ print "Please, introduce a number <= 10\n";
+ $N = <>;
+ $N =~ s/\s//g;
+ if ($N =~ /^\d+$/ and $N <= 10) {
+ obtainFactorial();
+ }
+ else {
+ print "That's not a valid number\n";
+ requestNumber();
+ }
+
+}
+
+sub obtainFactorial {
+
+ foreach (my $i = 2; $i <= $N; $i++){
+
+ $result = $result * $i;
+ }
+ $sentence = "$N! = $result";
+ trailingZeros();
+}
+
+
+sub trailingZeros {
+
+ if ($result =~ /0$/) {
+
+ chop($result);
+ $count++;
+ trailingZeros();
+ }
+ else {
+ #Output
+ print "$count as ".$sentence." has $count trailing zero\n";
+ }
+
+}
diff --git a/challenge-072/cristian-heredia/perl/ch-2.pl b/challenge-072/cristian-heredia/perl/ch-2.pl
new file mode 100644
index 0000000000..060d496e82
--- /dev/null
+++ b/challenge-072/cristian-heredia/perl/ch-2.pl
@@ -0,0 +1,81 @@
+use strict;
+use warnings;
+
+#You are given a text file name $file and range $A - $B where $A <= $B.
+#
+#Write a script to display lines range $A and $B in the given file.
+#
+#Example
+#Input:
+# $ cat input.txt
+# L1
+# L2
+# L3
+# L4
+# ...
+# ...
+# ...
+# ...
+# L100
+#$A = 4 and $B = 12
+#Output:
+# L4
+# L5
+# L6
+# L7
+# L8
+# L9
+# L10
+# L11
+# L12
+
+
+#Must accept three arguments
+if ($#ARGV != 2){
+ die "Error of arguments: ch-2.pl <file_name> <A> <B>";
+}
+
+my $file_name = $ARGV[0];
+my $A = $ARGV[1];
+my $B = $ARGV[2];
+my $count;
+
+#In case is needed to create the file and filing it
+#createFile();
+sub createFile {
+ foreach (my $i=1; $i<=100; $i++){
+ open (F, ">>$file_name") || die "Could not open file: $!\n";
+ print F "L$i\n";
+ close F;
+ }
+}
+
+#First we check that the file exists
+if ( !-f $file_name) {
+ die "the file $file_name doesn't exist\n";
+}
+
+#We check $A <= $B
+if ($A >= $B) {
+ die "$A isn't smaller that $B\n";
+}
+
+printLines();
+
+sub printLines {
+
+ open(FILE, $file_name) or die "Could not read from $file_name\n";
+
+ $count = 1;
+ while (<FILE>) {
+ if ($count > $B) {
+ close FILE;
+ exit;
+ }
+ if ($count >= $A) {
+ print $_;
+ }
+ $count++;
+ }
+}
+