Palindrome and Anagram Assignment: Python Implementation

Verified

Added on  2019/09/22

|3
|323
|666
Homework Assignment
AI Summary
This assignment solution presents a Python implementation for identifying palindromes and anagrams. The solution begins by outlining the pseudocode for determining if a string is a palindrome, which involves comparing characters from both ends of the string. The code then implements this algorithm. The solution also includes the pseudocode and implementation for finding anagrams using a dictionary and list data structures. The anagram algorithm sorts each string, uses the sorted value as a key, and stores the original string as a value. If a key already exists, the original string is appended to the existing list of anagrams. The final output lists the anagrams.
Document Page
Pseudo Code of Palindrome
start -> pointer to traverse string from beginning
end -> pointer to traverse string from end.
function boolPalindrome(string,start,end):
if (length of string < 1)
return True
else:
if string[start] == string[end]:
return boolPalindrome(string,start+1,end-1)
else:
return False
Implementation of Algorithm in Code
1. According to the algorithm first the length of string is checked whether it is less than 1 or
not.
2. After that first and last index of string are compared.
3. If they are similar then continue comparing the indexes until there is only 1 or 0 index left
to compare.
4. If any of the indexes are not similar then string is not a palindrome
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Pseudo Code of Anagram
function printAnagrams(txtfile):
dict = {}
Document Page
for all the string in txtfile:
sorted = sort(string)
key = sorted
value of key = original value of sorted string
if key in dict.keys():
dict[key].append(str)
else:
dict[key] = []
dict[key].append(str)
output = ""
for key,value in dict.items():
output = output + ' '.join(value) + ' '+'\n'
return output
Implementation of Algorithm in Code
Above Algorithm is printing anagrams using List and Dictionary data structures.
1. First sort each string and take sorted value as key and the original value as value of key.
2. If key is already in the dictionary then just append the value in it as the key is already
there.
3. If key is not there then map a empty list to key and append value to it.
4. In the end all key will have list of anagrams
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]