diff options
| author | David Schwartz <dms061@bucknell.edu> | 2021-05-30 23:35:14 -0400 |
|---|---|---|
| committer | David Schwartz <dms061@bucknell.edu> | 2021-05-30 23:35:14 -0400 |
| commit | f7817d4f601fb7f039b610162ce4cdf7a3c2297a (patch) | |
| tree | 7811179d2b5e47797813a5387589b5b35489e735 /challenge-114 | |
| parent | dc32df28d7c1a41694b16b97325407751bdb8a8b (diff) | |
| parent | aa519f5a1be2a316fe104086fc1eb52e3a819dcc (diff) | |
| download | perlweeklychallenge-club-f7817d4f601fb7f039b610162ce4cdf7a3c2297a.tar.gz perlweeklychallenge-club-f7817d4f601fb7f039b610162ce4cdf7a3c2297a.tar.bz2 perlweeklychallenge-club-f7817d4f601fb7f039b610162ce4cdf7a3c2297a.zip | |
Merge remote-tracking branch 'upstream/master' into challenge114
Diffstat (limited to 'challenge-114')
100 files changed, 5677 insertions, 697 deletions
diff --git a/challenge-114/aaronreidsmith/blog.txt b/challenge-114/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..8bb2dc2414 --- /dev/null +++ b/challenge-114/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-114/ diff --git a/challenge-114/aaronreidsmith/raku/ch-1.raku b/challenge-114/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..5e67985a5f --- /dev/null +++ b/challenge-114/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +sub challenge(Int $N) returns Int { + ($N^..Inf).first(-> $num { $num == $num.flip }, :v); +} + +multi sub MAIN(Int $N) { + say challenge($N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (1234, 1331), + (999, 1001) + ); + + for @tests -> ($N, $expected) { + is(challenge($N), $expected); + } + + done-testing; +} diff --git a/challenge-114/aaronreidsmith/raku/ch-2.raku b/challenge-114/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..20d191d790 --- /dev/null +++ b/challenge-114/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku + +sub bits(Int $base-ten) returns Int { + $base-ten.base(2).comb.grep(* eq '1').elems; +} + +sub challenge(Int $N where $N > 0) returns Int { + my $bits = bits($N); + ($N^..Inf).first(-> $num { bits($num) == $bits }, :v); +} + +multi sub MAIN(Int $N) { + say challenge($N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (1, 2), + (3, 5), + (12, 17) + ); + + for @tests -> ($N, $expected) { + is(challenge($N), $expected); + } + + done-testing; +} diff --git a/challenge-114/abigail/README.md b/challenge-114/abigail/README.md index a026cb8bf7..7548aee37d 100644 --- a/challenge-114/abigail/README.md +++ b/challenge-114/abigail/README.md @@ -1,76 +1,58 @@ # Solutions by Abigail -## [Represent Integer](https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK1) +## [Next Palindrome Number](https://perlweeklychallenge.org/blog/perl-weekly-challenge-114/#TASK1) -> 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 ~~~~ ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) -* [Lua](lua/ch-1.lua) -* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) -* [Python](python/ch-1.py) -* [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 113: Represent -Integer](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html) +[Next Palindrome Number](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-114-1.html) -## [Recreate Binary Tree](https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2) +## [Higher Integer Set Bits](https://perlweeklychallenge.org/blog/perl-weekly-challenge-114/#TASK2) -> 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 +### Examples ~~~~ - 1 - / \ - 2 3 - / / \ - 4 5 6 - \ - 7 +Input: $N = 3 +Output: 5 ~~~~ -#### Output + +Binary representation of `$N` 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`. + ~~~~ - 27 - / \ - 26 25 - / / \ - 24 23 22 - \ - 21 +Input: $N = 12 +Output: 17 ~~~~ +Binary representation of `$N` is `1100`. There are two `1` bits. So the next +higher integer is `17` having the same number of `1` bits i.e. `10001`. ### Solutions -* [AWK](awk/ch-2.awk) +* [GNU AWK](awk/ch-2.gawk) * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) -* [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) -* [Ruby](ruby/ch-2.rb) ### Blog -[Perl Weekly Challenge 113: Recreate Binary -Tree](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-2.html) +[Higher Integet Set Bits](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-114-2.html) diff --git a/challenge-114/abigail/awk/ch-1.awk b/challenge-114/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..3d1822ea29 --- /dev/null +++ b/challenge-114/abigail/awk/ch-1.awk @@ -0,0 +1,44 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +function reverse (word, out, i) { + for (i = length (word); i > 0; i --) { + out = out substr (word, i, 1) + } + return out +} + + + +/^9+$/ { + print $1 + 2 + next +} + +length ($1) == 1 { + print $1 + 1 + next +} + +{ + part1 = substr ($1, 1, int (length ($1) / 2)) + part2 = substr ($1, 1 + int (length ($1) / 2), length ($1) % 2) + part3 = substr ($1, 1 + int (length ($1) / 2) + length ($1) % 2) + + if (reverse(part1) <= part3) { + part1 = (part1 part2) + 1 + if (length (part2)) { + part2 = substr (part1, length (part1), 1) + part1 = substr (part1, 1, length (part1) - 1) + } + } + + print part1 part2 reverse(part1) +} diff --git |
