diff options
| author | dcw <d.white@imperial.ac.uk> | 2021-07-18 22:31:32 +0100 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2021-07-18 22:31:32 +0100 |
| commit | ef5e0426fadb4f3528a1c936423be799527ee5ec (patch) | |
| tree | 28166aa3a40f204ca2d5e598f0fa2e946cbc1d5f | |
| parent | df4cf0dec3295559d5a1053c6a82081cebbedf50 (diff) | |
| download | perlweeklychallenge-club-ef5e0426fadb4f3528a1c936423be799527ee5ec.tar.gz perlweeklychallenge-club-ef5e0426fadb4f3528a1c936423be799527ee5ec.tar.bz2 perlweeklychallenge-club-ef5e0426fadb4f3528a1c936423be799527ee5ec.zip | |
only had a go at task 1 this time; not in the mood for TSP tonight
| -rw-r--r-- | challenge-121/duncan-c-white/README | 60 | ||||
| -rwxr-xr-x | challenge-121/duncan-c-white/perl/ch-1.pl | 46 |
2 files changed, 75 insertions, 31 deletions
diff --git a/challenge-121/duncan-c-white/README b/challenge-121/duncan-c-white/README index 55af529741..a67e73b42b 100644 --- a/challenge-121/duncan-c-white/README +++ b/challenge-121/duncan-c-white/README @@ -1,53 +1,51 @@ -Task 1: "Swap Odd/Even bits +Task 1: "Invert Bit -You are given a positive integer $N less than or equal to 255. +You are given integers 0 <= $m <= 255 and 1 <= $n <= 8. -Write a script to swap the odd positioned bit with even positioned bit -and print the decimal equivalent of the new binary representation. +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. Example - Input: $N = 101 - Output: 154 + Input: $m = 12, $n = 3 + Output: 8 - Binary representation of the given number is 01 10 01 01. - The new binary representation after the odd/even swap is 10 01 10 10. - The decimal equivalent of 10011010 is 154. +Binary representation of $m = 00001100 +Invert 3rd bit from the end = 00001000 +Decimal equivalent of 00001000 = 8 - Input: $N = 18 - Output: 33 + Input $m = 18, $n = 4 + Output: 26 - Binary representation of the given number is 00 01 00 10. - The new binary representation after the odd/even swap is 00 10 00 01. - The decimal equivalent of 100001 is 33. +Binary representation of $m = 00010010 +Invert 4th bit from the end = 00011010 +Decimal equivalent of 00011010 = 26 " My notes: trivial again. -Task 2: "Clock Angle +Task 2: "Travelling Salesman +Submitted by: Jorg Sommrey -You are given time $T in the format hh:mm. +You are given a NxN matrix containing the distances between N cities. -Write a script to find the smaller angle formed by the hands of an analog -clock at a given time. - -HINT: A analog clock is divided up into 12 sectors. One sector represents -30 degree (360/12 = 30). +Write a script to find a round trip of minimum length visiting all N +cities exactly once and returning to the start. Example - Input: $T = '03:10' - Output: 35 degree +Matrix: [0, 5, 2, 7] + [5, 0, 5, 3] + [3, 1, 0, 6] + [4, 5, 4, 0] - The distance between the 2 and the 3 on the clock is 30 degree. - For the 10 minutes i.e. 1/6 of an hour that have passed. - The hour hand has also moved 1/6 of the distance between the 3 and the 4, - which adds 5 degree (1/6 of 30). - The total measure of the angle is 35 degree. +Output: + length = 10 + tour = (0 2 1 3 0) - Input: $T = '04:00' - Output: 120 degree +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 " -My notes: sounds pretty easy. +My notes: gurk! TSP is hard. I'm tired and I'm not doing that. diff --git a/challenge-121/duncan-c-white/perl/ch-1.pl b/challenge-121/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..657f60ad3b --- /dev/null +++ b/challenge-121/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl +# +# Task 1: "Invert Bit +# +# You are given integers 0 <= $m <= 255 and 1 <= $n <= 8. +# +# 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. +# +# Example +# +# Input: $m = 12, $n = 3 +# Output: 8 +# +# 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 +# " +# +# My notes: trivial again. +# + +use strict; +use warnings; +use feature 'say'; +#use Data::Dumper; + +my $debug = 0; +die "Usage: invert-bit M N\n" unless @ARGV == 2; +my( $m, $n ) = @ARGV; +die "invert-bit: N ($n) must be 0..255\n" unless $n>=0 && $n<=255; +die "invert-bit: M ($m) must be 1..8\n" unless $n>=1 && $n<=8; + +#printf "m=%08b\n", $m; +my $mask = (1 << ($n-1)); +#say "mask=$mask"; +$m = ($m & $mask ) ? ($m & ~$mask) : ($m | $mask); +#printf "m=%08b\n", $m; +say $m; |
