diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-29 21:43:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-29 21:43:16 +0100 |
| commit | 2ed19a161adf10bc6c5f0d6361baebca5e58aa8f (patch) | |
| tree | cdb06b74bd9831dec14fea9c8999151185fdad86 | |
| parent | 70eca18f6227332848f7d8d33a7ca84ab4b7b361 (diff) | |
| parent | 7df0a85db7af75a6eb8d21c28ba03c7a1f72421d (diff) | |
| download | perlweeklychallenge-club-2ed19a161adf10bc6c5f0d6361baebca5e58aa8f.tar.gz perlweeklychallenge-club-2ed19a161adf10bc6c5f0d6361baebca5e58aa8f.tar.bz2 perlweeklychallenge-club-2ed19a161adf10bc6c5f0d6361baebca5e58aa8f.zip | |
Merge pull request #4160 from dcw803/master
imported my solutions to this week's challenge..
| -rw-r--r-- | challenge-114/duncan-c-white/README | 62 | ||||
| -rw-r--r-- | challenge-114/duncan-c-white/perl/bintree.eg | 8 | ||||
| -rwxr-xr-x | challenge-114/duncan-c-white/perl/ch-1.pl | 49 | ||||
| -rwxr-xr-x | challenge-114/duncan-c-white/perl/ch-2.pl | 66 |
4 files changed, 141 insertions, 44 deletions
diff --git a/challenge-114/duncan-c-white/README b/challenge-114/duncan-c-white/README index 6a326449a7..de73e12315 100644 --- a/challenge-114/duncan-c-white/README +++ b/challenge-114/duncan-c-white/README @@ -1,52 +1,42 @@ -Task 1: "Represent Integer +Task 1: "Next Palindrome Number -You are given a positive integer $N and a digit $D. +You are given a positive integer $N. -Write a script to check if $N can be represented as a sum of positive -integers having $D at least once. If check passes print 1 otherwise 0. +Write a script to find out the next Palindrome Number higher than the given integer $N. Example -Input: $N = 25, $D = 7 -Output: 0 as there are 2 numbers between 1 and 25 having the digit 7 -i.e. 7 and 17. If we add up both we don't get 25. + Input: $N = 1234 + Output: 1331 -Input: $N = 24, $D = 7 -Output: 1 + Input: $N = 999 + Output: 1001 " -My notes: sounds very simple. sum ( grep contains 7 ) +My notes: sounds very simple. Generate and test for palindromic-ness. -Task 2: "Recreate Binary Tree +Task 2: "Higher Integer Set Bits -You are given a Binary Tree. +You are given a positive integer $N. -Write a script to replace each node of the tree with the sum of all the remaining nodes. +Write a script to find the next higher integer having the same number +of 1 bits in binary representation as $N. Example -Input Binary Tree - - 1 - / \ - 2 3 - / / \ - 4 5 6 - \ - 7 - -Output Binary Tree - - 27 - / \ - 26 25 - / / \ - 24 23 22 - \ - 21 + +Input: $N = 3 +Output: 5 + +Binary representation of 3 is 011. There are two 1 bits. So the next +higher integer is 5 having the same the number of 1 bits i.e. 101. + +Input: $N = 12 +Output: 17 + +Binary representation of 12 is 1100. There are two 1 bits. So the next +higher integer is 17 having the same number of 1 bits i.e. 10001. " -My notes: so each node's value becomes sum(allnodes) - that node's value -Very tempting to view this as an ascii art -> ascii art problem, last time -we did a similar tree question, I spent a lot of time building the tree -structure, and getting nice layout from print_tree.. +My notes: also sounds pretty simple. Generate and test for "having B +bits set in the binary representation". diff --git a/challenge-114/duncan-c-white/perl/bintree.eg b/challenge-114/duncan-c-white/perl/bintree.eg deleted file mode 100644 index 9af8a8a980..0000000000 --- a/challenge-114/duncan-c-white/perl/bintree.eg +++ /dev/null @@ -1,8 +0,0 @@ - - 1 - / \ - 2 3 - / / \ - 4 5 6 - \ - 7 diff --git a/challenge-114/duncan-c-white/perl/ch-1.pl b/challenge-114/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..6f9b316a78 --- /dev/null +++ b/challenge-114/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +# +# Task 1: "Next Palindrome Number +# +# You are given a positive integer $N. +# +# Write a script to find out the next Palindrome Number higher than the given integer $N. +# +# Example +# +# Input: $N = 1234 +# Output: 1331 +# +# Input: $N = 999 +# Output: 1001 +# " +# +# My notes: sounds very simple. Generate and test for palindromic-ness. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +#use Data::Dumper; + +# +# my $ispal = ispalindromic( $n ); +# Return 1 iff $n is palindromic (in decimal of course) +# +fun ispalindromic( $n ) +{ + my $rev = join( '', reverse split( //, $n ) ); + return $n==$rev ? 1 : 0; +} + + + + +die "Usage: next-palindromic-number N\n" unless @ARGV==1; +my $n = shift; + +do +{ + $n++; +} +while( ! ispalindromic($n) ); + +say $n; diff --git a/challenge-114/duncan-c-white/perl/ch-2.pl b/challenge-114/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..9a836cc748 --- /dev/null +++ b/challenge-114/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl +# +# Task 2: "Higher Integer Set Bits +# +# You are given a positive integer $N. +# +# Write a script to find the next higher integer having the same number +# of 1 bits in binary representation as $N. +# +# Example +# +# Input: $N = 3 +# Output: 5 +# +# Binary representation of 3 is 011. There are two 1 bits. So the next +# higher integer is 5 having the same the number of 1 bits i.e. 101. +# +# Input: $N = 12 +# Output: 17 +# +# Binary representation of 12 is 1100. There are two 1 bits. So the next +# higher integer is 17 having the same number of 1 bits i.e. 10001. +# " +# +# My notes: also sounds pretty simple. Generate and test for "having B +# bits set in the binary representation of numbers > N, where B==nbits(N)". +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +#use Data::Dumper; + +# +# my $nbits = nbits( $n ); +# Count up and return the number of 1 bits in the binary representation of $n. +# +fun nbits( $n ) +{ + my $result = 0; + while( $n > 0 ) + { + $result++ if $n%2==1; + $n >>= 1; + } + return $result; +} + + +die "Usage: next-same-no-1-bits N\n" unless @ARGV==1; +my $n = shift; + +my $nbits = nbits( $n ); + +my $i=$n; +do { + $i++; +} while( nbits($i) != $nbits ); + +say "Input: \$N = $n"; +say "Output: $i"; + +printf "\nBinary representation of $n is %b. There are %d 1 bits. So the next\n", + $n, $nbits; +printf "higher integer is $i having the same number of 1 bits, i.e. %b\n", $i; |
