From 4fec17e56cbb6acfe3fd5e186e20196a2633a863 Mon Sep 17 00:00:00 2001 From: Rushil Shah Date: Fri, 24 Sep 2021 16:35:35 +0530 Subject: [PATCH 1/3] Added an optimized function is_palindrome_pythonic() Used list comprehension to store alphanumeric characters. Then compared the original and the reversed list and returned the result of the comparison. --- strings/is_palindrome.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/strings/is_palindrome.py b/strings/is_palindrome.py index 5090215..c063fb9 100644 --- a/strings/is_palindrome.py +++ b/strings/is_palindrome.py @@ -34,3 +34,15 @@ def is_palindrome(s): # Time Complexity : O(len(s)) # Space Complexity : O(1) + + +def is_palindrome_pythonic(s): + """ + :type s: str + :rtype: bool + """ + alphanumerics = [c.lower() for c in s if c.isalnum()] + return alphanumerics == alphanumerics[::-1] + +# Time Complexity : O(len(s)) +# Space Complexity : O(len(s)) From ccc83a99c3ff83b876dfa009eab1573839c9090f Mon Sep 17 00:00:00 2001 From: Rushil Shah Date: Fri, 24 Sep 2021 17:25:16 +0530 Subject: [PATCH 2/3] Added optimized function add_binary_pythonic() It uses inbuilt type conversions. Strings are converted to decimal representation, then the decimal numbers are added and the result is again converted to string. The required part of the resulting string is returned by the function. --- strings/add_binary.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/strings/add_binary.py b/strings/add_binary.py index b2bb523..fbdcd20 100644 --- a/strings/add_binary.py +++ b/strings/add_binary.py @@ -24,3 +24,10 @@ def add_binary(a, b): # c //= 2 for python3 c /= 2 return s + + +def add_binary_pythonic(a, b): + decimal_a = int('0b' + a, 2) + decimal_b = int('0b' + b, 2) + s = bin(decimal_a + decimal_b)[2:] + return s From c0b402766b1a83880d6e7a7d6ecb18866ff26296 Mon Sep 17 00:00:00 2001 From: Rushil Shah Date: Sat, 25 Sep 2021 13:55:29 +0530 Subject: [PATCH 3/3] Removed unnecessary semicolons at the line endings. --- strings/int_to_roman.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/strings/int_to_roman.py b/strings/int_to_roman.py index 00aa4bb..c553009 100644 --- a/strings/int_to_roman.py +++ b/strings/int_to_roman.py @@ -9,11 +9,11 @@ def int_to_roman(num): :type num: int :rtype: str """ - M = ["", "M", "MM", "MMM"]; - C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]; - X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]; - I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]; - return M[num // 1000] + C[(num % 1000) // 100] + X[(num % 100) // 10] + I[num % 10]; + M = ["", "M", "MM", "MMM"] + C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"] + X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"] + I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"] + return M[num // 1000] + C[(num % 1000) // 100] + X[(num % 100) // 10] + I[num % 10] print(int_to_roman(644))