Printing anagrams together using List and Dictionary

def allAnagram(input):

dict = {}

for strVal in input:

key = ''.join(sorted(strVal))

if key in dict.keys():

dict[key].append(strVal)

else:

dict[key] = []

dict[key].append(strVal)

output = ""

for key,value in dict.items():

output = output + ' '.join(value) + ' '

return output

if __name__ == "__main__":

input=['cat', 'dog', 'tac', 'god', 'act']

print (allAnagram(input))


Program for Reversal algorithm for array rotation

def rverseArray(arr, start, end):

while (start < end):

temp = arr[start]

arr[start] = arr[end]

arr[end] = temp

start += 1

end = end-1

def leftRotate(arr, d):

n = len(arr)

rverseArray(arr, 0, d-1)

rverseArray(arr, d, n-1)

rverseArray(arr, 0, n-1)

def printArray(arr):

for i in range(0, len(arr)):

print (arr[i])

arr = [1, 2, 3, 4, 5, 6, 7]

leftRotate(arr, 2)

printArray(arr)


Program to Split the array and add the first part to the end

def splitArr(arr, n, k):

for i in range(0, k):

x = arr[0]

for j in range(0, n-1):

arr[j] = arr[j + 1]

arr[n-1] = x

arr = [12, 8, 10, 11, 52, 36]

n = len(arr)

position = 2

splitArr(arr, n, position)

for i in range(0, n):

print(arr[i], end = ' ')


Program for BogoSort or Permutation Sort

import random

def bogoSort(a):

n = len(a)

while (is_sorted(a)== False):

shuffle(a)

def is_sorted(a):

n = len(a)

for i in range(0, n-1):

if (a[i] > a[i+1] ):

return False

return True

def shuffle(a):

n = len(a)

for i in range (0,n):

r = random.randint(0,n-1)

a[i], a[r] = a[r], a[i]

a = [3, 2, 4, 1, 0, 5]

bogoSort(a)

print("Sorted array :")

for i in range(len(a)):

print ("%d" %a[i]),


Program to find mirror characters in a string

def mirrorChars(input,k):

original = 'abcdefghijklmnopqrstuvwxyz'

reverse = 'zyxwvutsrqponmlkjihgfedcba'

dictChars = dict(zip(original,reverse))

prefix = input[0:k-1]

suffix = input[k-1:]

mirror = ''

for i in range(0,len(suffix)):

mirror = mirror + dictChars[suffix[i]]

print (prefix+mirror)

if __name__ == "__main__":

input = 'paradox'

k = 3

mirrorChars(input,k)