diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-03 15:22:21 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-03 15:22:21 +0000 |
| commit | dd979819d8042c04464033dcff0c2853546e34c1 (patch) | |
| tree | 22aba73e60e78d2f2c154a6c575bb9efc1b96a17 /challenge-114 | |
| parent | 0e3775f53fc56ed1528f508e80450ef85884d6ff (diff) | |
| download | perlweeklychallenge-club-dd979819d8042c04464033dcff0c2853546e34c1.tar.gz perlweeklychallenge-club-dd979819d8042c04464033dcff0c2853546e34c1.tar.bz2 perlweeklychallenge-club-dd979819d8042c04464033dcff0c2853546e34c1.zip | |
Add Python solution to challenge 114
Diffstat (limited to 'challenge-114')
| -rw-r--r-- | challenge-114/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-114/paulo-custodio/python/ch-1.py | 31 | ||||
| -rw-r--r-- | challenge-114/paulo-custodio/python/ch-2.py | 37 | ||||
| -rwxr-xr-x | challenge-114/paulo-custodio/test.pl | 4 |
4 files changed, 70 insertions, 4 deletions
diff --git a/challenge-114/paulo-custodio/Makefile b/challenge-114/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-114/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-114/paulo-custodio/python/ch-1.py b/challenge-114/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..e9e2967b0c --- /dev/null +++ b/challenge-114/paulo-custodio/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# Challenge 114 +# +# TASK #1 - Next Palindrome Number +# Submitted by: Mohammad S Anwar +# 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 + +import sys + +def is_palindrome(n): + rev_n = int(str(n)[::-1]) + return n==rev_n + +def next_palindrome(n): + while True: + n += 1 + if is_palindrome(n): + return n + +print(next_palindrome(int(sys.argv[1]))) diff --git a/challenge-114/paulo-custodio/python/ch-2.py b/challenge-114/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..09f6731bbc --- /dev/null +++ b/challenge-114/paulo-custodio/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# Challenge 114 +# +# TASK #2 - Higher Integer Set Bits +# Submitted by: Mohammad S Anwar +# 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. + +import sys + +def num_1bits(n): + return sum([int(x) for x in "{:b}".format(n)]) + +def next_same_1bits(n): + num1s = num_1bits(n) + while True: + n += 1 + if num_1bits(n)==num1s: + return n + +print(next_same_1bits(int(sys.argv[1]))) diff --git a/challenge-114/paulo-custodio/test.pl b/challenge-114/paulo-custodio/test.pl deleted file mode 100755 index ba6c37260b..0000000000 --- a/challenge-114/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; |
