sauravmaximus
BAN USERThe list is already sorted so we just need two pointers, initially they both start from the first element in the array. If at any point of time we meet the criteria where abs(arr[r] - arr[l]) == K, then we add that pair of numbers (arr[l], arr[r]) to the list of results. If the absolute difference is less than K then we update the r pointer, otherwise the l pointer. We use the builtin set DS to store unique pairs
def findAllPairs(arr, K):
if arr == None or len(arr) <= 1:
return 0
l = r = 0
pairs = set()
while r < len(arr):
if abs(arr[r] - arr[l]) == K:
l = l + 1
r = r + 1
pairs.add((arr[l], arr[r]))
elif abs(arr[r] - arr[l] < K):
r = r + 1
else:
l = l + 1
return list(pairs)
def commonCharacterCounter(l):
n = len(l)
if n == 0 or n == None:
return None
charDict = {}
for i in range(97, 123):
charDict[chr(i)] = False
for s in l:
for c in s:
if charDict.get(c) == None:
continue
if charDict.get(c) == True:
continue
if charDict.get(c) == False:
charDict[c] = True
for k in charDict.keys():
if charDict[k] == False:
del charDict[k]
else:
charDict[k] = False
return len(charDict.keys())
class SetOperator:
def __init__(self, order="asc"):
self.order = order
def union(self, a1, a2):
if a1 == None or a2 == None:
return None
n1 = len(a1); n2 = len(a2)
i = 0; j = 0
result = []
while (i < n1 and j < n2):
if a1[i] < a2[j]:
result.append(a1[i])
i = i + 1
elif a1[i] > a2[j]:
result.append(a2[j])
j = j + 1
else:
result.append(a1[i])
i = i + 1
j = j + 1
if i == n1:
while j < n2:
result.append(a2[j])
j = j + 1
if j == n2:
while i < n1:
result.append(a1[i])
i = i + 1
return result
def intersection(self, a1, a2):
if a1 == None or a2 == None:
return None
n1 = len(a1); n2 = len(a2)
i = 0; j = 0
result = []
while (i < n1 and j < n2):
if a1[i] < a2[j]:
i = i + 1
elif a1[i] > a2[j]:
j = j + 1
else:
result.append(a1[i])
i = i + 1
j = j + 1
return result
def removeDuplicatesFromArray(arr):
if arr == None:
return 0
n = len(arr)
if n == 1:
return n
l = r = 0;
dict = {}
for el in arr:
if dict.get(el) == None:
dict[el] = True
arr[r] = arr[l]
r = r + 1
l = l + 1
arr = arr[:r]
return r
Use a queue to store a tuple of values ( treenode, level). Start with initial sum of 0 for the root. Add the tuple (rootnode, 0) to the queue initially. Loop till the queue is empty. At each iteration check if the level of the removed node matches the current level. If it matches, then add node.data to the sum. If level is not the same, add the sum till now to the results list, reset sum to the current node.data and continue.
def findLevelOrderSumBST(root):
if root == None:
return root
results = []
level = 0; levelsum = 0
q = Queue()
q.put((root, level))
while not q.empty():
node, currlevel = q.get()
leftchild = node.left
rightchild = node.right
if leftchild != None:
q.put((leftchild, currlevel + 1))
if rightchild != None:
q.put((rightchild, currlevel + 1))
if currlevel == level:
levelsum = levelsum + node.data
else:
results.append(levelsum)
levelsum = node.data
level = currlevel
results.append(levelsum)
return results
def findSquaresInArray(a1, a2):
n1 = len(a1)
n2 = len(a2)
if n2 == 0 or n1 == 0:
return None
hashmap = {}
results = []
for el in a1:
hashmap[el] = True
for el in a2:
root = pow(el, 0.5)
int_root = int(root)
if root != int_root:
continue
pos_root = hashmap.get(int_root)
neg_root = hashmap.get(-1*int_root)
if pos_root == True:
results.append(el)
elif neg_root == True:
results.append(el)
else:
continue
return results
def removeDuplicatesInPlace(s):
##Strings are immutable so we must use a list
l = [c for c in s]
r = i = 0
dict = {}
for el in l:
if dict.get(el) is None:
dict[el] = True
l[r] = l[i]
r = r + 1
i = i + 1
return "".join(l[:r])
if __name__ == "__main__":
s = raw_input()
print removeDuplicatesInPlace(s)
RepBlakeRileyHomes, Analyst at Aristocrat Gaming
Blake Riley Homes is the leading interior designing and real estate home staging Orange County CA company.Our professional designs ...
Reppfisterruiz, Accountant at 247quickbookshelp
Door-to-door is a canvassing technique that is generally used for sales, marketing, advertising, evangelism or campaigning, in which I walk ...
Reppedrospencerp, Blockchain Developer at 247quickbookshelp
My name is Pedro and I am a 28 years old Title abstractor . I research all documents and activities affecting ...
Convert each number to string representation, then sort the numbers in descending order, first comparing the first digit of each number with the others and then the last digit. The biggest number will have the first digit as biggest among all the numbers and also it's last digit. So question boils down to sorting the list of strings by first and last characters, in reverse order
- sauravmaximus November 22, 2015