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/aaron-sherman') 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