Question

In: Computer Science

Write a program in Python where you can swap only two consecutive elements. You have to...

Write a program in Python where you can swap only two consecutive elements. You have to show all steps to convert a string into another string (both strings will be anagrams of each other). E.g. GUM to MUG GUM GMU MGU MUG

Solutions

Expert Solution

# Python program to check if two strings are anagrams of

# each other

NO_OF_CHARS = 256

# Function to check whether two strings are anagram of

# each other

def areAnagram(str1, str2):

# Create two count arrays and initialize all values as 0

count1 = [0] * NO_OF_CHARS

count2 = [0] * NO_OF_CHARS

# For each character in input strings, increment count

# in the corresponding count array

for i in str1:

count1[ord(i)] += 1

for i in str2:

count2[ord(i)] += 1

# If both strings are of different length. Removing this

# condition will make the program fail for strings like

# "aaca" and "aca"

if len(str1) != len(str2):

return 0

# Compare count arrays

for i in xrange(NO_OF_CHARS):

if count1[i] != count2[i]:

return 0

return 1

# Driver code

str1 = "geeksforgeeks"

str2 = "forgeeksgeeks"

# Function call

if areAnagram(str1, str2):

print "The two strings are anagram of each other"

else:

print "The two strings are not anagram of each other"


Related Solutions

Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a Python program to read in the temperatures for ten consecutive days in Celsius and...
Write a Python program to read in the temperatures for ten consecutive days in Celsius and store them into an array. The entire array should then be displayed. Next each temperature in the array should be converted to Fahrenheit and the entire array should be again be displayed. The formula for converting Celsius to Fahrenheit is °F = (°C × 1.8) + 32. Finally, the number of cool, warm and hot days should be counted and the number of each...
Swap two adjacent elements by adjusting only the links (and not the data) using a. singly...
Swap two adjacent elements by adjusting only the links (and not the data) using a. singly linked lists b. doubly linked lists
How to make a python program that imports only turtle and math library where I can...
How to make a python program that imports only turtle and math library where I can click once on the screen to set the center of the square, move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point?
Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter...
Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function...
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements....
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements. Assume the number of identical consecutive elements are no more than three if they exist. Sample input/output #1: Enter the length of the array: 11 Enter the elements of the array: -12 0 4 2 2 2 36 7 7 7 43 Output: The array contains 2 of three identical consecutive elements: 2 7 Sample input/output #2: Enter the length of the array: 4...
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Write a C++ program to swap two numbers and show your output
Write a C++ program to swap two numbers and show your output
python program You are going to write a program that takes two inputs: A string representing...
python program You are going to write a program that takes two inputs: A string representing a list of names of the following form: first name 1, last name 1, (nick name 1); first name 2, last name 2, (nick name 2); ... A string representing part of or the complete family name. You are going to output all nick names for those names where there is a partial or complete match of the family name. If no match was...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT