Skip to content

Instantly share code, notes, and snippets.

@sjsakib
Created November 27, 2024 17:01
Show Gist options
  • Save sjsakib/e4c8c7ab6a1a09f1e465705606618f46 to your computer and use it in GitHub Desktop.
Save sjsakib/e4c8c7ab6a1a09f1e465705606618f46 to your computer and use it in GitHub Desktop.
# Problem 1
def reverse_integer(n):
LOWER = -(2**31)
UPPER = (2**31)-1
n_str = str(n)
n_digit_list = list(n_str)
n_digit_list.reverse()
reverse_str = ''.join(n_digit_list)
if reverse_str.endswith('-'):
result = -1 * int(reverse_str[:-1])
else:
result = int(reverse_str)
if result < LOWER or result > UPPER:
return 0
return result
# Problem 2
def is_valid(input):
closing_map = {
'(': ')',
'{': '}',
'[': ']'
}
stack = []
for current in input:
if current in '({[':
stack.append(current)
else:
if len(stack) == 0:
return False
last = stack.pop()
if closing_map[last] != current:
return False
return True
# Problem 3
def max_sub_array(array):
max = -1e20
for i in range(0, len(array)+1):
for j in range(i, len(array)+1):
s = sum(array[i:j])
if s > max:
max = s
return max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment