diff options
Diffstat (limited to 'challenge-011')
68 files changed, 1772 insertions, 11 deletions
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 => <F C>, + freezing => [32, 0], + boiling => [212, 100] ); + +proto MAIN($input?) {*} + +multi MAIN( + #= Convert the given value e.g. 37C + Str $input where m/<after \d><alpha>$/ +) { + my $value = +$input.substr(0,*-1); + my $from = $input.substr(*-1); + my $units = %knowns<unit>.join('/'); + die "$from is not one of $units" if !%knowns<unit>.grep: * eq $from; + my $to = %knowns<unit>.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<unit> +) { + my $to = %knowns<unit>.grep({$_ ne $input}).first; + my $conversion = conversion(:$to); + my $scale = $conversion<scale>.nude.join('/'); + say "Conversion to $to: {$input}*{$scale} + {$conversion<offset>}"; +} + +#= by default show crossover for units +multi MAIN() { + my $units = %knowns<unit>.join('/'); + say "$units cross over at {crossover}"; +} + +sub other-unit($unit) { return %knowns<unit>.grep({$_ ne $unit}).first } + +sub convert($value, :$to where $to eq [|] |%knowns<unit>) { + my $conv = conversion(:$to); + return $value*$conv<scale> + $conv<offset>; +} + +sub crossover() { + my $conv = conversion(:to<F>); + return $conv<offset>/(1-$conv<scale>); +} + +sub conversion(:$to where $to eq [|] |%knowns<unit>) { + my $to-index = %knowns<unit>.first: * eq $to, :k; + # Get the offset between the two + my $offset = [-] |%knowns<freezing>; + my $scale = [/] (|%knowns<boiling> <<->> |%knowns<freezing>); + 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 => <F C>, + freezing => [32, 0], + boiling => [212, 100] ); + +proto MAIN($input?) {*} + +multi MAIN( + #= Convert the given value e.g. 37C + Str $input where m/<after \d><alpha>$/ +) { + my $value = +$input.substr(0,*-1); + my $from = $input.substr(*-1); + my $units = %knowns<unit>.join('/'); + die "$from is not one of $units" if !%knowns<unit>.grep: * eq $from; + my $to = %knowns<unit>.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<unit> +) { + my $to = %knowns<unit>.grep({$_ ne $input}).first; + my $conversion = conversion(:$to); + my $scale = $conversion<scale>.nude.join('/'); + say "Conversion to $to: {$input}*{$scale} + {$conversion<offset>}"; +} + +#= by default show crossover for units +multi MAIN() { + my $units = %knowns<unit>.join('/'); + say "$units cross over at {crossover}"; +} + +sub other-unit($unit) { return %knowns<unit>.grep({$_ ne $unit}).first } + +sub convert($value, :$to where $to eq [|] |%knowns<unit>) { + my $conv = conversion(:$to); + return $value*$conv<scale> + $conv<offset>; +} + +sub crossover() { + my $conv = conversion(:to<F>); + return $conv<offset>/(1-$conv<scale>); +} + +sub conversion(:$to where $to eq [|] |%knowns<unit>) { + my $to-index = %knowns<unit>.first: * eq $to, :k; + # Get the offset between the two + my $offset = [-] |%knowns<freezing>; + my $scale = [/] (|%knowns<boiling> <<->> |%knowns<freezing>); + 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/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"; +} 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 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 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 <matrix-size>" unless $ARGV[0] && $ARGV[0] =~ /\d+/; + +my $n = int($ARGV[0]) - 1; + +for (0..$n) { print " 0" x $_ . " 1" . " 0" x ($n - $_) . $/; } 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; + 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 +###################################################### |
