diff options
| author | dcw <d.white@imperial.ac.uk> | 2021-08-01 21:23:03 +0100 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2021-08-01 21:23:03 +0100 |
| commit | b1b28010fbf8aafa1820f14897a6548fa655467b (patch) | |
| tree | 93889238472e897910ace555eadb7f4c37656d32 | |
| parent | 496f89bf32c417efde1beffc8571075a56b46f26 (diff) | |
| download | perlweeklychallenge-club-b1b28010fbf8aafa1820f14897a6548fa655467b.tar.gz perlweeklychallenge-club-b1b28010fbf8aafa1820f14897a6548fa655467b.tar.bz2 perlweeklychallenge-club-b1b28010fbf8aafa1820f14897a6548fa655467b.zip | |
imported my solution's to challenge 123: 2 easy tasks this time..
| -rw-r--r-- | challenge-123/duncan-c-white/README | 70 | ||||
| -rwxr-xr-x | challenge-123/duncan-c-white/perl/ch-1.pl | 70 | ||||
| -rwxr-xr-x | challenge-123/duncan-c-white/perl/ch-2.pl | 63 |
3 files changed, 163 insertions, 40 deletions
diff --git a/challenge-123/duncan-c-white/README b/challenge-123/duncan-c-white/README index 943444f99f..b7d8d96453 100644 --- a/challenge-123/duncan-c-white/README +++ b/challenge-123/duncan-c-white/README @@ -1,57 +1,47 @@ -Task 1: "Average of Stream +Task 1: "Ugly Numbers -You are given a stream of numbers, @N. +You are given an integer $n >= 1. -Write a script to print the average of the stream at every point. +Write a script to find the $nth element of Ugly Numbers. + +Ugly numbers are those number whose prime factors are 2, 3 or 5. +For example, the first 10 Ugly Numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. Example -Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) -Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... + Input: $n = 7 + Output: 8 -Average of first number is 10. -Average of first 2 numbers (10+20)/2 = 15 -Average of first 3 numbers (10+20+30)/3 = 20 -Average of first 4 numbers (10+20+30+40)/4 = 25 and so on. + Input: $n = 10 + Output: 12 " -My notes: sounds quite simple, just requires a running total. - +My notes: sounds quite simple. I thought of digging out primes and +factors code from previous PWCs, but the only primes we care about are +2, 3 and 5. -Task 2: "Basketball Points -You are given a score $S. +Task 2: "Square Points -You can win basketball points e.g. 1 point, 2 points and 3 points. +You are given coordinates of four points +i.e. (x1, y1), (x2, y2), (x3, y3) and (x4, y4). -Write a script to find out the different ways you can score $S. +Write a script to find out if the given four points form a square. Example -Input: $S = 4 -Output: 1 1 1 1 - 1 1 2 - 1 2 1 - 1 3 - 2 1 1 - 2 2 - 3 1 - -Input: $S = 5 -Output: 1 1 1 1 1 - 1 1 1 2 - 1 1 2 1 - 1 1 3 - 1 2 1 1 - 1 2 2 - 1 3 1 - 2 1 1 1 - 2 1 2 - 2 2 1 - 2 3 - 3 1 1 + Input: x1 = 10, y1 = 20 + x2 = 20, y2 = 20 + x3 = 20, y3 = 10 + x4 = 10, y4 = 10 + Output: 1 as the given coordinates form a square. + + Input: x1 = 12, y1 = 24 + x2 = 16, y2 = 10 + x3 = 20, y3 = 12 + x4 = 18, y4 = 16 + Output: 0 as the given coordinates doesn't form a square. " -My notes: Assuming I'm understanding it right, all combinations of -removing 1, 2 or 3 from N repeatedly. Easyish (recursive). -NOTE: surely the $S = 5 example omits one last solution "3 2"? +My notes: sounds surprisingly easy for a task 2. I guess the points +can be in any order. diff --git a/challenge-123/duncan-c-white/perl/ch-1.pl b/challenge-123/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..38fde07b51 --- /dev/null +++ b/challenge-123/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,70 @@ +#!/usr/bin/perl +# +# Task 1: "Ugly Numbers +# +# You are given an integer $n >= 1. +# +# Write a script to find the $nth element of Ugly Numbers. +# +# Ugly numbers are those number whose prime factors are 2, 3 or 5. +# For example, the first 10 Ugly Numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. +# +# Example +# +# Input: $n = 7 +# Output: 8 +# +# Input: $n = 10 +# Output: 12 +# " +# +# My notes: sounds quite simple. I thought of digging out primes and +# factors code from previous PWCs, but the only primes we care about are +# 2, 3 and 5. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +#use Data::Dumper; + +die "Usage: ugly-number N\n" unless @ARGV == 1; +my $n = shift; +die "ugly-number: N ($n) must be >= 1\n" if $n<1; + +my $x = nth_ugly( $n ); +say $x; + + +# +# my $ugly = isugly( $x ); +# Return 1 iff $x is ugly; return 0 otherwise. +# +fun isugly( $x ) +{ + return 0 if $x < 1; + while( $x % 5 == 0 ) { $x /= 5 } + while( $x % 3 == 0 ) { $x /= 3 } + while( $x % 2 == 0 ) { $x /= 2 } + return $x==1 ? 1 : 0; +} + + +# +# my $x = nth_ugly( $n ); +# Determine and result $x, the $n-th ugly number. +# +fun nth_ugly( $n ) +{ + for( my $i=1; ; $i++ ) + { + if( isugly($i) ) + { + if( --$n==0 ) + { + return $i; + } + } + } +} diff --git a/challenge-123/duncan-c-white/perl/ch-2.pl b/challenge-123/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..5bdd745893 --- /dev/null +++ b/challenge-123/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/perl +# +# Task 2: "Square Points +# +# You are given coordinates of four points +# i.e. (x1, y1), (x2, y2), (x3, y3) and (x4, y4). +# +# Write a script to find out if the given four points form a square. +# +# Example +# +# Input: x1 = 10, y1 = 20 +# x2 = 20, y2 = 20 +# x3 = 20, y3 = 10 +# x4 = 10, y4 = 10 +# Output: 1 as the given coordinates form a square. +# +# Input: x1 = 12, y1 = 24 +# x2 = 16, y2 = 10 +# x3 = 20, y3 = 12 +# x4 = 18, y4 = 16 +# Output: 0 as the given coordinates doesn't form a square. +# " +# +# My notes: sounds surprisingly easy for a task 2. I guess the points +# can be in any order. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Getopt::Long; +use Data::Dumper; + +my $debug = 0; +die "Usage: square-points X1 Y2 X2 Y2 X3 Y3 X4 Y4\n" unless + GetOptions( "debug" => \$debug ) && @ARGV==8; +my( $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4 ) = @ARGV; +my @p = ( [$x1,$y1], [$x2,$y2], [$x3,$y3], [$x4,$y4] ); + +# +# my $square = issquare( @p ); +# Return 1 iff the 4 points in @p make a square, 0 otherwise. +# +fun issquare( @p ) +{ + # first, let's sort the coordinates by x-first-and-then-y-if-xs-same + @p = sort { $a->[0]<=>$b->[0] || $a->[1]<=>$b->[1] } @p; + + #print Dumper \@p; + + return 0 unless $p[0]->[0] == $p[1]->[0]; + return 0 unless $p[2]->[0] == $p[3]->[0]; + my $size = $p[1]->[1] - $p[0]->[1]; + return 0 if $size==0; + return 0 unless $p[3]->[1] - $p[2]->[1] == $size; + return 1; +} + + +my $square = issquare( @p ); +say $square; |
