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 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)) 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))