Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
34 changes: 34 additions & 0 deletions 3sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def threeSum(nums):
nums.sort()
result = []
n = len(nums)

for i in range(n - 2):
# Skip duplicate values for i
if i > 0 and nums[i] == nums[i - 1]:
continue

left = i + 1
right = n - 1

while left < right:
total = nums[i] + nums[left] + nums[right]

if total == 0:
result.append([nums[i], nums[left], nums[right]])

# Skip duplicates for left and right
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1

left += 1
right -= 1

elif total < 0:
left += 1
else:
right -= 1

return result
39 changes: 39 additions & 0 deletions 4sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def fourSum(nums, target):
nums.sort()
n = len(nums)
result = []

for i in range(n - 3):
# Skip duplicates for first number
if i > 0 and nums[i] == nums[i - 1]:
continue

for j in range(i + 1, n - 2):
# Skip duplicates for second number
if j > i + 1 and nums[j] == nums[j - 1]:
continue

left = j + 1
right = n - 1

while left < right:
total = nums[i] + nums[j] + nums[left] + nums[right]

if total == target:
result.append([nums[i], nums[j], nums[left], nums[right]])

# Skip duplicates for left and right
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1

left += 1
right -= 1

elif total < target:
left += 1
else:
right -= 1

return result
125 changes: 125 additions & 0 deletions Basic python code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#1. Print “Hello World”
print("Hello World")


#2. Sum of two numbers
a = 10
b = 20
sum = a + b
print("Sum =", sum)




#3 Check whether a number is even or odd
num = 7

if num % 2 == 0:
print("Even")
else:
print("Odd")




#4 . Find the largest of two numbers
a = 10
b = 20

if a > b:
print("Largest =", a)
else:
print("Largest =", b)



#5 Print numbers from 1 to 10
for i in range(1, 11):
print(i)



#6 Find factorial of a number
num = 5
fact = 1

for i in range(1, num + 1):
fact = fact * i

print("Factorial =", fact)



#7 check whether a number is prime
num = 7
flag = True

if num <= 1:
flag = False

for i in range(2, num):
if num % i == 0:
flag = False
break

if flag:
print("Prime number")
else:
print("Not a prime number")




#8 Reverse a string
s = "python"
rev = ""

for ch in s:
rev = ch + rev

print("Reversed string =", rev)



#9 s = "python"
rev = ""

for ch in s:
rev = ch + rev

print("Reversed string =", rev)



#10. Find sum of elements in a list
list1 = [1, 2, 3, 4, 5]
total = 0

for i in list1:
total += i

print("Sum =", total)



#11. Function to add two numbers
def add(a, b):
return a + b

print(add(5, 3))




#12 Check even/odd using function
def check_even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"






26 changes: 26 additions & 0 deletions Boyer–Moore Voting Algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def majorityElement(nums):
# Step 1: Find potential candidates
count1 = count2 = 0
candidate1 = candidate2 = None

for num in nums:
if num == candidate1:
count1 += 1
elif num == candidate2:
count2 += 1
elif count1 == 0:
candidate1, count1 = num, 1
elif count2 == 0:
candidate2, count2 = num, 1
else:
count1 -= 1
count2 -= 1

# Step 2: Verify the candidates
result = []
if nums.count(candidate1) > len(nums) // 3:
result.append(candidate1)
if candidate2 != candidate1 and nums.count(candidate2) > len(nums) // 3:
result.append(candidate2)

return result
11 changes: 11 additions & 0 deletions First Unique Character in a String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def firstUniqChar(s):
freq = {}

for ch in s:
freq[ch] = freq.get(ch, 0) + 1

for i, ch in enumerate(s):
if freq[ch] == 1:
return i

return -1
9 changes: 9 additions & 0 deletions Range Sum Query – Immutable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class NumArray:

def __init__(self, nums):
self.prefix = [0]
for num in nums:
self.prefix.append(self.prefix[-1] + num)

def sumRange(self, left, right):
return self.prefix[right + 1] - self.prefix[left]
31 changes: 31 additions & 0 deletions Range Sum Query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class NumArray:

def __init__(self, nums):
self.prefix = [0]
for num in nums:
self.prefix.append(self.prefix[-1] + num)

def sumRange(self, left: int, right: int) -> int:
return self.prefix[right + 1] - self.prefix[left]






#Binary Search
class Solution:
def search(self, nums, target):
left, right = 0, len(nums) - 1

while left <= right:
mid = (left + right) // 2

if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1

return -1
20 changes: 20 additions & 0 deletions add binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def addBinary(self, a: str, b: str) -> str:
i, j = len(a) - 1, len(b) - 1
carry = 0
result = []

while i >= 0 or j >= 0 or carry:
total = carry

if i >= 0:
total += int(a[i])
i -= 1
if j >= 0:
total += int(b[j])
j -= 1

result.append(str(total % 2))
carry = total // 2

return ''.join(reversed(result))
Loading