diff options
| author | dcw <d.white@imperial.ac.uk> | 2021-07-04 22:05:16 +0100 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2021-07-04 22:05:16 +0100 |
| commit | ee2fec8b172a973abb8636db7f139703ee1c51de (patch) | |
| tree | 280795ea7c7ca4f4325125dc95d276afca51418f /challenge-119 | |
| parent | 8cab52b28709e2e730a973ed9d27068d01651a8e (diff) | |
| download | perlweeklychallenge-club-ee2fec8b172a973abb8636db7f139703ee1c51de.tar.gz perlweeklychallenge-club-ee2fec8b172a973abb8636db7f139703ee1c51de.tar.bz2 perlweeklychallenge-club-ee2fec8b172a973abb8636db7f139703ee1c51de.zip | |
imported my solutions to challenge-119's two tasks - both thankfully easy after last week's Knight's adventure question..
Diffstat (limited to 'challenge-119')
| -rw-r--r-- | challenge-119/duncan-c-white/README | 70 | ||||
| -rwxr-xr-x | challenge-119/duncan-c-white/perl/ch-1.pl | 46 | ||||
| -rwxr-xr-x | challenge-119/duncan-c-white/perl/ch-2.pl | 76 |
3 files changed, 157 insertions, 35 deletions
diff --git a/challenge-119/duncan-c-white/README b/challenge-119/duncan-c-white/README index 5d19dbe786..fde1701c16 100644 --- a/challenge-119/duncan-c-white/README +++ b/challenge-119/duncan-c-white/README @@ -1,54 +1,54 @@ -Task 1: "Binary Palindrome +Task 1: "Swap Nibbles You are given a positive integer $N. -Write a script to find out if the binary representation of the given -integer is Palindrome. Print 1 if it is otherwise 0. +Write a script to swap the two nibbles of the binary representation +of the given number and print the decimal number of the new binary +representation. + +A nibble is a four-bit aggregation, or half an octet. + +To keep the task simple, we only allow integer less than or equal to 255. Example - Input: $N = 5 - Output: 1 as binary representation of 5 is 101 which is Palindrome. + Input: $N = 101 + Output: 86 + + Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101). + The swapped nibbles would be (0101)(0110) same as decimal 86. - Input: $N = 4 - Output: 0 as binary representation of 4 is 100 which is NOT Palindrome. + Input: $N = 18 + Output: 33 + + Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010). + The swapped nibbles would be (0010)(0001) same as decimal 33. " -My notes: seems pretty easy. +My notes: trivial. -Task 2: "Adventure of Knight -Submitted by: Cheok-Yin Fung +Task 2: "Sequence without 1-on-1 -A knight is restricted to move on an 8x8 chessboard. The knight is -denoted by N and its way of movement is the same as what it is defined in -Chess. * represents an empty square. x represents a square with treasure. +Write a script to generate sequence starting at 1. Consider the increasing +sequence of integers which contain only 1's, 2's and 3's, and +do not have any doublets of 1's like below. Please accept a positive +integer $N and print the $Nth term in the generated sequence. -The Knight's movement is unique. It may move two squares vertically -and one square horizontally, or two squares horizontally and one square -vertically (with both forming the shape of an L). + 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ... -There are 6 squares with treasures. +Example -Write a script to find the path such that Knight can capture all -treasures. The Knight starts from the top-left square. + Input: $N = 5 + Output: 13 - a b c d e f g h - 8 N * * * * * * * 8 - 7 * * * * * * * * 7 - 6 * * * * x * * * 6 - 5 * * * * * * * * 5 - 4 * * x * * * * * 4 - 3 * x * * * * * * 3 - 2 x x * * * * * * 2 - 1 * x * * * * * * 1 - a b c d e f g h + Input: $N = 10 + Output: 32 -BONUS: If you believe that your algorithm can output one of the shortest possible path. + Input: $N = 60 + Output: 2223 " -My notes: looks reasonably straight forward, highly recursive. Obvious way -to find a shortest possible path is to generate all paths of length L before -extending each path of length L to paths of length L+1. NB: Why not allow -any number of treasures, not fixed at 6? Got it working, although it takes -forever to solve larger problems. +My notes: hmm, depends on what we mean by "doublet", as in "no doublets of 1". +I'm choosing to assume that it means "no two sequential 1s". should be +pretty easy. diff --git a/challenge-119/duncan-c-white/perl/ch-1.pl b/challenge-119/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..d6457719f6 --- /dev/null +++ b/challenge-119/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl +# +# Task 1: "Swap Nibbles +# +# You are given a positive integer $N. +# +# Write a script to swap the two nibbles of the binary representation +# of the given number and print the decimal number of the new binary +# representation. +# +# A nibble is a four-bit aggregation, or half an octet. +# +# To keep the task simple, we only allow integer less than or equal to 255. +# +# Example +# +# Input: $N = 101 +# Output: 86 +# +# Binary representation of decimal 101 is 1100101 or, as 2 nibbles, +# (0110)(0101). The swapped nibbles would be (0101)(0110) - decimal 86. +# +# Input: $N = 18 +# Output: 33 +# +# Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010). +# The swapped nibbles would be (0010)(0001) same as decimal 33. +# " +# +# My notes: trivial. +# + +use strict; +use warnings; +use feature 'say'; +#use Data::Dumper; + +my $debug = 0; +die "Usage: swap-nibbles N\n" unless @ARGV == 1; +my $n = shift; +die "swap-nibbles: N ($n) must be 0..255\n" unless $n>=0 && $n<=255; + +my $l = $n & 15; +my $h = ($n>>4) & 15; +my $swap = $l<<4 | $h; +say $swap; diff --git a/challenge-119/duncan-c-white/perl/ch-2.pl b/challenge-119/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..79cf72841a --- /dev/null +++ b/challenge-119/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl +# +# Task 2: "Sequence without 1-on-1 +# +# Write a script to generate sequence starting at 1. Consider the increasing +# sequence of integers which contain only 1's, 2's and 3's, and +# do not have any doublets of 1's like below. Please accept a positive +# integer $N and print the $Nth term in the generated sequence. +# +# 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ... +# +# Example +# +# Input: $N = 5 +# Output: 13 +# +# Input: $N = 10 +# Output: 32 +# +# Input: $N = 60 +# Output: 2223 +# " +# +# My notes: hmm, depends on what we mean by "doublet", as in "no doublets of 1". +# I'm choosing to assume that it means "no two sequential 1s". should be +# pretty easy - let's do it by generate and test. +# + +use strict; +use warnings; +use feature 'say'; +use List::Util qw(sum); +use Function::Parameters; +use Getopt::Long; +use Data::Dumper; + +my $debug = 0; +die "Usage: seq-no-11 N\n" unless + GetOptions( "debug" => \$debug ) && @ARGV==1; +my $n = shift; + + +# +# my $isterm = isterm( $x ); +# Test whether $x is a term in our sequence. +# Return 1 iff it is; 0 otherwise. +# +fun isterm( $x ) +{ + return 0 unless $x =~ /^[123]+$/; # made up of 1,2 and 3s + return 0 if $x =~ /11/; # no sequence of 2 or more 1s + return 1; +} + + +# +# my $nth = nthterm( $n ); +# Generate and return the $n th term in the above sequence. +# Let's just do generate and test. +# +fun nthterm( $n ) +{ + my $found = 0; + my $i; + for( $i=0; $found < $n; $i++ ) + { + next unless isterm($i); + $found++; + say "debug: term $found is $i" if $debug; + } + return $i-1; +} + + +my $nth = nthterm( $n ); +say $nth; |
