Skip to content

Commit f5e22d7

Browse files
committed
Python Array Data structure
1 parent f8a17bd commit f5e22d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+119
-119
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Find the equilibrium index of an array. An equilibrium index is such that
2-
# the sum of elements to the left of it is equal to sum of elements to the right
3-
# of it
4-
5-
def find_equi(arr):
6-
total_sum = sum(arr)
7-
8-
left_sum = 0
9-
10-
for i, num in enumerate(arr):
11-
12-
total_sum -= num
13-
14-
if left_sum == total_sum:
15-
return i
16-
17-
left_sum += num
18-
19-
return -1
20-
21-
22-
arr = [-7, 1, 5, 2, -4, 3, 0]
23-
print(find_equi(arr))
1+
# Find the equilibrium index of an array. An equilibrium index is such that
2+
# the sum of elements to the left of it is equal to sum of elements to the right
3+
# of it
4+
5+
def find_equi(arr):
6+
total_sum = sum(arr)
7+
8+
left_sum = 0
9+
10+
for i, num in enumerate(arr):
11+
12+
total_sum -= num
13+
14+
if left_sum == total_sum:
15+
return i
16+
17+
left_sum += num
18+
19+
return -1
20+
21+
22+
arr = [-7, 1, 5, 2, -4, 3, 0]
23+
print(find_equi(arr))
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Rearrange an array such that numbers at even indexes are greater than numbers
2-
# at odd indexes
3-
4-
def rearrange(arr):
5-
for i in range(1, len(arr)):
6-
if i % 2 == 0:
7-
if arr[i] > arr[i-1]:
8-
arr[i-1], arr[i] = arr[i], arr[i-1]
9-
else:
10-
if arr[i] < arr[i-1]:
11-
arr[i-1], arr[i] = arr[i], arr[i-1]
12-
print(arr)
13-
14-
15-
arr = [ 1, 3, 2, 2, 5 ]
1+
# Rearrange an array such that numbers at even indexes are greater than numbers
2+
# at odd indexes
3+
4+
def rearrange(arr):
5+
for i in range(1, len(arr)):
6+
if i % 2 == 0:
7+
if arr[i] > arr[i-1]:
8+
arr[i-1], arr[i] = arr[i], arr[i-1]
9+
else:
10+
if arr[i] < arr[i-1]:
11+
arr[i-1], arr[i] = arr[i], arr[i-1]
12+
print(arr)
13+
14+
15+
arr = [ 1, 3, 2, 2, 5 ]
1616
rearrange(arr)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
# Minimum swaps required to bring all elements less than or equal to k together
2-
3-
def min_swaps(arr, k):
4-
# First find out how many elements are there which are less than or
5-
# equal to k
6-
count = 0
7-
for i in arr:
8-
if i <= k:
9-
count += 1
10-
11-
# This count defines a window - inside this window all our elements should
12-
# be placed
13-
# Find the count of bad elements - elements which are more than k and that will be
14-
# our starting answer as we will have to swap them out
15-
bad = 0
16-
for i in range(0, count):
17-
if arr[i] > k:
18-
bad += 1
19-
20-
ans = bad
21-
j = count
22-
23-
for i in range(0, len(arr)):
24-
if j == len(arr):
25-
break
26-
27-
if arr[i] > k:
28-
bad -= 1 # because we have moved the bad element out of the window
29-
30-
if arr[j] > k:
31-
bad += 1
32-
33-
ans = min(bad, ans)
34-
j += 1
35-
36-
print('answer - ', ans)
37-
38-
arr = [2,7,9,5,8,7,4]
1+
# Minimum swaps required to bring all elements less than or equal to k together
2+
3+
def min_swaps(arr, k):
4+
# First find out how many elements are there which are less than or
5+
# equal to k
6+
count = 0
7+
for i in arr:
8+
if i <= k:
9+
count += 1
10+
11+
# This count defines a window - inside this window all our elements should
12+
# be placed
13+
# Find the count of bad elements - elements which are more than k and that will be
14+
# our starting answer as we will have to swap them out
15+
bad = 0
16+
for i in range(0, count):
17+
if arr[i] > k:
18+
bad += 1
19+
20+
ans = bad
21+
j = count
22+
23+
for i in range(0, len(arr)):
24+
if j == len(arr):
25+
break
26+
27+
if arr[i] > k:
28+
bad -= 1 # because we have moved the bad element out of the window
29+
30+
if arr[j] > k:
31+
bad += 1
32+
33+
ans = min(bad, ans)
34+
j += 1
35+
36+
print('answer - ', ans)
37+
38+
arr = [2,7,9,5,8,7,4]
3939
min_swaps(arr, 5)
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# A peak element is an element such that both of its neighbours are smaller than it
2-
# In case of corner elements, consider only one neighbour
3-
4-
5-
def peak(arr, low, high):
6-
n = len(arr)
7-
8-
while low <= high:
9-
mid = low + (high - low) / 2
10-
mid = int(mid)
11-
12-
if (mid == 0 or arr[mid-1] <= arr[mid]) and (mid == n-1 or arr[mid+1] <= arr[mid]):
13-
return(arr[mid])
14-
15-
elif mid > 0 and arr[mid-1] > arr[mid]:
16-
high = mid - 1
17-
18-
else:
19-
low = mid + 1
20-
21-
arr = [1, 3, 20, 4, 1, 0]
1+
# A peak element is an element such that both of its neighbours are smaller than it
2+
# In case of corner elements, consider only one neighbour
3+
4+
5+
def peak(arr, low, high):
6+
n = len(arr)
7+
8+
while low <= high:
9+
mid = low + (high - low) / 2
10+
mid = int(mid)
11+
12+
if (mid == 0 or arr[mid-1] <= arr[mid]) and (mid == n-1 or arr[mid+1] <= arr[mid]):
13+
return(arr[mid])
14+
15+
elif mid > 0 and arr[mid-1] > arr[mid]:
16+
high = mid - 1
17+
18+
else:
19+
low = mid + 1
20+
21+
arr = [1, 3, 20, 4, 1, 0]
2222
print(peak(arr, 0, len(arr) - 1))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Rearragne positive and negative numbers in an array such that they appear
2-
# alternately. If there are more numbers of any one kind, put them at the end
3-
4-
def rearrange(arr):
5-
i = -1
6-
7-
for j in range(len(arr)):
8-
if arr[j] < 0:
9-
i += 1 # maintaining index of the last negative number
10-
arr[j], arr[i] = arr[i], arr[j]
11-
12-
pos = i + 1 # index of first positive number
13-
neg = 0 # index of first negative number
14-
15-
while pos < len(arr) and neg < pos and arr[neg] < 0:
16-
arr[pos], arr[neg] = arr[neg], arr[pos]
17-
pos += 1
18-
neg += 2
19-
20-
print(arr)
21-
22-
arr = [-1, 2, -3, 4, 5, 6, -7, 8, 9]
1+
# Rearragne positive and negative numbers in an array such that they appear
2+
# alternately. If there are more numbers of any one kind, put them at the end
3+
4+
def rearrange(arr):
5+
i = -1
6+
7+
for j in range(len(arr)):
8+
if arr[j] < 0:
9+
i += 1 # maintaining index of the last negative number
10+
arr[j], arr[i] = arr[i], arr[j]
11+
12+
pos = i + 1 # index of first positive number
13+
neg = 0 # index of first negative number
14+
15+
while pos < len(arr) and neg < pos and arr[neg] < 0:
16+
arr[pos], arr[neg] = arr[neg], arr[pos]
17+
pos += 1
18+
neg += 2
19+
20+
print(arr)
21+
22+
arr = [-1, 2, -3, 4, 5, 6, -7, 8, 9]
2323
rearrange(arr)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)