aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-05-24 18:45:25 +0800
committer冯昶 <seaker@qq.com>2021-05-24 18:45:25 +0800
commitb6e7efe041df29ba2302cca693e98ed6792842ab (patch)
treed1ba52b5924fa49a14574a5cb611534442f0725f
parentb876dd5b111bf1c267b2fb58cd64e6b655128e5c (diff)
downloadperlweeklychallenge-club-b6e7efe041df29ba2302cca693e98ed6792842ab.tar.gz
perlweeklychallenge-club-b6e7efe041df29ba2302cca693e98ed6792842ab.tar.bz2
perlweeklychallenge-club-b6e7efe041df29ba2302cca693e98ed6792842ab.zip
challenge 114, raku solutions
-rwxr-xr-xchallenge-114/feng-chang/raku/ch-1.raku22
-rwxr-xr-xchallenge-114/feng-chang/raku/ch-2.raku27
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-114/feng-chang/raku/ch-1.raku b/challenge-114/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..1dac259d6a
--- /dev/null
+++ b/challenge-114/feng-chang/raku/ch-1.raku
@@ -0,0 +1,22 @@
+#!/bin/env raku
+
+#`[
+ Perl weekly chanllege 114, 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 method is-palindrome(UInt:D $n: --> Bool:D) { $n.flip eq $n }
+
+sub MAIN(UInt:D $N) {
+ put ($N^..*).grep(*.&is-palindrome)[0];
+}
diff --git a/challenge-114/feng-chang/raku/ch-2.raku b/challenge-114/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..a082bfb69b
--- /dev/null
+++ b/challenge-114/feng-chang/raku/ch-2.raku
@@ -0,0 +1,27 @@
+#!/bin/env raku
+
+#`[
+ Perl weekly chanllege 114, 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 $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.
+
+ 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.
+#]
+
+my method bits-number(UInt:D $n: --> UInt:D) { $n.base(2).comb.grep(1).elems }
+my method has-bits(UInt:D $n: UInt:D $bits --> Bool:D) { $n.&bits-number == $bits }
+
+sub MAIN(UInt:D $N) {
+ put ($N ^.. *).grep(*.&has-bits($N.&bits-number))[0];
+}