Skip to content

Commit 979d6f1

Browse files
committed
GFG Improvements - Pending for publications
1 parent 80bbfe1 commit 979d6f1

4 files changed

Lines changed: 189 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import itertools
2+
3+
def Sub_Sequences(STR):
4+
combs = []
5+
for l in range(1,len(STR)+1):
6+
combs.append(list(itertools.combinations(STR,l)))
7+
for c in combs:
8+
for t in c:
9+
print (''.join(t),end=' ')
10+
11+
12+
if __name__ == '__main__':
13+
Sub_Sequences('geek')
14+
Sub_Sequences('geeks')
15+
Sub_Sequences('geeksforgeeks')
16+
Sub_Sequences('hackerrank')
17+
Sub_Sequences('hackerearth')
18+
Sub_Sequences('codechef')
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
def encrypted_string(strg):
3+
EncryptedString = [strg[0]]
4+
for t in range(1,len(strg)):
5+
if (t%2 == 1):
6+
EncryptedString.append(strg[t])
7+
else:
8+
EncryptedString.insert(0,strg[t])
9+
return (''.join(EncryptedString))
10+
11+
print (encrypted_string('geeks'))
12+
print (encrypted_string('geeksforgeeks'))
13+
print (encrypted_string('strive'))
14+
print (encrypted_string('vikaschitturi'))
15+
print (encrypted_string('padmachitturi'))
16+
print (encrypted_string('hackerrank'))
17+
print (encrypted_string('hackerearth'))
18+
print (encrypted_string('codechef'))
19+
print (encrypted_string('IamVikas'))
20+
print (encrypted_string('HelloHowareyou'))
21+
print (encrypted_string('whatareyoudoing'))
22+
23+
24+
25+
26+
def decrypted_string(strg):
27+
if (len(strg)%2!=0):
28+
DecryptedString = [strg[0]]
29+
for t in range(1,len(strg)):
30+
DecryptedString.append(strg[-1*t])
31+
DecryptedString.append(strg[t])
32+
DecryptedString = DecryptedString[:len(strg)][::-1]
33+
return (''.join(DecryptedString))
34+
else:
35+
reverse_strg = strg[::-1]
36+
DecryptedString=[reverse_strg[0]]
37+
for t in range(1,len(reverse_strg)):
38+
DecryptedString.append(reverse_strg[-1*t])
39+
DecryptedString.append(reverse_strg[t])
40+
DecryptedString = DecryptedString[:len(reverse_strg)][::-1]
41+
return (''.join(DecryptedString))
42+
43+
44+
print(decrypted_string('segek'))
45+
print(decrypted_string('segosegekfrek'))
46+
print(decrypted_string('vrstie'))
47+
print(decrypted_string('iuthskviacitr'))
48+
print(decrypted_string('iuthadpamcitr'))
49+
print(decrypted_string('nrechakrak'))
50+
print(decrypted_string('hreechakrat'))
51+
print(decrypted_string(encrypted_string('IamVikas')))
52+
print(decrypted_string(encrypted_string('HelloHowareyou')))
53+
print(decrypted_string(encrypted_string('whatareyoudoing')))
54+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Traversing from one point to another point
2+
#storing the minimum number of steps
3+
def traversal_steps(A,B):
4+
points = list(zip(A,B))
5+
minSteps = 0
6+
for p in range(len(points)-1):
7+
#taking the manhattan distance between x and y-coordinates
8+
d1 = abs(points[p][0] - points[p+1][0])
9+
d2 = abs(points[p][1] - points[p+1][1])
10+
#adding the maximum among the two to the running steps parameter
11+
minSteps += max(d1,d2)
12+
return (minSteps)
13+
14+
#Main Driver Code
15+
if __name__ == '__main__':
16+
A = list(map(int,input().strip().split()))
17+
B = list(map(int,input().strip().split()))
18+
print (traversal_steps(A,B))
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""
2+
Minimize the number of replacements to get a string with
3+
same number of ‘a’, ‘b’ and ‘c’ in it
4+
"""
5+
def minimize_replacements(STR):
6+
#Count of a's, b's and c's in a given string
7+
count_a = STR.count('a')
8+
count_b = STR.count('b')
9+
count_c = STR.count('c')
10+
# if three of them are equal return the same string
11+
if count_a == count_b == count_c:
12+
return (STR)
13+
14+
#if not a multiple of 3 then return -1
15+
if len(STR)%3 != 0:
16+
return -1
17+
#Increase the number of a's by
18+
#removing extra 'b' and ;c;
19+
i=0
20+
s = list(STR)
21+
while ((count_a < len(STR)//3) & (i < len(STR))):
22+
#Check if it is 'b' and it more than n/3
23+
if ((STR[i]=='b') & (count_b > len(STR)//3)):
24+
count_b-=1
25+
s[i]='a'
26+
count_a+=1
27+
#Check if it is 'c' and it more than n/3
28+
elif ((STR[i]=='c') & (count_c > len(STR)//3)):
29+
count_c-=1
30+
s[i]='a'
31+
count_a+=1
32+
i+=1
33+
34+
35+
i=0
36+
STR = ''.join(s)
37+
t = list(STR)
38+
#Increase the number of b's by removing extra 'c'
39+
while ((count_b < len(STR)//3) & (i < len(STR))):
40+
#Check if it is 'c' and it more than n/3
41+
if ((STR[i] == 'c') & (count_c > len(STR)//3)):
42+
count_c-=1
43+
t[i]='b'
44+
count_b+=1
45+
i+=1
46+
47+
48+
i = len(STR) - 1
49+
STR = ''.join(t)
50+
u = list(STR)
51+
#Increase the number of c's from back
52+
while ((count_c < len(STR)//3) & (i >= 0)):
53+
#Check if it is 'a' and it more than n/3
54+
if ((STR[i] == 'a') & (count_a > len(STR)//3)):
55+
count_a-=1
56+
u[i] = 'c';
57+
count_c+=1
58+
i-=1
59+
60+
61+
62+
i = len(STR) - 1
63+
STR = ''.join(u)
64+
v = list(STR)
65+
#Increase the number of b's from back
66+
while ((count_b < len(STR)//3) & (i >= 0)):
67+
#Check if it is 'a' and it more than n/3
68+
if ((STR[i] == 'a') & (count_a > len(STR)//3)):
69+
count_a-=1
70+
v[i] = 'b'
71+
count_b+=1
72+
i-=1
73+
74+
75+
76+
i = len(STR) - 1
77+
STR = ''.join(v)
78+
w = list(STR)
79+
#Increase the number of c's from back
80+
while ((count_c < len(STR)//3) & (i >= 0)):
81+
#Check if it is 'b' and it more than n/3
82+
if ((STR[i] == 'b') & (count_b > len(STR)//3)):
83+
count_b-=1
84+
w[i] = 'c'
85+
count_c+=1
86+
i-=1
87+
88+
89+
STR = ''.join(w)
90+
return STR
91+
92+
93+
if __name__ == '__main__':
94+
print (minimize_replacements('bcabba'))
95+
print (minimize_replacements('aaaaaa'))
96+
print (minimize_replacements('aabcbabbc'))
97+
print (minimize_replacements('aaabcbcbc'))
98+
print (minimize_replacements('aaaaa'))
99+

0 commit comments

Comments
 (0)