Skip to content

Instantly share code, notes, and snippets.

@mdpabel
Last active August 14, 2022 17:23
Show Gist options
  • Save mdpabel/22f1d531243fe45fc68fc45f90983892 to your computer and use it in GitHub Desktop.
Save mdpabel/22f1d531243fe45fc68fc45f90983892 to your computer and use it in GitHub Desktop.
Assignment
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'alternatingCharacters' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING s as parameter.
#
"""
AAABBB
AA ->1
AA ->
AB
BB->1
BB ->1
"""
def alternatingCharacters(s):
n = len(s)
count = 0
for i in range(n - 1):
if s[i] == s[i+1]:
count += 1
return count
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input().strip())
for q_itr in range(q):
s = input()
result = alternatingCharacters(s)
fptr.write(str(result) + '\n')
fptr.close()
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'countSwaps' function below.
#
# The function accepts INTEGER_ARRAY a as parameter.
#
"""
3 2 1
i = 0
2 3 1
2 1 3
2
i = 1
1 2 3
3
1 2 3
3 -> break
i = 2
1 2 3
1 2 3
"""
def countSwaps(a):
n = len(a)
no_of_swap = 0
for i in range(n):
prev_swap = no_of_swap
for j in range(n-1):
if a[j] > a[j + 1]:
a[j],a[j+1] = a[j+1],a[j]
no_of_swap += 1
if(prev_swap == no_of_swap):
break
if(prev_swap == no_of_swap):
break
print(f"Array is sorted in {no_of_swap} swaps.")
print(f"First Element: {a[0]}")
print(f"Last Element: {a[-1]}")
if __name__ == '__main__':
n = int(input().strip())
a = list(map(int, input().rstrip().split()))
countSwaps(a)
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
#
# Complete the 'makeAnagram' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING a
# 2. STRING b
#
"""
cde -> {
c : 1
d : 1
e : 1
}
abc -> {
a : 1
b : 1
c : 1
}
s1 -> c
"""
def makeAnagram(a, b):
s1 = Counter(a)
s2 = Counter(b)
count = 0
for i in s1:
if item in s2:
count += min(s1[item], s2[item]) * 2
return (len(a) + len(b)) - count
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
a = input()
b = input()
res = makeAnagram(a, b)
fptr.write(str(res) + '\n')
fptr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment