From dd979819d8042c04464033dcff0c2853546e34c1 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 3 Nov 2021 15:22:21 +0000 Subject: Add Python solution to challenge 114 --- challenge-114/paulo-custodio/Makefile | 2 ++ challenge-114/paulo-custodio/python/ch-1.py | 31 ++++++++++++++++++++++++ challenge-114/paulo-custodio/python/ch-2.py | 37 +++++++++++++++++++++++++++++ challenge-114/paulo-custodio/test.pl | 4 ---- 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 challenge-114/paulo-custodio/Makefile create mode 100644 challenge-114/paulo-custodio/python/ch-1.py create mode 100644 challenge-114/paulo-custodio/python/ch-2.py delete mode 100755 challenge-114/paulo-custodio/test.pl 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'; -- cgit