Skip to content

Commit fe27685

Browse files
committed
New Strings questions
1 parent d43af76 commit fe27685

12 files changed

+429
-0
lines changed

Arrays/BubbleSort.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Bubble sort is a example of sorting algorithm . In this method we at first compare the data element in the first position with the second position and arrange them in desired order.Then we compare the data element with with third data element and arrange them in desired order. The same process continuous until the data element at second last and last position
3+
'''
4+
def bubbleSort(arr,n):
5+
for i in range(0,n):
6+
for j in range(0,n):
7+
if arr[j]>arr[i]:
8+
arr[i], arr[j] = arr[j], arr[i]
9+
return arr
10+
11+
arr = [3,2,6,4,1]
12+
print bubbleSort(arr,5)

Binary-Search/Matrix-median.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.
3+
'''
4+
class Solution:
5+
# @param A : list of list of integers
6+
# @return an integer
7+
def findMedian(self, matrix):
8+
9+
lis = []
10+
for i in range(len(matrix)):
11+
for j in range(len(matrix[0])):
12+
lis.append(matrix[i][j])
13+
14+
lis.sort()
15+
return lis[len(lis)/2]

Binary-Search/Median-of-array.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
There are two sorted arrays A and B of size m and n respectively.
3+
4+
Find the median of the two sorted arrays ( The median of the array formed by merging both the arrays ).
5+
6+
The overall run time complexity should be O(log (m+n)).
7+
'''
8+
9+
class Solution:
10+
# @param A : tuple of integers
11+
# @param B : tuple of integers
12+
# @return a double
13+
def findMedianSortedArrays(self, A, B):
14+
m = len(A)
15+
n = len(B)
16+
if m > n:
17+
n,m = m,n
18+
A,B = B,A
19+
if m == 0:
20+
if n == 0:
21+
return 0
22+
if n%2:
23+
return B[n/2]
24+
else:
25+
return (B[n/2]+B[n/2-1])/2.0
26+
low = 0
27+
high = m
28+
while low <= high:
29+
i = (low+high)/2
30+
j = (m+n+1)/2-i
31+
if (j == 0 or i == m or B[j - 1] <= A[i]) and (i == 0 or j == n or A[i-1] <= B[j]):
32+
if (m+n)%2:
33+
if i == 0:
34+
return B[j-1]
35+
elif j == 0:
36+
return A[i-1]
37+
return max(A[i-1],B[j-1])
38+
else:
39+
if i == 0:
40+
return (B[j-1] + min(A[i],B[j]))/2.0
41+
if j == 0:
42+
return (A[i-1] + min(A[i],B[j]))/2.0
43+
if i == m:
44+
return (max(A[i-1],B[j-1]) + B[j])/2.0
45+
if j == n:
46+
return (max(A[i-1],B[j-1]) + A[i])/2.0
47+
return (max(A[i-1],B[j-1]) + min(A[i],B[j]))/2.0
48+
elif (j == 0 or i == m or B[j - 1] > A[i]):
49+
low = i+1
50+
elif (i == 0 or j == n or A[i-1] > B[j]):
51+
high = i-1
52+
return -1
53+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
You have to paint N boards of length {A0, A1, A2, A3 … AN-1}. There are K painters available and you are also given how much time a painter takes to paint 1 unit of board. You have to get this job done as soon as possible under the constraints that any painter will only paint contiguous sections of board.
3+
'''
4+
def ppp(n,time,arr):
5+
def isPossible(t,c,a):
6+
summ = 0
7+
while a > 0 and len(c) > 0:
8+
if summ + c[-1] > t:
9+
summ = 0
10+
a -=1
11+
else:
12+
summ += c.pop()
13+
if len(c) == 0:
14+
return True
15+
else:
16+
return False
17+
18+
x = max(arr)
19+
y = sum(arr)
20+
if len(arr) == 0:
21+
return 0
22+
if len(arr) == 1:
23+
return time*arr[0]
24+
prev = y
25+
while x < y:
26+
m = (x + y)/2
27+
if isPossible(m,arr[:],n):
28+
prev = m
29+
y = m - 1
30+
else:
31+
x = m + 1
32+
m = prev
33+
while isPossible(m-1,arr[:],n):
34+
m = m - 1
35+
return m*B % 1000003
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from math import floor
2+
def sqrtsearch(x):
3+
start = 1
4+
end = x
5+
ans = 0
6+
if x == 0 or x == 1:
7+
return x
8+
while start <= end:
9+
mid = (start+end)/2
10+
if x == mid*mid:
11+
return mid
12+
elif x > (mid*mid):
13+
start = mid + 1
14+
ans = mid
15+
else:
16+
end = mid - 1
17+
return floor(ans)
18+
19+
x = 11
20+
print sqrtsearch(x)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
3+
4+
You may assume no duplicates in the array.
5+
'''
6+
class Solution:
7+
# @param A : list of integers
8+
# @param B : integer
9+
# @return an integer
10+
def searchInsert(self, arr, target):
11+
12+
floor_index = 0
13+
ceiling_index = len(arr) - 1
14+
if len(arr) == 1 and arr[0] == target:
15+
return 0
16+
if arr[ceiling_index-1] < target:
17+
return ceiling_index + 1
18+
19+
while floor_index < ceiling_index:
20+
mid = (floor_index + ceiling_index)/2
21+
if arr[mid] == target:
22+
return mid
23+
elif arr[mid] > target:
24+
ceiling_index = mid-1
25+
ans = mid -1
26+
else:
27+
floor_index = mid + 1
28+
ans = mid + 1
29+
if arr[ans] < target:
30+
return ans + 1
31+
else:
32+
return ans

Math/Sorted-Permutation-Rank.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
Given a string, find the rank of the string amongst its permutations sorted lexicographically.
3+
Assume that no characters are repeated.
4+
5+
EXAMPLE:
6+
Input : 'acb'
7+
Output : 2
8+
9+
The order permutations with letters 'a', 'c', and 'b' :
10+
abc
11+
acb
12+
bac
13+
bca
14+
cab
15+
cba
16+
The answer might not fit in an integer, so return your answer % 1000003
17+
'''
18+
from itertools import permutations
19+
class Solution:
20+
# @param A : string
21+
# @return an integer
22+
def findRank(self, st):
23+
24+
def fact(n):
25+
26+
f = 1
27+
while n >= 1:
28+
f = f*n
29+
n = n-1
30+
return f
31+
32+
def findSmallerInRight(st,low,high):
33+
countRight = 0
34+
i = low + 1
35+
while i <= high:
36+
if st[i] < st[low]:
37+
countRight += 1
38+
i = i + 1
39+
return countRight
40+
41+
mul = fact(len(st))
42+
rank = 1
43+
i = 0
44+
while i < len(st):
45+
mul = mul/(len(st)-i)
46+
#count no of chars smaller than str[i] from str[i+1] to str[len-1]
47+
countRight = findSmallerInRight(st,i,len(st)-1)
48+
rank = rank + countRight*mul
49+
i = i+1
50+
for i in set(st):
51+
if st.count(i) > 1:
52+
rank =rank/fact(st.count(i))
53+
54+
return rank%1000003

Math/largest-coprime-divisor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
You are given two positive numbers A and B. You need to find the maximum valued integer X such that:
3+
4+
X divides A i.e. A % X = 0
5+
X and B are co-prime i.e. gcd(X, B) = 1
6+
'''
7+
def gcd(a, b):
8+
while b:
9+
a, b = b, a%b
10+
return a
11+
12+
class Solution:
13+
# @param A : integer
14+
# @param B : integer
15+
# @return an integer
16+
def cpFact(self, A, B):
17+
p=gcd(A,B)
18+
while p!=1:
19+
A/=p
20+
p=gcd(A,B)
21+
return A
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from itertools import permutations
2+
class Solution:
3+
4+
def fact (self, n ) :
5+
if n <= 1 :
6+
return 1
7+
else :
8+
return n * self.fact(n-1)
9+
10+
def findRank(self, A):
11+
12+
res = 1
13+
char_occur = {}
14+
for char in A:
15+
char_occur[char] = char_occur.get(char, 0) + 1
16+
for i in range(0, len(A)-1) :
17+
rank = 0
18+
for j in range(i+1, len(A)) :
19+
if A[i] > A[j] :
20+
rank += 1
21+
temp = self.fact(len(A) - i - 1)%1000003
22+
temp1= 1
23+
for key in char_occur.keys() :
24+
temp1 *= self.fact(char_occur[key])
25+
temp1 = pow(temp1, 1000001, 1000003)
26+
res = (res + rank * temp1 * temp)%1000003
27+
char_occur[A[i]] -= 1
28+
return res
29+
30+
'''
31+
Another Solution
32+
'''
33+
def fact(n) :
34+
f = 1
35+
while n >= 1 :
36+
f = f * n
37+
n = n - 1
38+
return f
39+
40+
# A utility function to count smaller
41+
# characters on right of arr[low]
42+
def findSmallerInRight(st, low, high) :
43+
44+
countRight = 0
45+
i = low + 1
46+
while i <= high :
47+
if st[i] < st[low] :
48+
countRight = countRight + 1
49+
i = i + 1
50+
51+
return countRight
52+
53+
# A function to find rank of a string
54+
# in all permutations of characters
55+
def findRank (st) :
56+
ln = len(st)
57+
mul = fact(ln)
58+
rank = 1
59+
i = 0
60+
61+
while i < ln :
62+
63+
mul = mul / (ln - i)
64+
65+
# count number of chars smaller
66+
# than str[i] fron str[i+1] to
67+
# str[len-1]
68+
countRight = findSmallerInRight(st, i, ln-1)
69+
70+
rank = rank + countRight * mul
71+
i = i + 1
72+
for j in set(st):
73+
if st.count(j) > 1:
74+
rank = rank/fact(st.count(j))
75+
return rank + 1
76+
77+
78+
# Driver program to test above function
79+
st = "aba"
80+
print (findRank(st))

String/KMP-Algo.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def KMPSearch(pat,txt):
2+
m = len(pat)
3+
n = len(txt)
4+
#Now create a lps array that will hold the longest prefix suffix value
5+
lps = [0]*m
6+
j = 0 #index for pat
7+
computeLPSArray(pat,m,lps)
8+
9+
i = 0 #index For txt[]
10+
11+
while i < n:
12+
if pat[j] == txt[i]:
13+
i += 1
14+
j += 1
15+
if j == m:
16+
print "Pattern found at index" + str(i-j)
17+
j = lps[j-1]
18+
elif i < n and pat[j] != txt[i]:
19+
if j != 0:
20+
j = lps[j-1]
21+
else:
22+
i += 1
23+
24+
def computeLPSArray(pat,m,lps):
25+
j = 0 #length of previous lps
26+
lps[0] = 0
27+
i = 1
28+
while i < m:
29+
if pat[i] == pat[j]:
30+
j += 1
31+
lps[i] = j
32+
i += 1
33+
else:
34+
if j != 0:
35+
j = lps[j-1]
36+
37+
else:
38+
lps[i] = 0
39+
i += 1
40+
41+
#Now time to test
42+
txt = "ABABDABACDABABCABAB"
43+
pat = "ABABCABAB"
44+
KMPSearch(pat,txt)

0 commit comments

Comments
 (0)