Skip to content

Commit f40d37c

Browse files
a few problems at a time
1 parent c49cbe0 commit f40d37c

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

1_TwoSum/solution1.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
2+
#
3+
# You may assume that each input would have exactly one solution, and you may not use the same element twice.
4+
#
5+
# You can return the answer in any order.
6+
from typing import Tuple, Any
7+
8+
nums = [2,7,11,15]
9+
target = 9
10+
11+
def sum(nums, target) -> tuple[Any, Any]:
12+
13+
# for i in range(len(nums)):
14+
# for j in range(i+1, len(nums)):
15+
# if nums[i] + nums[j] == target:
16+
# return i, j
17+
#
18+
# return -1, -1
19+
20+
dict = {}
21+
22+
for i, n in enumerate(nums):
23+
complement = target - n
24+
if complement in dict:
25+
return dict[complement], i
26+
dict[n] = i
27+
28+
print(sum(nums, target))
29+

724_ Find Pivot Index/solution.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Given an array of integers nums, calculate the pivot index of this array.
2+
#
3+
# The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
4+
#
5+
# If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.
6+
#
7+
# Return the leftmost pivot index. If no such index exists, return -1.
8+
9+
10+
nums = [1,7,3,6,5,6]
11+
12+
def pivot(nums) -> int:
13+
l, r = 0, len(nums) - 1
14+
l1, r1 = nums[l], nums[r]
15+
16+
while l < r:
17+
if l1 < r1:
18+
l += 1
19+
l1 += nums[l]
20+
else:
21+
r -= 1
22+
r1 += nums[r]
23+
24+
25+
if l1 == r1:
26+
return l
27+
28+
return -1
29+
30+
31+
print(pivot(nums))

linked_list.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None # Reference to the next node
5+
# LinkedList class manages the nodes and operations of the linked list
6+
class LinkedList:
7+
def __init__(self):
8+
self.head = None # Initialize an empty linked list
9+
def append(self, data):
10+
new_node = Node(data)
11+
if not self.head:
12+
self.head = new_node
13+
return
14+
last_node = self.head
15+
while last_node.next:
16+
last_node = last_node.next
17+
last_node.next = new_node
18+
def print_list(self):
19+
current_node = self.head
20+
while current_node:
21+
print(current_node.data, end=" -> ")
22+
current_node = current_node.next
23+
print("None")
24+
# Example usage:
25+
llist = LinkedList()
26+
llist.append(1)
27+
llist.append(2)
28+
llist.append(3)
29+
llist.print_list()

pol_with_x.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Given a string with "?", return a palindrome if possible. EX: ?ab?b returns babab
2+
3+
string = "?ab??b"
4+
5+
def checkpol(string) -> str:
6+
answer_list = [i for i in string]
7+
8+
if len(answer_list) % 2 != 0:
9+
return "Not a palindrome"
10+
11+
l, r = 0, len(answer_list) - 1
12+
13+
while l < r:
14+
if answer_list[l] == "?":
15+
answer_list[l] = answer_list[r]
16+
elif answer_list[r] == "?":
17+
answer_list[r] = answer_list[l]
18+
l += 1
19+
r -= 1
20+
21+
answer_string = ""
22+
23+
for i in answer_list:
24+
answer_string += i
25+
26+
return answer_string
27+
28+
print(checkpol(string))

0 commit comments

Comments
 (0)