From d42d0edb7569c79d031dcc4a7f95f1a5919e6ffd Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 4 Jun 2019 16:25:28 +0100 Subject: - Added solution by Steven Wilson. --- challenge-011/steven-wilson/perl5/ch-3.pl | 71 +++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 challenge-011/steven-wilson/perl5/ch-3.pl (limited to 'challenge-011') diff --git a/challenge-011/steven-wilson/perl5/ch-3.pl b/challenge-011/steven-wilson/perl5/ch-3.pl new file mode 100644 index 0000000000..69668f68b2 --- /dev/null +++ b/challenge-011/steven-wilson/perl5/ch-3.pl @@ -0,0 +1,71 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-06-03 +# Week: 011 +# Challenge: #3 +# +# Using Open Weather Map API, write a script to fetch the current weather for +# an arbitrary city. Note that you will need to sign up for Open Weather Map’s +# free tier and then wait a couple hours before your API key will be valid. +# This challenge was proposed by Joelle Maslak. The API challenge is optional +# but would love to see your solution. +# https://openweathermap.org/current +# usage: $ ./ch-3.pl London +# or: $ ./ch-3.pl "London, US" + +use strict; +use warnings; +use feature qw / say /; +use Switch; +use LWP::Simple; +use JSON::MaybeXS; + +my $API_KEY = "5f1b876baa129252e557f43d029e974b"; +my $city = $ARGV[0]; + +my $api_call_url + = "http://api.openweathermap.org/data/2.5/weather?" + . "q=$city" + . "&APIKEY=$API_KEY"; +my $filename = "weather." . lc($city) . ".json"; +my $content; +my $data_structure; + +if ( -s $filename and -M $filename < 0.0069 ) { + open my $fh, '<', $filename or die "Cannot read '$filename': $!\n"; + $content = <$fh>; + $data_structure = decode_json($content); + close $fh; +} +else { + $content = get($api_call_url); + die "Couldn't get it!" unless defined $content; + $data_structure = decode_json($content); + open my $fh, '>', $filename or die "Cannot write '$filename': $!\n"; + print $fh $content; + close $fh; +} + +my %weather = %{$data_structure}; +my $condition; +my $item; + +switch ( $weather{"weather"}[0]{id} ) { + case [ 200 .. 232 ] { $condition = "thundery"; $item = "golf club" } + case [ 300 .. 321 ] { $condition = "drizzly"; $item = "lemon cake" } + case [ 500 .. 531 ] { $condition = "rainy"; $item = "wellies" } + case [ 600 .. 622 ] { $condition = "snowy"; $item = "sledge" } + case [ 701 .. 781 ] { + $condition = "low visability"; + $item = "flashlight" + } + case [800] { $condition = "clear skies"; $item = "sunscreen" } + case [ 801 .. 804 ] { + $condition = "cloudy"; + $item = "imagination, what shapes can you see?" + } +} + +say "Another $condition day in old " + . $weather{"name"} + . " town, don't forget your $item."; \ No newline at end of file -- cgit From 571dfc2efec88b8d21e60a9b93e2ba5f122aacee Mon Sep 17 00:00:00 2001 From: aliciabielsa Date: Tue, 4 Jun 2019 19:01:38 +0200 Subject: Challenge 1 Week 11 --- challenge-011/alicia-bielsa/perl5/ch-1.pl | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 challenge-011/alicia-bielsa/perl5/ch-1.pl (limited to 'challenge-011') diff --git a/challenge-011/alicia-bielsa/perl5/ch-1.pl b/challenge-011/alicia-bielsa/perl5/ch-1.pl new file mode 100644 index 0000000000..7c90ea500e --- /dev/null +++ b/challenge-011/alicia-bielsa/perl5/ch-1.pl @@ -0,0 +1,65 @@ +use strict; +use warnings; +use POSIX; + + + + +#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 + + +my $pointCelsius = 0; +my $equalPoint = 0; +my $keepSearching = 1; + +while ($keepSearching){ + + my $pointFahrenheid= celsiusToFarenheit($pointCelsius) ; + + my $difference = $pointCelsius - $pointFahrenheid; + + if ($difference > 0 ){ + + $pointCelsius -= int( $difference/2) ? int( $difference/2) : ceil( $difference/2) ; + + } elsif ($difference < 0 ){ + + $pointCelsius += int( $difference/2) ? int( $difference/2) : floor($difference/2) ; + + } else { + + $equalPoint = $pointCelsius; + + $keepSearching = 0; + } +} + + +print "Equal Point is $equalPoint\n"; + + + +sub fahrenheitToCelsius { + + my $degreesFarenheit = shift; + + my $degreesCelsius = ($degreesFarenheit - 32) * 5 / 9; + + return $degreesCelsius; + +} + + + +sub celsiusToFarenheit { + + my $degreesCelsius = shift; + + my $degreesFarenheit = $degreesCelsius * 9 / 5 + 32; + + return $degreesFarenheit; + +} \ No newline at end of file -- cgit From 15f869ee193a88b7cd3a49bdaa92d39cb77e4dd1 Mon Sep 17 00:00:00 2001 From: aliciabielsa Date: Tue, 4 Jun 2019 19:44:06 +0200 Subject: challenge 2 week 11 --- challenge-011/alicia-bielsa/perl5/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-011/alicia-bielsa/perl5/ch-2.pl (limited to 'challenge-011') diff --git a/challenge-011/alicia-bielsa/perl5/ch-2.pl b/challenge-011/alicia-bielsa/perl5/ch-2.pl new file mode 100644 index 0000000000..d645f4bffb --- /dev/null +++ b/challenge-011/alicia-bielsa/perl5/ch-2.pl @@ -0,0 +1,30 @@ +#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. + +use strict; +use warnings; + +my $size = 11; + +my @aIdentityMatrix = (); + +for my $a (0..$size-1){ + my @aRow = (); + for my $b (0..$size-1){ + if ($a == $b){ + push (@aRow, 1); + } else { + push (@aRow, 0); + } + } + push ( @aIdentityMatrix , \@aRow); +} + + +foreach my $row (@aIdentityMatrix){ + foreach my $column ( @{$row} ) { + print $column ."\t"; + } + print "\n"; +} \ No newline at end of file -- cgit From ac86a448e03d3324e3b3666608fb632c05839303 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 4 Jun 2019 19:17:24 +0100 Subject: - Added blog by Dave Jacoby. --- challenge-011/dave-jacoby/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-011/dave-jacoby/blog.txt (limited to 'challenge-011') diff --git a/challenge-011/dave-jacoby/blog.txt b/challenge-011/dave-jacoby/blog.txt new file mode 100644 index 0000000000..1e514656fc --- /dev/null +++ b/challenge-011/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io//2019/06/04/two-technical-posts-about-temperature-kinda.html -- cgit From 5ceaaf8645e5d2c0653f56df1aeacfeea016b6d4 Mon Sep 17 00:00:00 2001 From: andrezgz Date: Tue, 4 Jun 2019 20:48:41 -0300 Subject: challenge-011 andrezgz solution --- challenge-011/andrezgz/perl5/ch-1.pl | 15 +++++++++++++++ challenge-011/andrezgz/perl5/ch-2.pl | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 challenge-011/andrezgz/perl5/ch-1.pl create mode 100644 challenge-011/andrezgz/perl5/ch-2.pl (limited to 'challenge-011') diff --git a/challenge-011/andrezgz/perl5/ch-1.pl b/challenge-011/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..2671169b2b --- /dev/null +++ b/challenge-011/andrezgz/perl5/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-011/ +# Challenge #1 +# 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. + +use strict; +use warnings; + +my $f = 212; +while ( $f - ($f - 32) * (100-0)/(212-32) ) { $f-- }; #Simple, but effective +print $f; diff --git a/challenge-011/andrezgz/perl5/ch-2.pl b/challenge-011/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..276dbc4453 --- /dev/null +++ b/challenge-011/andrezgz/perl5/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-011/ +# Challenge #2 +# 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. +# https://en.wikipedia.org/wiki/Identity_matrix + + +use strict; +use warnings; + +die "Usage: $0 " unless $ARGV[0] && $ARGV[0] =~ /\d+/; + +my $n = int($ARGV[0]) - 1; + +for (0..$n) { print " 0" x $_ . " 1" . " 0" x ($n - $_) . $/; } -- cgit From 46035186eb4356ae75cc22f81edb3731316d3263 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 5 Jun 2019 11:53:48 +0100 Subject: - Added solutions by Laurent Rosenfeld. --- challenge-011/laurent-rosenfeld/blog.txt | 1 + challenge-011/laurent-rosenfeld/perl5/ch-1.pl | 17 +++++++++++++++++ challenge-011/laurent-rosenfeld/perl5/ch-2.pl | 17 +++++++++++++++++ challenge-011/laurent-rosenfeld/perl5/ch-2a.pl | 19 +++++++++++++++++++ challenge-011/laurent-rosenfeld/perl5/ch-2b.pl | 17 +++++++++++++++++ challenge-011/laurent-rosenfeld/perl5/ch-2c.pl | 14 ++++++++++++++ challenge-011/laurent-rosenfeld/perl6/ch-1.p6 | 22 ++++++++++++++++++++++ challenge-011/laurent-rosenfeld/perl6/ch-2.p6 | 17 +++++++++++++++++ challenge-011/laurent-rosenfeld/perl6/ch-2a.p6 | 10 ++++++++++ challenge-011/laurent-rosenfeld/perl6/ch-2b.p6 | 9 +++++++++ 10 files changed, 143 insertions(+) create mode 100644 challenge-011/laurent-rosenfeld/blog.txt create mode 100644 challenge-011/laurent-rosenfeld/perl5/ch-1.pl create mode 100644 challenge-011/laurent-rosenfeld/perl5/ch-2.pl create mode 100644 challenge-011/laurent-rosenfeld/perl5/ch-2a.pl create mode 100644 challenge-011/laurent-rosenfeld/perl5/ch-2b.pl create mode 100644 challenge-011/laurent-rosenfeld/perl5/ch-2c.pl create mode 100644 challenge-011/laurent-rosenfeld/perl6/ch-1.p6 create mode 100644 challenge-011/laurent-rosenfeld/perl6/ch-2.p6 create mode 100644 challenge-011/laurent-rosenfeld/perl6/ch-2a.p6 create mode 100644 challenge-011/laurent-rosenfeld/perl6/ch-2b.p6 (limited to 'challenge-011') diff --git a/challenge-011/laurent-rosenfeld/blog.txt b/challenge-011/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..4e429fef44 --- /dev/null +++ b/challenge-011/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2019/06/perl-weekly-challenge-11-fahrenheit-celsius-and-identity-matrix.html diff --git a/challenge-011/laurent-rosenfeld/perl5/ch-1.pl b/challenge-011/laurent-rosenfeld/perl5/ch-1.pl new file mode 100644 index 0000000000..028c8dc8b2 --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl5/ch-1.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; +use feature qw/say/; + +my %freeze = (F => 32, C => 0); +my %boil = (F => 212, C => 100); +my $speed_diff = ($boil{F} - $freeze{F} ) /($boil{C} - $freeze{C} ) ; + +my $c = $freeze{C}; +my $f = $freeze{F}; + +while (abs($c - $f) > 0.2) { + $f += ($c - $f)/2; + $c += ( ($c - $f) / $speed_diff ); + # say "Approx: $c $f ", $c-$f; # De-comment this line to see the intermediate results +} +printf "Fahrenheit and Celsius scales meet at: %.0f\n", $c; diff --git a/challenge-011/laurent-rosenfeld/perl5/ch-2.pl b/challenge-011/laurent-rosenfeld/perl5/ch-2.pl new file mode 100644 index 0000000000..77153629b2 --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl5/ch-2.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; +use feature qw/say/; +use Data::Dumper; + +my $size = shift() - 1; +my @matrix; +for my $i (0..$size) { + for my $j (0..$size-1) { + if ($i == $j) { + $matrix[$i][$j] = 1; + } else { + $matrix[$i][$j] = 0; + } + } +} +say Dumper \@matrix; diff --git a/challenge-011/laurent-rosenfeld/perl5/ch-2a.pl b/challenge-011/laurent-rosenfeld/perl5/ch-2a.pl new file mode 100644 index 0000000000..1521d81f4f --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl5/ch-2a.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use feature qw/say/; + +sub pretty_print { + my $matrix_ref = shift; + for my $rows (@$matrix_ref) { + say join " ", @$rows; + } +} + +my $size = shift() - 1; +my @matrix; +for my $i (0..$size) { + for my $j (0..$size) { + $matrix[$i][$j] = $i == $j ? 1 : 0; + } +} +pretty_print \@matrix; diff --git a/challenge-011/laurent-rosenfeld/perl5/ch-2b.pl b/challenge-011/laurent-rosenfeld/perl5/ch-2b.pl new file mode 100644 index 0000000000..d653e75918 --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl5/ch-2b.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; +use feature qw/say/; + +sub pretty_print { + my $matrix_ref = shift; + for my $rows (@$matrix_ref) { + say join " ", @$rows; + } +} + +my $size = shift() - 1; +my @matrix; +for my $i (0..$size) { + $matrix[$i][$_] = $i == $_ ? 1 : 0 for 0..$size; +} +pretty_print \@matrix; diff --git a/challenge-011/laurent-rosenfeld/perl5/ch-2c.pl b/challenge-011/laurent-rosenfeld/perl5/ch-2c.pl new file mode 100644 index 0000000000..f9f630628f --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl5/ch-2c.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature qw/say/; + +sub prettify { + my $matrix = shift; + return join "\n", map { join " ", @$_ } @$matrix; +} + +my $size = shift() - 1; +my @matrix = map { my $i = $_; [map { $i == $_ ? 1 : 0 } 0..$size] } + 0..$size; +say prettify \@matrix; diff --git a/challenge-011/laurent-rosenfeld/perl6/ch-1.p6 b/challenge-011/laurent-rosenfeld/perl6/ch-1.p6 new file mode 100644 index 0000000000..feedfdba33 --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl6/ch-1.p6 @@ -0,0 +1,22 @@ +use v6; + +#`{ 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. + } + + +my %freeze = (F => 32, C => 0); +my %boil = (F => 212, C => 100); +my $speed_diff = (%boil - %freeze ) / (%boil - %freeze ); + +my $c = %freeze; +my $f = %freeze; + +while abs($c - $f) > 0.2 { + $f += ($c - $f)/2; + $c += ( ($c - $f) / $speed_diff ); + # say "Approx: $c $f ", $c-$f; +} +say "Fahrenheit and Celsius scales meet at: ", $c.fmt("%.0f"); diff --git a/challenge-011/laurent-rosenfeld/perl6/ch-2.p6 b/challenge-011/laurent-rosenfeld/perl6/ch-2.p6 new file mode 100644 index 0000000000..cba7df15de --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl6/ch-2.p6 @@ -0,0 +1,17 @@ +use v6; + +sub pretty-print (@matrix) { + for @matrix -> @rows { + say join " ", @rows; + } +} +sub MAIN (Int $size where * > 0) { + my @matrix; + $size--; + for 0..$size -> $i { + for 0..$size -> $j { + @matrix[$i][$j] = $i == $j ?? 1 !! 0; + } + } + pretty-print @matrix; +} diff --git a/challenge-011/laurent-rosenfeld/perl6/ch-2a.p6 b/challenge-011/laurent-rosenfeld/perl6/ch-2a.p6 new file mode 100644 index 0000000000..7b1e6fa938 --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl6/ch-2a.p6 @@ -0,0 +1,10 @@ +use v6; + +sub prettify (@matrix) { + return join "\n", map { join " ", $_}, @matrix; +} +sub MAIN (Int $size where * > 0) { + my @matrix = map { my $i = $_; map { $i == $_ ?? 1 !! 0 }, + 0..$size }, 0..$size; + say prettify @matrix; +} diff --git a/challenge-011/laurent-rosenfeld/perl6/ch-2b.p6 b/challenge-011/laurent-rosenfeld/perl6/ch-2b.p6 new file mode 100644 index 0000000000..f1f8645333 --- /dev/null +++ b/challenge-011/laurent-rosenfeld/perl6/ch-2b.p6 @@ -0,0 +1,9 @@ +use v6; + +sub prettify (@matrix) { + @matrix.map({join(" ",$_)}).join("\n"); +} +sub MAIN (Int $size where * > 0) { + say prettify (1..$size).map( -> $i + { (1..$size).map( { $_ == $i ?? 1 !! 0 })}); +} -- cgit From c48d69fcc6f1593381f85161331559d948709401 Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Wed, 5 Jun 2019 21:21:43 -0300 Subject: Gustavo Chaves solutions to challenge 011 --- challenge-011/gustavo-chaves/perl5/README.pod | 44 ++++++++++++++++++ challenge-011/gustavo-chaves/perl5/ch-1.pl | 64 +++++++++++++++++++++++++++ challenge-011/gustavo-chaves/perl5/ch-2a.pl | 13 ++++++ challenge-011/gustavo-chaves/perl5/ch-2b.pl | 12 +++++ 4 files changed, 133 insertions(+) create mode 100644 challenge-011/gustavo-chaves/perl5/README.pod create mode 100755 challenge-011/gustavo-chaves/perl5/ch-1.pl create mode 100755 challenge-011/gustavo-chaves/perl5/ch-2a.pl create mode 100755 challenge-011/gustavo-chaves/perl5/ch-2b.pl (limited to 'challenge-011') diff --git a/challenge-011/gustavo-chaves/perl5/README.pod b/challenge-011/gustavo-chaves/perl5/README.pod new file mode 100644 index 0000000000..127fadf691 --- /dev/null +++ b/challenge-011/gustavo-chaves/perl5/README.pod @@ -0,0 +1,44 @@ +=pod + +=encoding utf8 + +=head1 #1 Temperatures + +=over 4 + +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 +B. + +=back + +It's not clear what the script is supposed to do. After all, with a little +algebra we can calculate the solution by hand. For a while I thought about +writing the following script: + + #!/usr/bin/env perl + print "40\n"; + +But, since the purpose of the challenge is to allow us to learn something I +searched CPAN for a Math module that could help me solve a system of linear +equations. I ended up using the L module which seems cool. + +=head1 #2 Identity Matrix + +=over 4 + +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 +L page. + +=back + +This one was easier. I wrote two scripts. The first one simply builds a +two-dimensional array representing the matrix and prints it with the +L module. The only real interest in it is how the array is +constructed using the 'x' and the '..' operators to make it terse. + +The second solution is even simpler using the L module's +C constructor. Go figure! diff --git a/challenge-011/gustavo-chaves/perl5/ch-1.pl b/challenge-011/gustavo-chaves/perl5/ch-1.pl new file mode 100755 index 0000000000..0f99e21f87 --- /dev/null +++ b/challenge-011/gustavo-chaves/perl5/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +use 5.026; +use strict; +use autodie; +use warnings; + +=encoding utf8 + +=head1 + +I know that: + + 32℉ = 0℃ + 212℉ = 100℃ + +I also know the formula to convert ℉ to ℃ is linear. So: + + ℃*x + y = ℉ + +So, I can express the two equalities above as a system of linear equations: + + 0*x + y = 32 + 100*x + y = 212 + +I could do it by hand, but let's use some Perl L. + +Searching CPAN I found a bunch of Math modules that could help me solve the +linear system above for x and y. I use the L module below to +solve a system in vector form: + + Matrix * vector = solution + +=cut + +use Math::MatrixReal; + +my $Matrix = Math::MatrixReal->new_from_cols([[0, 100], [1, 1]]); + +my $vector = Math::MatrixReal->new_from_cols([[32, 212]]); + +my $LR = $Matrix->decompose_LR(); + +my ($dimension, $solution, $base_matrix) = $LR->solve_LR($vector); + +my ($x, $y) = $solution->as_list(); + +=pod + +Now that we have found x and y we can make ℉ = ℃ = T to find the equal numeric +temperature: + + ℃*x + y = ℉ + T*x + y = T + y = T*(1 - x) + T = y / (1 - x) + +=cut + +my $T = $y / (1 - $x); + +print "$T℃ = $T℉\n"; diff --git a/challenge-011/gustavo-chaves/perl5/ch-2a.pl b/challenge-011/gustavo-chaves/perl5/ch-2a.pl new file mode 100755 index 0000000000..1d46116cc3 --- /dev/null +++ b/challenge-011/gustavo-chaves/perl5/ch-2a.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Data::Dump; + +my $n = shift or die "Usage: $0 N\n"; + +my @matrix = map {[(0) x $n]} (1 .. $n); + +$matrix[$_][$_] = 1 foreach (0 .. $n-1); + +ddx \@matrix; diff --git a/challenge-011/gustavo-chaves/perl5/ch-2b.pl b/challenge-011/gustavo-chaves/perl5/ch-2b.pl new file mode 100755 index 0000000000..cfbf301937 --- /dev/null +++ b/challenge-011/gustavo-chaves/perl5/ch-2b.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl + +use 5.026; +use strict; +use warnings; +use Math::MatrixReal; + +my $n = shift or die "Usage: $0 N\n"; + +my $matrix = Math::MatrixReal->new_diag([(1) x $n]); + +say $matrix; -- cgit From d56d883a9784671c88f9353c4cd3a7d287aeabbe Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 6 Jun 2019 11:21:13 +0100 Subject: - Added solutions by Gustavo Chaves. --- challenge-011/gustavo-chaves/perl5/ch-2.pl | 13 +++++++++++++ challenge-011/gustavo-chaves/perl5/ch-2a.pl | 9 ++++----- challenge-011/gustavo-chaves/perl5/ch-2b.pl | 12 ------------ 3 files changed, 17 insertions(+), 17 deletions(-) create mode 100755 challenge-011/gustavo-chaves/perl5/ch-2.pl delete mode 100755 challenge-011/gustavo-chaves/perl5/ch-2b.pl (limited to 'challenge-011') diff --git a/challenge-011/gustavo-chaves/perl5/ch-2.pl b/challenge-011/gustavo-chaves/perl5/ch-2.pl new file mode 100755 index 0000000000..1d46116cc3 --- /dev/null +++ b/challenge-011/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Data::Dump; + +my $n = shift or die "Usage: $0 N\n"; + +my @matrix = map {[(0) x $n]} (1 .. $n); + +$matrix[$_][$_] = 1 foreach (0 .. $n-1); + +ddx \@matrix; diff --git a/challenge-011/gustavo-chaves/perl5/ch-2a.pl b/challenge-011/gustavo-chaves/perl5/ch-2a.pl index 1d46116cc3..cfbf301937 100755 --- a/challenge-011/gustavo-chaves/perl5/ch-2a.pl +++ b/challenge-011/gustavo-chaves/perl5/ch-2a.pl @@ -1,13 +1,12 @@ #!/usr/bin/env perl +use 5.026; use strict; use warnings; -use Data::Dump; +use Math::MatrixReal; my $n = shift or die "Usage: $0 N\n"; -my @matrix = map {[(0) x $n]} (1 .. $n); +my $matrix = Math::MatrixReal->new_diag([(1) x $n]); -$matrix[$_][$_] = 1 foreach (0 .. $n-1); - -ddx \@matrix; +say $matrix; diff --git a/challenge-011/gustavo-chaves/perl5/ch-2b.pl b/challenge-011/gustavo-chaves/perl5/ch-2b.pl deleted file mode 100755 index cfbf301937..0000000000 --- a/challenge-011/gustavo-chaves/perl5/ch-2b.pl +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env perl - -use 5.026; -use strict; -use warnings; -use Math::MatrixReal; - -my $n = shift or die "Usage: $0 N\n"; - -my $matrix = Math::MatrixReal->new_diag([(1) x $n]); - -say $matrix; -- cgit From b8c0bbf829a93ab70347ec6bafc40ef93594c9f1 Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Thu, 6 Jun 2019 23:50:09 +1000 Subject: fjwhittle challenge 011 solution --- challenge-011/fjwhittle/perl6/ch-1.p6 | 12 ++++++++++++ challenge-011/fjwhittle/perl6/ch-2.p6 | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 challenge-011/fjwhittle/perl6/ch-1.p6 create mode 100644 challenge-011/fjwhittle/perl6/ch-2.p6 (limited to 'challenge-011') diff --git a/challenge-011/fjwhittle/perl6/ch-1.p6 b/challenge-011/fjwhittle/perl6/ch-1.p6 new file mode 100644 index 0000000000..e35332670d --- /dev/null +++ b/challenge-011/fjwhittle/perl6/ch-1.p6 @@ -0,0 +1,12 @@ +#!/usr/bin/env perl6 + +my $minf = 32; +my $maxf = 212; +my $minc = 0; +my $maxc = 100; + +# Degrees Fahrenheit can be expressed as a function of the form f(x) = ax + C where x +# is the temperature in degrees Celsius. +# When f(x) = x, x = C / (1 - a) + +say ($minf) / (1 - ($maxf - $minf) / ($maxc - $minc)); diff --git a/challenge-011/fjwhittle/perl6/ch-2.p6 b/challenge-011/fjwhittle/perl6/ch-2.p6 new file mode 100644 index 0000000000..6f99e37fb3 --- /dev/null +++ b/challenge-011/fjwhittle/perl6/ch-2.p6 @@ -0,0 +1,12 @@ +#!/usr/bin/env perl6 + +sub identity-matrix (UInt $n) { + (^$n).map: -> $i { (^$n).map: -> $j { Int($j == $i) } }; +} + +#| Script to create an identity matrix +unit sub MAIN ( + UInt $n #= Dimensions of the generated matrix. +); + +.say for identity-matrix $n; -- cgit From e3f2ceba05cce2c3f6550d83ce0eed41e7f89540 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 6 Jun 2019 14:57:18 +0100 Subject: - Added member Matthew Persico. --- challenge-011/matthew-persico/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-011/matthew-persico/README (limited to 'challenge-011') diff --git a/challenge-011/matthew-persico/README b/challenge-011/matthew-persico/README new file mode 100644 index 0000000000..5aa5622296 --- /dev/null +++ b/challenge-011/matthew-persico/README @@ -0,0 +1 @@ +Solutions by Matthew O. Persico. -- cgit From 5dfd69977e1ff5490e7c15d7b7608b82bd1eb9ba Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 6 Jun 2019 17:01:14 +0100 Subject: - Added member Aaron Sherman. --- challenge-011/aaron-sherman/README | 1 + challenge-011/aaron-sherman/perl6/ch-1.p6 | 76 +++++++++++++++++++++++++++++++ challenge-011/aaron-sherman/perl6/ch-2.p6 | 76 +++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 challenge-011/aaron-sherman/README create mode 100644 challenge-011/aaron-sherman/perl6/ch-1.p6 create mode 100644 challenge-011/aaron-sherman/perl6/ch-2.p6 (limited to 'challenge-011') diff --git a/challenge-011/aaron-sherman/README b/challenge-011/aaron-sherman/README new file mode 100644 index 0000000000..14ec31f570 --- /dev/null +++ b/challenge-011/aaron-sherman/README @@ -0,0 +1 @@ +Solutions by Aaron Sherman. diff --git a/challenge-011/aaron-sherman/perl6/ch-1.p6 b/challenge-011/aaron-sherman/perl6/ch-1.p6 new file mode 100644 index 0000000000..d61cb697bc --- /dev/null +++ b/challenge-011/aaron-sherman/perl6/ch-1.p6 @@ -0,0 +1,76 @@ +#!/usr/bin/env perl6 + +# From : https://perlweeklychallenge.org/blog/perl-weekly-challenge-011/ + +# Challenge #1 +# 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. + +# The challenge was a bit bland, so I jazzed it up. +# --help for more info + +use v6.c; + +# Note that this is the only place we keep the +# units, so everything else except comments has +# to derive even the abbreviations from this lookup. +our %knowns = ( + unit => , + freezing => [32, 0], + boiling => [212, 100] ); + +proto MAIN($input?) {*} + +multi MAIN( + #= Convert the given value e.g. 37C + Str $input where m/$/ +) { + my $value = +$input.substr(0,*-1); + my $from = $input.substr(*-1); + my $units = %knowns.join('/'); + die "$from is not one of $units" if !%knowns.grep: * eq $from; + my $to = %knowns.grep({$_ ne $input}).first; + say "$value$from in $to: {convert($value, :$to)}"; +} + +multi MAIN( + #= Show conversion for given unit e.g. C + Str $input where $input eq [|] |%knowns +) { + my $to = %knowns.grep({$_ ne $input}).first; + my $conversion = conversion(:$to); + my $scale = $conversion.nude.join('/'); + say "Conversion to $to: {$input}*{$scale} + {$conversion}"; +} + +#= by default show crossover for units +multi MAIN() { + my $units = %knowns.join('/'); + say "$units cross over at {crossover}"; +} + +sub other-unit($unit) { return %knowns.grep({$_ ne $unit}).first } + +sub convert($value, :$to where $to eq [|] |%knowns) { + my $conv = conversion(:$to); + return $value*$conv + $conv; +} + +sub crossover() { + my $conv = conversion(:to); + return $conv/(1-$conv); +} + +sub conversion(:$to where $to eq [|] |%knowns) { + my $to-index = %knowns.first: * eq $to, :k; + # Get the offset between the two + my $offset = [-] |%knowns; + my $scale = [/] (|%knowns <<->> |%knowns); + if !$to-index { + return { scale => $scale, offset => $offset, index => $to-index }; + } else { + return { scale => 1/$scale, offset => -$offset, index => $to-index }; + } +} diff --git a/challenge-011/aaron-sherman/perl6/ch-2.p6 b/challenge-011/aaron-sherman/perl6/ch-2.p6 new file mode 100644 index 0000000000..d61cb697bc --- /dev/null +++ b/challenge-011/aaron-sherman/perl6/ch-2.p6 @@ -0,0 +1,76 @@ +#!/usr/bin/env perl6 + +# From : https://perlweeklychallenge.org/blog/perl-weekly-challenge-011/ + +# Challenge #1 +# 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. + +# The challenge was a bit bland, so I jazzed it up. +# --help for more info + +use v6.c; + +# Note that this is the only place we keep the +# units, so everything else except comments has +# to derive even the abbreviations from this lookup. +our %knowns = ( + unit => , + freezing => [32, 0], + boiling => [212, 100] ); + +proto MAIN($input?) {*} + +multi MAIN( + #= Convert the given value e.g. 37C + Str $input where m/$/ +) { + my $value = +$input.substr(0,*-1); + my $from = $input.substr(*-1); + my $units = %knowns.join('/'); + die "$from is not one of $units" if !%knowns.grep: * eq $from; + my $to = %knowns.grep({$_ ne $input}).first; + say "$value$from in $to: {convert($value, :$to)}"; +} + +multi MAIN( + #= Show conversion for given unit e.g. C + Str $input where $input eq [|] |%knowns +) { + my $to = %knowns.grep({$_ ne $input}).first; + my $conversion = conversion(:$to); + my $scale = $conversion.nude.join('/'); + say "Conversion to $to: {$input}*{$scale} + {$conversion}"; +} + +#= by default show crossover for units +multi MAIN() { + my $units = %knowns.join('/'); + say "$units cross over at {crossover}"; +} + +sub other-unit($unit) { return %knowns.grep({$_ ne $unit}).first } + +sub convert($value, :$to where $to eq [|] |%knowns) { + my $conv = conversion(:$to); + return $value*$conv + $conv; +} + +sub crossover() { + my $conv = conversion(:to); + return $conv/(1-$conv); +} + +sub conversion(:$to where $to eq [|] |%knowns) { + my $to-index = %knowns.first: * eq $to, :k; + # Get the offset between the two + my $offset = [-] |%knowns; + my $scale = [/] (|%knowns <<->> |%knowns); + if !$to-index { + return { scale => $scale, offset => $offset, index => $to-index }; + } else { + return { scale => 1/$scale, offset => -$offset, index => $to-index }; + } +} -- cgit From b60e11c9f512612892c9b76a7f5ac9035cfad033 Mon Sep 17 00:00:00 2001 From: Yozen Hernandez Date: Thu, 6 Jun 2019 16:44:40 -0400 Subject: challenge-011 yozen-hernandez solution --- challenge-011/yozen-hernandez/blog.txt | 1 + challenge-011/yozen-hernandez/perl5/ch-1.pl | 32 +++++++++++++++++++++++++ challenge-011/yozen-hernandez/perl5/ch-2.pl | 37 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 challenge-011/yozen-hernandez/blog.txt create mode 100755 challenge-011/yozen-hernandez/perl5/ch-1.pl create mode 100755 challenge-011/yozen-hernandez/perl5/ch-2.pl (limited to 'challenge-011') 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 "; +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] } ); +} -- cgit From e3d46091fdb8bf0c49294bf8a6d6d94502ed1be7 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 7 Jun 2019 11:21:07 +0100 Subject: - Added solution by Maxim Nechaev. --- challenge-011/maxim-nechaev/perl5/ch-1.pl | 22 ++++++++++++++++++++++ challenge-011/maxim-nechaev/perl5/ch-2.pl | 13 +++++++++++++ 2 files changed, 35 insertions(+) create mode 100755 challenge-011/maxim-nechaev/perl5/ch-1.pl create mode 100755 challenge-011/maxim-nechaev/perl5/ch-2.pl (limited to 'challenge-011') diff --git a/challenge-011/maxim-nechaev/perl5/ch-1.pl b/challenge-011/maxim-nechaev/perl5/ch-1.pl new file mode 100755 index 0000000000..11735070d8 --- /dev/null +++ b/challenge-011/maxim-nechaev/perl5/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl -w +use strict; +use feature 'say'; + +# function to find root +sub f { + my $c = shift; # celsius + my $f = 9*$c/5 + 32; # fahrenheit + return $c - $f; +} + +# see https://en.wikipedia.org/wiki/Bisection_method +my $a = -1000; # begin interval +my $b = 1000; # end interval +my $s = 1E-13; # accuracy +my $x; # root to find +do { + $x = ($a + $b)/2; + f($a) * f($x) < 0? $b = $x : $a = $x; +} while abs f($x) > $s; + +say $x; # -40 diff --git a/challenge-011/maxim-nechaev/perl5/ch-2.pl b/challenge-011/maxim-nechaev/perl5/ch-2.pl new file mode 100755 index 0000000000..c3430a1095 --- /dev/null +++ b/challenge-011/maxim-nechaev/perl5/ch-2.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl -w +use strict; +use Math::Matrix; + +my $size = 5; +my $indentity = Math::Matrix->diagonal( (1) x $size ); + +print $indentity; +# 1.00000 0.00000 0.00000 0.00000 0.00000 +# 0.00000 1.00000 0.00000 0.00000 0.00000 +# 0.00000 0.00000 1.00000 0.00000 0.00000 +# 0.00000 0.00000 0.00000 1.00000 0.00000 +# 0.00000 0.00000 0.00000 0.00000 1.00000 -- cgit From ccbd2682b031809d75444023e49fb1f743120b78 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 7 Jun 2019 15:38:07 +0100 Subject: - Added solution by Jo Christian Oterhals. --- challenge-011/jo-christian-oterhals/perl6/ch-1.sh | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-011/jo-christian-oterhals/perl6/ch-1.sh (limited to 'challenge-011') diff --git a/challenge-011/jo-christian-oterhals/perl6/ch-1.sh b/challenge-011/jo-christian-oterhals/perl6/ch-1.sh new file mode 100644 index 0000000000..46451905ba --- /dev/null +++ b/challenge-011/jo-christian-oterhals/perl6/ch-1.sh @@ -0,0 +1 @@ +perl6 -e 'sub MAIN($a, $b) { say "Equal point: " ~ ( $b - $a == 100 ?? "None" !! $a / (1 - (($b - $a) / 100))) }' 32 212 -- cgit From 8d3b71a68c5f96b12cbc52c4ce86249d3a6cc40b Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 7 Jun 2019 16:16:55 +0100 Subject: - Added member Feng Chang. --- challenge-011/feng-chang/README | 1 + challenge-011/feng-chang/perl5/ch-2.pl | 16 ++++++++++++++++ challenge-011/feng-chang/perl6/ch-1.p6 | 3 +++ challenge-011/feng-chang/perl6/ch-2.p6 | 8 ++++++++ challenge-011/feng-chang/perl6/ch-3.p6 | 24 ++++++++++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 challenge-011/feng-chang/README create mode 100644 challenge-011/feng-chang/perl5/ch-2.pl create mode 100644 challenge-011/feng-chang/perl6/ch-1.p6 create mode 100644 challenge-011/feng-chang/perl6/ch-2.p6 create mode 100644 challenge-011/feng-chang/perl6/ch-3.p6 (limited to 'challenge-011') diff --git a/challenge-011/feng-chang/README b/challenge-011/feng-chang/README new file mode 100644 index 0000000000..74e56de3ed --- /dev/null +++ b/challenge-011/feng-chang/README @@ -0,0 +1 @@ +Solutions by Feng Chang. diff --git a/challenge-011/feng-chang/perl5/ch-2.pl b/challenge-011/feng-chang/perl5/ch-2.pl new file mode 100644 index 0000000000..197278775c --- /dev/null +++ b/challenge-011/feng-chang/perl5/ch-2.pl @@ -0,0 +1,16 @@ +#!/bin/env perl + +use Modern::Perl; +use Data::Dumper; + +my $n = shift; +my @a; + +for my $i (0 .. $n - 1) { + my @b = (0) x $n; + $b[$i] = 1; + + push(@a, \@b); +} + +print Dumper(\@a); diff --git a/challenge-011/feng-chang/perl6/ch-1.p6 b/challenge-011/feng-chang/perl6/ch-1.p6 new file mode 100644 index 0000000000..fc16353b45 --- /dev/null +++ b/challenge-011/feng-chang/perl6/ch-1.p6 @@ -0,0 +1,3 @@ +#!/bin/env perl6 + +(-32 * 5 / (9 - 5)).say; diff --git a/challenge-011/feng-chang/perl6/ch-2.p6 b/challenge-011/feng-chang/perl6/ch-2.p6 new file mode 100644 index 0000000000..59ca4dc214 --- /dev/null +++ b/challenge-011/feng-chang/perl6/ch-2.p6 @@ -0,0 +1,8 @@ +#!/bin/env perl6 + +sub MAIN(Int $n where * > 0) { + my @a = [0 xx $n] xx $n; + @a[$_][$_] = 1 for 0 .. $n - 1; + + @a.say; +} diff --git a/challenge-011/feng-chang/perl6/ch-3.p6 b/challenge-011/feng-chang/perl6/ch-3.p6 new file mode 100644 index 0000000000..3b3660edf1 --- /dev/null +++ b/challenge-011/feng-chang/perl6/ch-3.p6 @@ -0,0 +1,24 @@ +#!/bin/env perl6 + +use HTTP::UserAgent; +use JSON::Tiny; +use Data::Dump; + +my Str $base_url = 'http://api.openweathermap.org/data/2.5/weather'; +my Str $app_id = 'appid=82642c9af842d8d57f7df99b291e0e75'; + +sub MAIN( + Str :$city = 'london', + Str :$country = '', +) { + my $ua = HTTP::UserAgent.new; + + my $url = "$base_url?q={ $city }{ (',' ~ $country) if $country }&$app_id"; + + my $resp = $ua.get($url); + if $resp.is-success { + say Dump(from-json($resp.content)); + } else { + say 'fetch error'; + } +} -- cgit From 82d3bc4156c6bc9a94bb5d6bca89b7b747371cc6 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 7 Jun 2019 23:36:48 +0100 Subject: - Added blog by Jo Christian Oterhals. --- challenge-011/jo-christian-oterhals/blog.txt | 1 + challenge-011/jo-christian-oterhals/perl6/ch-1.p6 | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 challenge-011/jo-christian-oterhals/blog.txt create mode 100644 challenge-011/jo-christian-oterhals/perl6/ch-1.p6 (limited to 'challenge-011') diff --git a/challenge-011/jo-christian-oterhals/blog.txt b/challenge-011/jo-christian-oterhals/blog.txt new file mode 100644 index 0000000000..8abe3a4a23 --- /dev/null +++ b/challenge-011/jo-christian-oterhals/blog.txt @@ -0,0 +1 @@ +https://medium.com/@jcoterhals/perl-6-small-stuff-20-from-anonymous-one-line-functions-to-full-length-mains-with-type-and-error-3d3a69faabda diff --git a/challenge-011/jo-christian-oterhals/perl6/ch-1.p6 b/challenge-011/jo-christian-oterhals/perl6/ch-1.p6 new file mode 100644 index 0000000000..54955d03c7 --- /dev/null +++ b/challenge-011/jo-christian-oterhals/perl6/ch-1.p6 @@ -0,0 +1,21 @@ +#!/usr/bin/env perl6 + +multi MAIN( #= Compares the C. scale to any scale you dream up + Real $f, #= freezing point in custom scale + Real $b #= boiling point in custom scale +) { + say "There is no equal point for this scale." and exit if $b - $f == 100; + my $equal-point = $f / (1 - (($b - $f) / 100)); + say "The calculated equal point is only theoretical as it is below absolute zero." if $equal-point < -273.15; + say "Equal point: $equal-point"; +} + +multi MAIN( #= Compares the C. scale to a named temperature scale + Str $scale where { $_ ~~ m:i/^(fahrenheit|kelvin|rankin)$/ } #= Name of scale (Fahrenheit, Kelvin or Rankin) +) { + given $scale.fc { + when "fahrenheit" { MAIN( 32 , 212 ); } + when "kelvin" { MAIN(273.15, 373.15); } + when "rankin" { MAIN(491.67, 671.67); } + } +} -- cgit From 6a9de49a1fceeb6e6c085e2dc17b91873e67e89f Mon Sep 17 00:00:00 2001 From: Alexander <39702500+threadless-screw@users.noreply.github.com> Date: Sat, 8 Jun 2019 13:18:50 +0000 Subject: ch-1.p6 --- challenge-011/ozzy/perl6/ch-1.p6 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 challenge-011/ozzy/perl6/ch-1.p6 (limited to 'challenge-011') diff --git a/challenge-011/ozzy/perl6/ch-1.p6 b/challenge-011/ozzy/perl6/ch-1.p6 new file mode 100644 index 0000000000..656cf2aca4 --- /dev/null +++ b/challenge-011/ozzy/perl6/ch-1.p6 @@ -0,0 +1,14 @@ +#!/usr/bin/env perl6 + +sub T_F ( Num $T_C ) { $T_C * (212-32)/100 + 32 } +sub T_C ( Num $T_F ) { ($T_F - 32) * 100/(212-32) } + +sub MAIN ( + Str $T #= Temperature +) +{ + my $Tn = $T.Num; + + printf("%7.2f degrees Celcius is equal to %7.2f degrees Fahrenheit.\n", $Tn, T_F($Tn)); + printf("%7.2f degrees Fahrenheit is equal to %7.2f degrees Celcius.\n", $Tn, T_C($Tn)); +} -- cgit From 3679a03c9a70a13cf5028739ad4d15a6a562c325 Mon Sep 17 00:00:00 2001 From: Alexander <39702500+threadless-screw@users.noreply.github.com> Date: Sat, 8 Jun 2019 13:19:30 +0000 Subject: Create ch-2.p6 --- challenge-011/ozzy/perl6/ch-2.p6 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 challenge-011/ozzy/perl6/ch-2.p6 (limited to 'challenge-011') diff --git a/challenge-011/ozzy/perl6/ch-2.p6 b/challenge-011/ozzy/perl6/ch-2.p6 new file mode 100644 index 0000000000..346fc299d2 --- /dev/null +++ b/challenge-011/ozzy/perl6/ch-2.p6 @@ -0,0 +1,21 @@ +#!/usr/bin/env perl6 +# +# Compose and print identity matrix +# +# Notes: +# - "is default" trait not implemented for shaped arrays +# - Partially dimensioned views of shaped arrays not yet implemented +# - Could use Math::Matrix + +sub MAIN (Int $dim) +{ + my @md[$dim;$dim]; + + for 0..$dim-1 -> $i { + for 0..$dim-1 -> $j { + @md[$i;$j]= $i==$j ?? 1 !! 0; + printf "%3i", @md[$i;$j]; + } + printf "\n"; + } +} -- cgit From 473e75c01cf702c79883061fdd04b040fb75625a Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Sat, 8 Jun 2019 22:23:17 +0800 Subject: challenge 11 #3 perl5 solution --- challenge-011/feng-chang/perl5/ch-3.pl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 challenge-011/feng-chang/perl5/ch-3.pl (limited to 'challenge-011') diff --git a/challenge-011/feng-chang/perl5/ch-3.pl b/challenge-011/feng-chang/perl5/ch-3.pl new file mode 100755 index 0000000000..469483b200 --- /dev/null +++ b/challenge-011/feng-chang/perl5/ch-3.pl @@ -0,0 +1,26 @@ +#!/bin/env perl + +use Modern::Perl; +use WWW::Mechanize; +use JSON; +use Data::Dumper; +use Getopt::Long; + +my $base_url = 'http://api.openweathermap.org/data/2.5/weather'; +my $app_id = 'appid=82642c9af842d8d57f7df99b291e0e75'; + +my $city = 'london'; +my $country = ''; + +GetOptions( + 'city=s' => \$city, + 'country=s' => \$country, +); + +my $m = WWW::Mechanize->new() or die "cannot initialize robot\n"; + +my $url = "$base_url?q=$city"; +$url .= ",$country" if $country; +$url .= "&$app_id"; + +print Dumper(decode_json($m->content)) if $m->get($url); -- cgit From d250eab734e2cf52fc99dc229efb117592cec4ff Mon Sep 17 00:00:00 2001 From: Daniel Mantovani Date: Sat, 8 Jun 2019 18:52:47 -0300 Subject: my proposed solution for challenge-011, ch-1.pl --- challenge-011/daniel-mantovani/perl5/ch-1.pl | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 challenge-011/daniel-mantovani/perl5/ch-1.pl (limited to 'challenge-011') diff --git a/challenge-011/daniel-mantovani/perl5/ch-1.pl b/challenge-011/daniel-mantovani/perl5/ch-1.pl new file mode 100644 index 0000000000..18e2b9a3cd --- /dev/null +++ b/challenge-011/daniel-mantovani/perl5/ch-1.pl @@ -0,0 +1,56 @@ +# 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. + +use strict; +use warnings; +use v5.10; + +# we start by writing a function (to_f) that converts from +# celcius to farenheit. As it is a linear function, we +# have to take into account de relationship on increments +# +# From the description, we know that a change from 0 to 100 +# celcius corresponde to a change from 32 to 212 farenheit, +# so 100 C <--> 180 F +# so to go from C to F we divide by 100, multiply by 180 and +# add 32. + +sub to_f { + return (shift) * 9 / 5 + 32; +} + +# now as a response to the challenge, we just try the bisection +# method https://en.wikipedia.org/wiki/Bisection_method. +# (We could just solve the ecuation using standard math +# but that would't be a computer challenge I guess). +# we start with -1000 and +1000 as endpoints, and +# 1/1000000 as tolerance +# 10000 max iterations (although 50 is more than enough for this +# linear, simple case) + +my ( $min, $max ) = ( -1000, 1000 ); +my $tol = 1 / 1_000_000; +my $nmax = 10_000; +my ( $c, $f, $found ); + +while ( $nmax-- ) { + $c = ( $min + $max ) / 2; + $f = to_f($c); + $found++ && last if abs( $f - $c ) < $tol; + my $fmin = to_f($min); + + # now compares signs of ($f - $c) vs ($fmin - $min) to decide + # the new interval + if ( $f > $c && $fmin > $min || $f < $c && $fmin < $min ) { + $min = $c; + } + else { + $max = $c; + } +} + +say $found + ? sprintf "Solution found: %.3f Centigrades is %.3f Farenheit", $c, $f + : "Error: no solution found"; -- cgit From 200df31e40095ad9494cc5f383d3cb35c73d9d7c Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sat, 8 Jun 2019 23:29:26 -0400 Subject: Challenge 11 by Jaldhar H. Vyas --- challenge-011/jaldhar-h-vyas/perl5/ch-1.pl | 13 +++++++++++++ challenge-011/jaldhar-h-vyas/perl5/ch-2.pl | 27 +++++++++++++++++++++++++++ challenge-011/jaldhar-h-vyas/perl6/ch-1.sh | 1 + challenge-011/jaldhar-h-vyas/perl6/ch-2.p6 | 13 +++++++++++++ 4 files changed, 54 insertions(+) create mode 100755 challenge-011/jaldhar-h-vyas/perl5/ch-1.pl create mode 100755 challenge-011/jaldhar-h-vyas/perl5/ch-2.pl create mode 100755 challenge-011/jaldhar-h-vyas/perl6/ch-1.sh create mode 100755 challenge-011/jaldhar-h-vyas/perl6/ch-2.p6 (limited to 'challenge-011') diff --git a/challenge-011/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-011/jaldhar-h-vyas/perl5/ch-1.pl new file mode 100755 index 0000000000..323424fe7e --- /dev/null +++ b/challenge-011/jaldhar-h-vyas/perl5/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; + +my $x = 0; + +# We know x cannot be positive because 0C = 32F... +while ($x != 32 + 1.8 * $x) { + $x--; # ...so count backwards. +} + +say $x; \ No newline at end of file diff --git a/challenge-011/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-011/jaldhar-h-vyas/perl5/ch-2.pl new file mode 100755 index 0000000000..5cd275cae8 --- /dev/null +++ b/challenge-011/jaldhar-h-vyas/perl5/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; + +sub usage() { + print <<"-USAGE-"; +Usage: + $0 + + the size of the identity matrix +-USAGE- +exit(1); +} + +my $n = shift // usage(); + +if ($n < 2) { + usage(); +} + +for my $i (0 .. $n - 1) { + for my $j (0 .. $n - 1) { + print (($j == $i) ? '1 ' : '0 '); + } + print "\n"; +} diff --git a/challenge-011/jaldhar-h-vyas/perl6/ch-1.sh b/challenge-011/jaldhar-h-vyas/perl6/ch-1.sh new file mode 100755 index 0000000000..dfe59c1eee --- /dev/null +++ b/challenge-011/jaldhar-h-vyas/perl6/ch-1.sh @@ -0,0 +1 @@ +perl6 -e 'say (0, -1 ... { $_ == 32 + 1.8 * $_ })[*-1];' \ No newline at end of file diff --git a/challenge-011/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-011/jaldhar-h-vyas/perl6/ch-2.p6 new file mode 100755 index 0000000000..2019d97295 --- /dev/null +++ b/challenge-011/jaldhar-h-vyas/perl6/ch-2.p6 @@ -0,0 +1,13 @@ +#!/usr/bin/perl6 + +multi sub MAIN( + Int $n where $n > 1#= the size of the identity matrix +) { + + for (0 .. $n - 1) -> $i { + for (0 .. $n - 1) -> $j { + print ($j == $i) ?? '1 ' !! '0 '; + } + print "\n"; + } +} \ No newline at end of file -- cgit From 81d59bc94b23b67ab9b2046bd9e36cd7000808a4 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sat, 8 Jun 2019 23:36:15 -0400 Subject: tiny formatting error. --- challenge-011/jaldhar-h-vyas/perl5/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-011') diff --git a/challenge-011/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-011/jaldhar-h-vyas/perl5/ch-2.pl index 5cd275cae8..3cfc95dba7 100755 --- a/challenge-011/jaldhar-h-vyas/perl5/ch-2.pl +++ b/challenge-011/jaldhar-h-vyas/perl5/ch-2.pl @@ -10,7 +10,7 @@ Usage: the size of the identity matrix -USAGE- -exit(1); + exit(1); } my $n = shift // usage(); -- cgit From f70444df60782c54955c25d7b462b002a16fa498 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 9 Jun 2019 00:19:26 -0400 Subject: solution for challenge 011 --- challenge-011/adam-russell/blog.txt | 1 + challenge-011/adam-russell/perl5/ch-1.pl | 14 ++++++++++++++ challenge-011/adam-russell/perl5/ch-2.pl | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 challenge-011/adam-russell/blog.txt create mode 100644 challenge-011/adam-russell/perl5/ch-1.pl create mode 100644 challenge-011/adam-russell/perl5/ch-2.pl (limited to 'challenge-011') diff --git a/challenge-011/adam-russell/blog.txt b/challenge-011/adam-russell/blog.txt new file mode 100644 index 0000000000..1b39aba56b --- /dev/null +++ b/challenge-011/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/3900.html diff --git a/challenge-011/adam-russell/perl5/ch-1.pl b/challenge-011/adam-russell/perl5/ch-1.pl new file mode 100644 index 0000000000..a723c744a4 --- /dev/null +++ b/challenge-011/adam-russell/perl5/ch-1.pl @@ -0,0 +1,14 @@ +use strict; +use warnings; +## +# 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. +# °F = (°C * 9/5) + 32 +## +for my $c (-100 .. 100){ + my $f = ($c * (9/5)) + 32; + if($f == $c){ + print "°F = °C at $f\n"; + } +} diff --git a/challenge-011/adam-russell/perl5/ch-2.pl b/challenge-011/adam-russell/perl5/ch-2.pl new file mode 100644 index 0000000000..d29c2bf377 --- /dev/null +++ b/challenge-011/adam-russell/perl5/ch-2.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +## +# Write a script to create an Identity Matrix for the given size. +# For example, if the size is 4, then create Identity Matrix 4x4. +## +use constant SIZE => 10; + +my @a; +for my $i (0 .. SIZE - 1){ + my @b = (0) x SIZE; + $b[$i] = 1; + push @a, \@b; +} +print SIZE . " x " . SIZE . " identity matrix:\n"; +for my $i (0 .. SIZE - 1){ + print "\t" . join(" ", @{$a[$i]}) . "\n"; +} -- cgit From d4b277b14cf4fa525814ff76e0665f2151022fe0 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 9 Jun 2019 05:30:01 +0100 Subject: - Added solutions by Arne Sommer. --- challenge-011/arne-sommer/blog.txt | 1 + challenge-011/arne-sommer/perl6/ch-1.p6 | 19 +++++++++++++++++++ challenge-011/arne-sommer/perl6/ch-1a.p6 | 3 +++ challenge-011/arne-sommer/perl6/ch-2.p6 | 9 +++++++++ challenge-011/arne-sommer/perl6/ch-2a.p6 | 26 ++++++++++++++++++++++++++ challenge-011/arne-sommer/perl6/ch-2b.p6 | 17 +++++++++++++++++ challenge-011/arne-sommer/perl6/ch-2c.p6 | 10 ++++++++++ challenge-011/arne-sommer/perl6/ch-2d.p6 | 10 ++++++++++ 8 files changed, 95 insertions(+) create mode 100644 challenge-011/arne-sommer/blog.txt create mode 100755 challenge-011/arne-sommer/perl6/ch-1.p6 create mode 100755 challenge-011/arne-sommer/perl6/ch-1a.p6 create mode 100755 challenge-011/arne-sommer/perl6/ch-2.p6 create mode 100755 challenge-011/arne-sommer/perl6/ch-2a.p6 create mode 100755 challenge-011/arne-sommer/perl6/ch-2b.p6 create mode 100755 challenge-011/arne-sommer/perl6/ch-2c.p6 create mode 100755 challenge-011/arne-sommer/perl6/ch-2d.p6 (limited to 'challenge-011') diff --git a/challenge-011/arne-sommer/blog.txt b/challenge-011/arne-sommer/blog.txt new file mode 100644 index 0000000000..ff33263a07 --- /dev/null +++ b/challenge-011/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://perl6.eu/fc-matrix.html diff --git a/challenge-011/arne-sommer/perl6/ch-1.p6 b/challenge-011/arne-sommer/perl6/ch-1.p6 new file mode 100755 index 0000000000..8160d12bfe --- /dev/null +++ b/challenge-011/arne-sommer/perl6/ch-1.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/env perl6 + +my $c = 0; + +loop +{ + my $f = celcius2fahrenheit($c); + if $f <= $c + { + say "Fahrenheit ($f) and Celsius ($c) are equal(ish)."; + last; + } + $c--; +} + +sub celcius2fahrenheit ($c) +{ + return $c * 1.8 + 32; +} diff --git a/challenge-011/arne-sommer/perl6/ch-1a.p6 b/challenge-011/arne-sommer/perl6/ch-1a.p6 new file mode 100755 index 0000000000..9799e85810 --- /dev/null +++ b/challenge-011/arne-sommer/perl6/ch-1a.p6 @@ -0,0 +1,3 @@ +#! /usr/bin/env perl6 + +say "Fahrenheit and Celsius are equal at -40."; diff --git a/challenge-011/arne-sommer/perl6/ch-2.p6 b/challenge-011/arne-sommer/perl6/ch-2.p6 new file mode 100755 index 0000000000..89ff615443 --- /dev/null +++ b/challenge-011/arne-sommer/perl6/ch-2.p6 @@ -0,0 +1,9 @@ +#! /usr/bin/env perl6 + +use Math::Matrix; + +unit sub MAIN (Int $size where $size > 0); + +my $im = Math::Matrix.new-identity( $size ); + +say $im; diff --git a/challenge-011/arne-sommer/perl6/ch-2a.p6 b/challenge-011/arne-sommer/perl6/ch-2a.p6 new file mode 100755 index 0000000000..0dd48982f3 --- /dev/null +++ b/challenge-011/arne-sommer/perl6/ch-2a.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/env perl6 + +unit sub MAIN (Int $size where $size > 0); + +my @im[$size;$size] = 0 xx $size xx $size; + +@im[$_;$_] = 1 for ^$size; + +print @im.&nice-format; + +sub nice-format (@shaped) +{ + my ($row, $col) = @shaped.shape; + + my $result; + + for ^$row -> $x + { + for ^$col -> $y + { + $result ~= @shaped[$x;$y] ~ " "; + } + $result ~= "\n"; + } + return $result; +} \ No newline at end of file diff --git a/challenge-011/arne-sommer/perl6/ch-2b.p6 b/challenge-011/arne-sommer/perl6/ch-2b.p6 new file mode 100755 index 0000000000..6c95394f34 --- /dev/null +++ b/challenge-011/arne-sommer/perl6/ch-2b.p6 @@ -0,0 +1,17 @@ +#! /usr/bin/env perl6 + +unit sub MAIN (Int $size where $size > 0); + +# my @im = 0 xx $size xx $size; +# @im[$_;$_] = 1 for ^$size; + +my @row = (1, 0 xx $size -1).flat; +my @x; @x.push: @row.rotate(- $_) for ^$size; +my @im = @x; + +print @im.&nice-format; + +sub nice-format (@array) +{ + return (@($_).join(" ") for @array).join("\n") ~ "\n"; +} \ No newline at end of file diff --git a/challenge-011/arne-sommer/perl6/ch-2c.p6 b/challenge-011/arne-sommer/perl6/ch-2c.p6 new file mode 100755 index 0000000000..2979c44550 --- /dev/null +++ b/challenge-011/arne-sommer/perl6/ch-2c.p6 @@ -0,0 +1,10 @@ +#! /usr/bin/env perl6 + +unit sub MAIN (Int $size where $size > 0); + +my @im[$size;$size] = 0 xx $size xx $size; + +@im[$_;$_] = 1 for ^$size; + +say @im; + diff --git a/challenge-011/arne-sommer/perl6/ch-2d.p6 b/challenge-011/arne-sommer/perl6/ch-2d.p6 new file mode 100755 index 0000000000..2458666409 --- /dev/null +++ b/challenge-011/arne-sommer/perl6/ch-2d.p6 @@ -0,0 +1,10 @@ +#! /usr/bin/env perl6 + +unit sub MAIN (Int $size where $size > 0); + +my @row = (1, 0 xx $size -1).flat; +my @x; @x.push: @row.rotate(- $_) for ^$size; +my @im[$size;$size] = @x; + +say @im; + -- cgit From 8cdc531858dfa1b8f3c6c90a1d3058a92bfd8f78 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 9 Jun 2019 05:39:51 +0100 Subject: - Added solutions by Jo Christian Oterhals. --- challenge-011/jo-christian-oterhals/perl6/ch-1.p6 | 28 +++++++++++----------- challenge-011/jo-christian-oterhals/perl6/ch-1.sh | 1 - challenge-011/jo-christian-oterhals/perl6/ch-1a.p6 | 3 +++ challenge-011/jo-christian-oterhals/perl6/ch-2.p6 | 5 ++++ 4 files changed, 22 insertions(+), 15 deletions(-) delete mode 100644 challenge-011/jo-christian-oterhals/perl6/ch-1.sh create mode 100644 challenge-011/jo-christian-oterhals/perl6/ch-1a.p6 create mode 100644 challenge-011/jo-christian-oterhals/perl6/ch-2.p6 (limited to 'challenge-011') diff --git a/challenge-011/jo-christian-oterhals/perl6/ch-1.p6 b/challenge-011/jo-christian-oterhals/perl6/ch-1.p6 index 54955d03c7..aa12ae87ce 100644 --- a/challenge-011/jo-christian-oterhals/perl6/ch-1.p6 +++ b/challenge-011/jo-christian-oterhals/perl6/ch-1.p6 @@ -1,21 +1,21 @@ #!/usr/bin/env perl6 -multi MAIN( #= Compares the C. scale to any scale you dream up - Real $f, #= freezing point in custom scale - Real $b #= boiling point in custom scale +multi MAIN( #= Compares the Celcius scale to whatever scale you dream up + Real $f, #= freezing point in this scale + Real $b #= boiling point in this scale ) { - say "There is no equal point for this scale." and exit if $b - $f == 100; - my $equal-point = $f / (1 - (($b - $f) / 100)); - say "The calculated equal point is only theoretical as it is below absolute zero." if $equal-point < -273.15; - say "Equal point: $equal-point"; + say "There is no equal point for this scale." and exit if $b - $f == 100; + my $equal-point = $f / (1 - (($b - $f) / 100)); + say "The calculated equal point is only theoretical as it is below absolute zero." if $equal-point < -273.15; + say "Equal point: $equal-point"; } -multi MAIN( #= Compares the C. scale to a named temperature scale - Str $scale where { $_ ~~ m:i/^(fahrenheit|kelvin|rankin)$/ } #= Name of scale (Fahrenheit, Kelvin or Rankin) +multi MAIN( #= Compares the Celcius scale to a given temperature scale + Str $scale where { $_ ~~ m:i/^(fahrenheit|kelvin|rankin)$/ } #= Name of scale (Fahrenheit, Kelvin or Rankin) ) { - given $scale.fc { - when "fahrenheit" { MAIN( 32 , 212 ); } - when "kelvin" { MAIN(273.15, 373.15); } - when "rankin" { MAIN(491.67, 671.67); } - } + given $scale.fc { + when "fahrenheit" { MAIN( 32 , 212 ); } + when "kelvin" { MAIN(273.15, 373.15); } + when "rankin" { MAIN(491.67, 671.67); } + } } diff --git a/challenge-011/jo-christian-oterhals/perl6/ch-1.sh b/challenge-011/jo-christian-oterhals/perl6/ch-1.sh deleted file mode 100644 index 46451905ba..0000000000 --- a/challenge-011/jo-christian-oterhals/perl6/ch-1.sh +++ /dev/null @@ -1 +0,0 @@ -perl6 -e 'sub MAIN($a, $b) { say "Equal point: " ~ ( $b - $a == 100 ?? "None" !! $a / (1 - (($b - $a) / 100))) }' 32 212 diff --git a/challenge-011/jo-christian-oterhals/perl6/ch-1a.p6 b/challenge-011/jo-christian-oterhals/perl6/ch-1a.p6 new file mode 100644 index 0000000000..bff294d1a6 --- /dev/null +++ b/challenge-011/jo-christian-oterhals/perl6/ch-1a.p6 @@ -0,0 +1,3 @@ +#!/usr/bin/env perl6 + +-> $a, $b { say "Equal point: " ~ ( $b - $a == 100 ?? "None" !! $a / (1 - (($b - $a) / 100))) }(32,212) diff --git a/challenge-011/jo-christian-oterhals/perl6/ch-2.p6 b/challenge-011/jo-christian-oterhals/perl6/ch-2.p6 new file mode 100644 index 0000000000..db71e84635 --- /dev/null +++ b/challenge-011/jo-christian-oterhals/perl6/ch-2.p6 @@ -0,0 +1,5 @@ +#!/usr/bin/env perl6 + +my &idm = -> $size { gather for ^$size -> $y { take map { Int($_ == $y) }, ^$size } }; +.join(' ').say for idm(4); # 4... 2... 16... or whatever... + -- cgit From 9232205b74dbb9c02dec10e8dcd50098623057c3 Mon Sep 17 00:00:00 2001 From: Trevor Vaughan Date: Sun, 9 Jun 2019 00:12:08 -0700 Subject: On branch new-branch Changes to be committed: new file: challenge-011/athanasius/perl5/ch-1.pl new file: challenge-011/athanasius/perl5/ch-2.pl new file: challenge-011/athanasius/perl5/ch-3.pl --- challenge-011/athanasius/perl5/ch-1.pl | 75 ++++++++ challenge-011/athanasius/perl5/ch-2.pl | 78 ++++++++ challenge-011/athanasius/perl5/ch-3.pl | 327 +++++++++++++++++++++++++++++++++ 3 files changed, 480 insertions(+) create mode 100644 challenge-011/athanasius/perl5/ch-1.pl create mode 100644 challenge-011/athanasius/perl5/ch-2.pl create mode 100644 challenge-011/athanasius/perl5/ch-3.pl (limited to 'challenge-011') diff --git a/challenge-011/athanasius/perl5/ch-1.pl b/challenge-011/athanasius/perl5/ch-1.pl new file mode 100644 index 0000000000..0d502980a0 --- /dev/null +++ b/challenge-011/athanasius/perl5/ch-1.pl @@ -0,0 +1,75 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 011 +========================= + +Challenge #1 +------------ + +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*. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2019 Perlmonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use utf8; +use Const::Fast; + +const my $FAHRENHEIT_FREEZING => 32; +const my $FAHRENHEIT_BOILING => 212; +const my $CELSIUS_FREEZING => 0; +const my $CELSIUS_BOILING => 100; + +$| = 1; + +MAIN: +{ + # At the equal point, degrees Fahrenheit (f) equal degrees Celsius, so + # f = f × ((212 - 32) / 100) + 32 + # => f × (1 - 9/5) = 32 + # => f = 32 / -4/5 + # = -40. + + my $equal = $FAHRENHEIT_FREEZING / + (1 - (($FAHRENHEIT_BOILING - $FAHRENHEIT_FREEZING) / $CELSIUS_BOILING)); + + printf "\nThe equal point is %3.1f°\n", $equal; + printf "This is %s by conversion from Celsius to Fahrenheit\n", + $equal == c2f($equal) ? 'confirmed' : 'refuted'; + printf "This is %s by conversion from Fahrenheit to Celsius\n", + $equal == f2c($equal) ? 'confirmed' : 'refuted'; +} + +# Convert Celsius to Fahrenheit + +sub c2f +{ + my ($degrees_celsius) = @_; + + return ($degrees_celsius * + (($F