diff options
| author | dcw <d.white@imperial.ac.uk> | 2021-07-25 19:22:08 +0100 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2021-07-25 19:22:08 +0100 |
| commit | c25d8be984c76b56f15326e4f83710e8ee92de26 (patch) | |
| tree | a16a10d12b57ddb001d69c72c4316a28ccf892f9 | |
| parent | ddfcc4d4ae8621000f5984c5503f9afe6d327cbd (diff) | |
| download | perlweeklychallenge-club-c25d8be984c76b56f15326e4f83710e8ee92de26.tar.gz perlweeklychallenge-club-c25d8be984c76b56f15326e4f83710e8ee92de26.tar.bz2 perlweeklychallenge-club-c25d8be984c76b56f15326e4f83710e8ee92de26.zip | |
imported my solutions to this week's challenges - I think there was a bug in task 2? - see README
| -rw-r--r-- | challenge-122/duncan-c-white/README | 74 | ||||
| -rwxr-xr-x | challenge-122/duncan-c-white/perl/ch-1.pl | 40 | ||||
| -rwxr-xr-x | challenge-122/duncan-c-white/perl/ch-2.pl | 80 |
3 files changed, 160 insertions, 34 deletions
diff --git a/challenge-122/duncan-c-white/README b/challenge-122/duncan-c-white/README index a67e73b42b..943444f99f 100644 --- a/challenge-122/duncan-c-white/README +++ b/challenge-122/duncan-c-white/README @@ -1,51 +1,57 @@ -Task 1: "Invert Bit +Task 1: "Average of Stream -You are given integers 0 <= $m <= 255 and 1 <= $n <= 8. +You are given a stream of numbers, @N. -Write a script to invert $n bit from the end of the binary representation -of $m and print the decimal representation of the new binary number. +Write a script to print the average of the stream at every point. Example - Input: $m = 12, $n = 3 - Output: 8 +Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) +Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... -Binary representation of $m = 00001100 -Invert 3rd bit from the end = 00001000 -Decimal equivalent of 00001000 = 8 - - Input $m = 18, $n = 4 - Output: 26 - -Binary representation of $m = 00010010 -Invert 4th bit from the end = 00011010 -Decimal equivalent of 00011010 = 26 +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. " -My notes: trivial again. +My notes: sounds quite simple, just requires a running total. -Task 2: "Travelling Salesman -Submitted by: Jorg Sommrey +Task 2: "Basketball Points -You are given a NxN matrix containing the distances between N cities. +You are given a score $S. -Write a script to find a round trip of minimum length visiting all N -cities exactly once and returning to the start. +You can win basketball points e.g. 1 point, 2 points and 3 points. -Example +Write a script to find out the different ways you can score $S. -Matrix: [0, 5, 2, 7] - [5, 0, 5, 3] - [3, 1, 0, 6] - [4, 5, 4, 0] - -Output: - length = 10 - tour = (0 2 1 3 0) +Example -BONUS 1: For a given number N, create a random NxN distance matrix and find a solution for this matrix. -BONUS 2: Find a solution for a random matrix of size 15x15 or 20x20 +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 " -My notes: gurk! TSP is hard. I'm tired and I'm not doing that. +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"? diff --git a/challenge-122/duncan-c-white/perl/ch-1.pl b/challenge-122/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..4562cd7ac2 --- /dev/null +++ b/challenge-122/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +# +# Task 1: "Average of Stream +# +# You are given a stream of numbers, @N. +# +# Write a script to print the average of the stream at every point. +# +# Example +# +# Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) +# Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... +# +# 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. +# " +# +# My notes: sounds quite simple, just requires a running total. +# + +use strict; +use warnings; +use feature 'say'; +#use Data::Dumper; + +die "Usage: stream-average N1 N2...\n" if @ARGV == 0; +my @n = @ARGV; + +my $sum = 0; +my $n = 0; +foreach my $val (@n) +{ + $sum += $val; + $n++; + print "," if $n>1; + print $sum/$n; +} +say ""; diff --git a/challenge-122/duncan-c-white/perl/ch-2.pl b/challenge-122/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..d60decb34c --- /dev/null +++ b/challenge-122/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl +# +# Task 2: "Basketball Points +# +# You are given a score $S. +# +# You can win basketball points e.g. 1 point, 2 points and 3 points. +# +# Write a script to find out the different ways you can score $S. +# +# 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 +# " +# +# 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"? +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Getopt::Long; +use Data::Dumper; + +my $debug = 0; +die "Usage: basketball-points N\n" unless + GetOptions( "debug" => \$debug ) && @ARGV==1; +my $n = shift; +die "basketball-points: N ($n) > 0\n" if $n<1; + +bp( $n, "" ); + + +# +# bp( $n, $prefix ); +# Print out all basketball-point combinations of $n given +# a prefix (such as "1 1 2") $prefix. +# +fun bp( $n, $prefix ) +{ + if( $n >= 1 ) + { + say "$prefix $n" if $n == 1; + bp( $n-1, "$prefix 1" ) if $n > 1; + } + if( $n >= 2 ) + { + say "$prefix $n" if $n == 2; + bp( $n-2, "$prefix 2" ) if $n > 2; + } + if( $n >= 3 ) + { + say "$prefix $n" if $n == 3; + bp( $n-3, "$prefix 3" ) if $n > 3; + } +} |
