Skip to content

Commit 6044b69

Browse files
committed
Add
1 parent 9142516 commit 6044b69

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

Python/Add_Binary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Add two binary numbers
2+
def addBinary(a, b):
3+
return (str(bin(int(a, 2) + int(b, 2)))[2:])

Python/Longest_Common_Prefix.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def longestCommonPrefix(strs):
2+
"""
3+
:type strs: List[str]
4+
:rtype: str
5+
"""
6+
for (string in strs):
7+
for i in range(len(string)[::-1]:
8+
9+
if(string[:i] in )
10+
11+
12+
13+
print(longestCommonPrefix(["flower","flow","flight"]))

Python/Privot_Index.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#The pivot index is the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
2+
#If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
3+
4+
def pivot(nums):
5+
total = sum(nums)
6+
for i in range(len(nums)):
7+
left_sum = sum(nums[:i])
8+
if(left_sum == total-left_sum-nums[i]):
9+
return i
10+
return -1

Python/Spiral_Matrix_Traversal.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
test = [
2+
[1, 2, 3, 4],
3+
[5, 6, 7, 8],
4+
[9, 10, 11, 12],
5+
[13, 14, 15, 16]
6+
]
7+
8+
def spiral(matrix):
9+
directions = ["R", "D", "L", "U"]
10+
direction_index = 0
11+
direction = "R"
12+
traversal = []
13+
offset = 0
14+
total = 0
15+
for i in range(len(matrix)):
16+
for j in range(len(matrix)):
17+
total += 1
18+
if(direction == 'R'):
19+
print(offset)
20+
if(j == (len(matrix) - 1 - offset)):
21+
traversal += ['X']
22+
direction = 'D'
23+
else:
24+
traversal += [matrix[i + offset][j - offset]]
25+
elif(direction == 'D'):
26+
traversal += [matrix[j][len(matrix) - 1 - offset]]
27+
if(j == (len(matrix) - 1 - offset)):
28+
traversal += ['X']
29+
direction = 'L'
30+
elif(direction == 'L'):
31+
traversal += [matrix[len(matrix) - 1 - offset][-1 * j - 1]]
32+
if(j == (len(matrix) - 1 - offset)):
33+
traversal += ['X']
34+
direction = 'U'
35+
elif(direction == 'U'):
36+
traversal += [matrix[-1 * j - 1][offset]]
37+
if(j == (len(matrix) - 1 - offset)):
38+
traversal += ['X']
39+
direction = 'R'
40+
offset += 1
41+
print(total)
42+
return traversal
43+
44+
print(spiral(test))

0 commit comments

Comments
 (0)