aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-27 18:33:12 +0100
committerGitHub <noreply@github.com>2021-06-27 18:33:12 +0100
commita134233fa6c2f1dfe7dd81dfc1714ce2ada86d77 (patch)
treeb47e1d37e5432df8a6afc1bb1b6c6e3712183157
parentd7f6dfe1b42963adb0e9a94451afad8b8f64dc11 (diff)
parent871927d364d8a4224813205a93794e7dafc27cad (diff)
downloadperlweeklychallenge-club-a134233fa6c2f1dfe7dd81dfc1714ce2ada86d77.tar.gz
perlweeklychallenge-club-a134233fa6c2f1dfe7dd81dfc1714ce2ada86d77.tar.bz2
perlweeklychallenge-club-a134233fa6c2f1dfe7dd81dfc1714ce2ada86d77.zip
Merge pull request #4353 from Cris-HD/branch-for-challenge-118
Added challenge 118 solution
-rwxr-xr-xchallenge-118/cristian-heredia/perl/ch_1.pl37
-rwxr-xr-xchallenge-118/cristian-heredia/python/ch_1.py28
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-118/cristian-heredia/perl/ch_1.pl b/challenge-118/cristian-heredia/perl/ch_1.pl
new file mode 100755
index 0000000000..68c332e7d3
--- /dev/null
+++ b/challenge-118/cristian-heredia/perl/ch_1.pl
@@ -0,0 +1,37 @@
+=begin
+ TASK #1 › Binary Palindrome
+ Submitted by: Mohammad S Anwar
+ 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.
+
+ Example
+ Input: $N = 5
+ Output: 1 as binary representation of 5 is 101 which is Palindrome.
+
+ Input: $N = 4
+ Output: 0 as binary representation of 4 is 100 which is NOT Palindrome.
+
+
+=end
+=cut
+
+use warnings;
+use strict;
+use Data::Dumper;
+
+my $N = 5;
+
+
+my @bits = split(//, sprintf ("%b", $N));
+my $j = @bits - 1;
+
+foreach (my $i=0; $i<@bits; $i++){
+ if ($bits[$i] != $bits[$j]){
+ print "Output: 0\n";
+ exit
+ }
+ $j -= 1;
+}
+
+print "Output: 1\n"; \ No newline at end of file
diff --git a/challenge-118/cristian-heredia/python/ch_1.py b/challenge-118/cristian-heredia/python/ch_1.py
new file mode 100755
index 0000000000..118ba99782
--- /dev/null
+++ b/challenge-118/cristian-heredia/python/ch_1.py
@@ -0,0 +1,28 @@
+'''
+ TASK #1 › Binary Palindrome
+ Submitted by: Mohammad S Anwar
+ 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.
+
+ Example
+ Input: $N = 5
+ Output: 1 as binary representation of 5 is 101 which is Palindrome.
+
+ Input: $N = 4
+ Output: 0 as binary representation of 4 is 100 which is NOT Palindrome.
+
+
+'''
+
+N = 5
+
+bits = f'{N:b}'
+j = -1
+
+for num in bits:
+ if num != bits[j]:
+ print("Output: 0")
+ exit()
+ j -= 1
+print("Output: 1")