Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions strings/add_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions strings/int_to_roman.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
12 changes: 12 additions & 0 deletions strings/is_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))