From 2fc0f6f4d604d74f77edbcafdd9db98b001aa50a Mon Sep 17 00:00:00 2001 From: dcw Date: Sun, 9 Jun 2019 11:04:20 +0100 Subject: did this week's challenges, both trivial --- challenge-011/duncan-c-white/README | 32 +++++++++------ challenge-011/duncan-c-white/perl5/ch-1.pl | 62 ++++++++++++++++++++++++++++++ challenge-011/duncan-c-white/perl5/ch-2.pl | 31 +++++++++++++++ 3 files changed, 114 insertions(+), 11 deletions(-) create mode 100755 challenge-011/duncan-c-white/perl5/ch-1.pl create mode 100755 challenge-011/duncan-c-white/perl5/ch-2.pl diff --git a/challenge-011/duncan-c-white/README b/challenge-011/duncan-c-white/README index fb3a863c80..3ff28fa716 100644 --- a/challenge-011/duncan-c-white/README +++ b/challenge-011/duncan-c-white/README @@ -1,14 +1,24 @@ -Challenge 1: "Create a script which takes a list of numbers from -command line and print the same in the compact form. For example, if -you pass 1,2,3,4,9,10,14,15,16 then it should print the compact form -like 1-4,9,10,14-16.." +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 Â," -Quite simple and dull problem. But ok, let's have a go. +My notes: Isn't that just Maths? solve F = 9/5C + 32 for F==C? -Challenge 2: "Create a script to calculate Ramanujan's constant with at -least 32 digits of precision." +C = 9/5C + 32 => 4/5C = -32 => C = 5/4 x -32 = 5 x -8 = -40 -Never heard of this constant, seems to be e^(pi*sqrt(163)), which is -"very nearly an integer", I don't particularly care about abtruse mathematical -formulae. But ok, Perl's built in module biggrat will let you do this anyway, -specifying accuracy 32; see ch-2.sh for the oneliner. +But if I have to "compute" something that I should obviously "solve by +algebra", could I do some sort of "where do two lines intersect" solver? +Let's have a go. + + +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." + +My notes: + +Surely that's incredibly straight forward. The identity matrix has 1s +on the leading diagonal and 0s everywhere else. But should we create +it in memory as a 2-D array and print that out, or just print out the +identity matrix? Let's choose the latter as it's simpler and more direct, +even though the former approach would be more useful in real life, as +presumably this is going to be one operation in a more general Matrix +class/module [really, these questions need to be BETTER SPECIFIED to +clarify this sort of thing] diff --git a/challenge-011/duncan-c-white/perl5/ch-1.pl b/challenge-011/duncan-c-white/perl5/ch-1.pl new file mode 100755 index 0000000000..8597a87110 --- /dev/null +++ b/challenge-011/duncan-c-white/perl5/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +# 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." +# +# My notes: Isn't that just Maths? solve F = 9/5C + 32 for F==C? +# +# C = 9/5C + 32 => 4/5C = -32 => C = 5/4 x -32 = 5 x -8 = -40 +# +# But if I have to "compute" something that I should obviously "solve by +# algebra", could I do some sort of "where do two lines intersect" solver? +# Let's have a go. + + +use strict; +use warnings; +use Function::Parameters; +use Data::Dumper; + +my $c = 0; +my $delta = 3.0; +my $epsilon = 0.000001; + +# +# my $f = fahr($c); +# Convert celsius $c to fahrenheit $f. +# +fun fahr( $c ) +{ + return 9.0*$c/5.0 + 32; +} + +# +# my $error = offby( $c ); +# Compute and return "how far is fahr(c) off by (different from c)" +# +fun offby( $c ) +{ + return abs(fahr($c)-$c); +} + + +#die "c=0, f=", fahr(0), "\nc=100, f=", fahr(100), "\n"; + +# compute the intersection point of F = 9/5C + 32 and C = 5/9(F-32), +# ie. the value of c for which F(c)==c, +# using the fact that | F(c)-c | diminishes monotonically as we get +# closer to the solution value of c +# +do { + my $sd = ( offby($c-$delta) < offby($c) ) ? -$delta : $delta; + while( offby($c+$sd) < offby($c) ) + { + $c += $sd; + } + print "c=$c, diff=", offby($c), ", delta=$delta\n"; + $delta /= 10.0; +} while( abs( fahr($c)-$c ) > $epsilon ); + +print "c=$c\n"; diff --git a/challenge-011/duncan-c-white/perl5/ch-2.pl b/challenge-011/duncan-c-white/perl5/ch-2.pl new file mode 100755 index 0000000000..2662e3b6d6 --- /dev/null +++ b/challenge-011/duncan-c-white/perl5/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +# 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." +# +# My notes: +# +# Surely that's incredibly straight forward. The identity matrix has 1s on the +# leading diagonal and 0s everywhere else. But should we create it in memory +# as a 2-D array and print that out, or just print out the identity matrix? +# Let's choose the latter as it's simpler and more direct, even though the +# former approach would be more useful in real life, as presumably this is +# going to be one operation in a Matrix class/module [really, these questions +# need to be BETTER SPECIFIED to clarify this sort of thing] + +die "Usage: ch-2.pl N\n" unless @ARGV == 1; + +my $n = shift; + +die "ch-2.pl: n ($n) must be > 0\n" unless $n>0; + +for( $row=0; $row<$n; $row++ ) +{ + my $line =''; + for( $col=0; $col<$n; $col++ ) + { + my $ch = ($row==$col)?'1':'0'; + $line .= $ch; + } + print "$line\n"; +} -- cgit