Skip to content

Instantly share code, notes, and snippets.

@ranasaani
ranasaani / find_duplicates_using_tree.py
Created April 4, 2019 19:39
Find Duplicates Using Binary Tree
import json
class Node:
def __init__(self, val=0):
self.right = ''
self.left = ''
self.value = val
class Tree(Node):
@ranasaani
ranasaani / palindrome.py
Last active May 3, 2019 16:51
Check if a number is a palindrome python
def is_palindrome(num):
rev = 0
duplicate = num
while duplicate > 0:
r = duplicate % 10
duplicate = int(duplicate / 10)
rev = rev * 10 + r % 10
return num == rev
@ranasaani
ranasaani / find_closest_palindrome_number.py
Created May 3, 2019 17:55
Find the Closest Palindrome
def convert_palindrome(_str):
first_half = _str[:int(len(_str)/2)]
middle = _str[int(len(_str)/2)]
if middle == '0':
middle = '9'
first_half = str(int(first_half) -1 )
elif middle == '9':
middle = '0'
first_half = str(int(first_half) +1 )
@ranasaani
ranasaani / stones_n_jewels.py
Created May 8, 2019 17:00
Stones and Jewels
""" You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.
The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
Note:
• S and J will consist of letters and have length at most 50.
@ranasaani
ranasaani / second_minimum_element.py
Created May 9, 2019 16:50
Find the second minimum element of an array
def second_minimum(arr):
second = arr[1]
first = arr[0]
for n in arr:
if n < first:
first = n
if n < second and n > first:
second = n
return second
@ranasaani
ranasaani / non-repeating.py
Created May 9, 2019 17:05
First non-repeating integers in an array in Python
def non_repeating(arr):
non_repeating = []
for n in arr:
if n in non_repeating:
non_repeating.pop(non_repeating.index(n))
else:
non_repeating.append(n)
return non_repeating[0] if non_repeating else None
@ranasaani
ranasaani / merge_two_sorted_arrays.py
Created May 9, 2019 17:37
Merge two sorted arrays
# Merge two sorted arrays
def merge_two_arrays(arr1, arr2):
merged = []
i = 0
j = 0
k = len(arr1)
m = len(arr2)
while len(merged) < (k + m):
@ranasaani
ranasaani / bst_sum.py
Last active May 13, 2019 18:56
BST: return the sum of values of all nodes with value between L and R
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def __init__(self):
self.sum = 0
@ranasaani
ranasaani / remove_outer_parentheses.py
Last active May 13, 2019 18:43
Remove Outermost Parentheses
class Solution:
def removeOuterParentheses(self, S):
complete_pairs = self.getCompletePairs(list(S))
output = ""
for pair in complete_pairs:
output += "".join(pair[1:-1])
return output
def getCompletePairs(self ,arr):
@ranasaani
ranasaani / hamming_distance.py
Last active May 13, 2019 18:43
461. Hamming Distance
def hammingDistance(x, y):
xor = '{0:32b}'.format(x ^ y)
count = 0
for x in xor:
if x == "1":
count += 1
return count
print(hammingDistance(1, 4))