# Sepember 30th 2025
# 'valid parentheses'
# self-commenting version:
class Solution:
def isValid(self, s: str) -> bool:
# {key: value}
matching_paren = {')':'(',
'}':'{',
']':'['}
open_parens = matching_paren.values()
close_parens = matching_paren.keys()
unmatched_open_parens = [] # a stack, which will grow and shrink.
for symbol in s:
if symbol in open_parens:
unmatched_open_parens.append(symbol) # push to the stack.
elif symbol in close_parens:
# Check if there's a matching open paren on top of the stack.
if len(unmatched_open_parens) == 0:
# this means our close paren is extra (there is no matching open paren)
return False
top_of_stack = unmatched_open_parens.pop() # what we have.
if top_of_stack != matching_paren[symbol]: # is it what we need?
return False
else: # it's not in the matching_paren dictionary.
return False
# We iterated thru the whole string without finding a problem.
if unmatched_open_parens: # this means we ended with unmatched open parens.
return False
else:
return True
# clean comments version:
# April 29th 2025
# 'can place flowers'
# Fast:
class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
size=len(flowerbed)
if n==0:
return True
if (size<3 and 1 in flowerbed) :
return False
if (size<3 and 1 not in flowerbed ) and n<2:
return True
nbrPlant=0
if flowerbed[size-1]==0 and flowerbed[size-2]==0:
flowerbed[size-1]=1
nbrPlant = nbrPlant+1
if flowerbed[0]==0 and flowerbed[1]==0:
flowerbed[0]=1
nbrPlant = nbrPlant+1
if size>2 and n>0 :
for i in range(1, size-2):
if flowerbed[i]==0 and flowerbed[i-1]==0 and flowerbed[i+1]==0:
flowerbed[i]=1
nbrPlant = nbrPlant+1
if nbrPlant>=n:
return True
else:
return False
# Slow (with padding to eliminate sliding window edge cases):
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
# Add padding:
flowerbed = [0] + flowerbed + [0]
num_remain = n
size = len(flowerbed);
# Sliding window (skip first and last):
for iCenter in range(1, size -1):
window = [flowerbed[iCenter -1], flowerbed[iCenter], flowerbed[iCenter +1]]
# print(window)
if window == [0, 0, 0]:
flowerbed[iCenter] = 1; # plant it.
num_remain -= 1; # decrement the number remaining to be planted.
# print(flowerbed)
if num_remain < 1: return True
return False
# April 8th 2025
# 'course scheduler'
# April 3rd 2025
# 'happy number'
# helper / subroutine:
def getNext(n) -> int:
sum = 0
for char in str(n):
digit = int(char)
sum += digit * digit
return sum
# list comprehension version:
def getNext(n) -> int:
return sum(int(digit) ** 2 for digit in str(n))
class Solution:
def isHappy(self, n: int) -> bool:
seenVals = set() # already seen values.
while n not in seenVals:
# print(f"{n} not seen before.")
seenVals.add(n)
n = getNext(n)
if n == 1:
return True
else:
return False
# November 3rd Code
# 'remove element'
# Solution 1 - while loop:
class Solution(object):
def removeElement(self, nums, val):
i = 0
while i < len(nums):
if nums[i]==val:
nums.remove(val)
else:
i += 1
return(len(nums))
# Solution 2 - go backwards:
class Solution(object):
def removeElement(self, nums, val):
for i in range(len(nums)-1, 0, -1):
if nums[i]==val:
nums.remove(val)
return(len(nums))
# Ocober 17th Code
# 'two sum'
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
res=[None]*2;
x=0;
found=False;
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]+nums[j]==target:
res[0]=i;
res[1]=j;
found=True;
break;
if found:
break;
return res
# Ocober 17th Code
# 'palindrome'
class Solution(object):
def isPalindrome(self, s:string):
cleaned_string = ''.join(s.split())
cleaned_string = remove_non_alphanumeric(cleaned_string)
r = True
i = 0
j = len(s_list)
while i<j && r == True:
if s_list[i] != s_list[j]:
r = False
i+= 1
j-= 1
return r
import re
def remove_non_alphanumeric(input_string):
return re.sub(r'[^a-zA-Z0-9]', '', input_string)