diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-07 11:11:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-07 11:11:54 +0100 |
| commit | bc692449158e3ee850d1e6919b16a24a3b610729 (patch) | |
| tree | d75393ee49d6cd189fab3247a82dfe92caeb4708 | |
| parent | 5dfd69977e1ff5490e7c15d7b7608b82bd1eb9ba (diff) | |
| parent | b60e11c9f512612892c9b76a7f5ac9035cfad033 (diff) | |
| download | perlweeklychallenge-club-bc692449158e3ee850d1e6919b16a24a3b610729.tar.gz perlweeklychallenge-club-bc692449158e3ee850d1e6919b16a24a3b610729.tar.bz2 perlweeklychallenge-club-bc692449158e3ee850d1e6919b16a24a3b610729.zip | |
Merge pull request #226 from yzhernand/ch-011-yozen
challenge-011 yozen-hernandez solutions
| -rw-r--r-- | challenge-011/yozen-hernandez/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-011/yozen-hernandez/perl5/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-011/yozen-hernandez/perl5/ch-2.pl | 37 |
3 files changed, 70 insertions, 0 deletions
diff --git a/challenge-011/yozen-hernandez/blog.txt b/challenge-011/yozen-hernandez/blog.txt new file mode 100644 index 0000000000..76d8945149 --- /dev/null +++ b/challenge-011/yozen-hernandez/blog.txt @@ -0,0 +1 @@ +https://yzhernand.github.io/posts/perl-weekly-challenge-11/ diff --git a/challenge-011/yozen-hernandez/perl5/ch-1.pl b/challenge-011/yozen-hernandez/perl5/ch-1.pl new file mode 100755 index 0000000000..6d60935d7a --- /dev/null +++ b/challenge-011/yozen-hernandez/perl5/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use Carp; + +# Write a script that computes the equal point in the Fahrenheit and Celsius +# scales, knowing that the freezing point of water is 32 °F and 0 °C, and that +# the boiling point of water is 212 °F and 100 °C. This challenge was proposed +# by Laurent Rosenfeld. + + +# Solve a linear equation (form of y = mx + b) +# where x = y. Needs slope and y-intercept of +# equation (m and b above) +sub find_x_eq_y { + my ($slope, $y_intercept) = @_; + + # Solve for x = y + # $x = $x*$slope + $y_intercept + # -$y_intercept = $x*$slope - $x + # $x($slope-1) = -$y_intercept; + return (-$y_intercept)/($slope - 1); +} + +# This is the same as 9/5, but here so that +# we use the information given in the challenge +my $slope = (212-32)/(100-0); + +# y-intercept is 32 in °F = °C(9/5) + 32 +say "Intersect of Fahrenheit and Celsius scale is: ", find_x_eq_y($slope, 32); diff --git a/challenge-011/yozen-hernandez/perl5/ch-2.pl b/challenge-011/yozen-hernandez/perl5/ch-2.pl new file mode 100755 index 0000000000..2f5db508a2 --- /dev/null +++ b/challenge-011/yozen-hernandez/perl5/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use Carp; + +# Write a script to create an Indentity Matrix for the given size. For example, +# if the size is 4, then create Identity Matrix 4x4. For more information about +# Indentity Matrix, please read the wiki page. + +# Identity matrices In exist for all nxn square matrices where n > 0 +# Simply take an n and generate a matrix with 1's along the diagonal +# ie, 1 @ position i for 0 ≤ i < n (in 0-indexed arrays) +sub ident_mat { + my $n = shift or croak "Must supply an value for size of square matrix"; + + # Force integer + $n = int $n; + my @i_mat = (); + + for my $i ( 0 .. $n - 1 ) { + my @a = (0) x $n; + $a[$i] = 1; + push @i_mat, \@a; + } + + return \@i_mat; +} + +my $n = shift or die "Usage: $0 <matrix size n>"; +my $i_mat = ident_mat($n); + +say "Identity matrix for an $n x $n square matrix"; +for my $i ( 0 .. $n - 1 ) { + say join( " ", @{ $i_mat->[$i] } ); +} |
