Python Project: Palindrome and Anagram Detection Algorithms

Verified

Added on  2025/05/01

|11
|716
|379
AI Summary
Desklib provides past papers and solved assignments for students. This project focuses on AI algorithms for palindrome and anagram detection.
Document Page
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
Contents
Palindromes................................................................................................................................2
Algorithm in pseudo-code......................................................................................................2
Implementation of algorithm in code.....................................................................................2
Demonstration that code works correctly using representative samples................................3
Correct Output and Discussion...............................................................................................4
Anagrams...................................................................................................................................5
Algorithm in pseudo-code......................................................................................................5
Implementation of algorithm in code.....................................................................................5
Demonstration that code works correctly using representative samples................................6
Correct Output and Discussion...........................................................................................7
Conclusion..................................................................................................................................8
Document Page
Palindromes
Algorithm in pseudo-code
1) A function is to be created with the name isPalindrome in which a parameter is
passed.
2) A variable is to be created by the name rev and string is reversed.
3) A comparison is made between these two results.
4) If they are matched then True is returned which prints the value
5) If they aren’t matched then false value is returned.
6) File in which the text are present are stored in the variable named file.
7) An empty list is created by the name word_list.
8) For loop is performed on the file.
9) Words are added in the word_list
10) The list is filtered and isPalindrome are printed (Overland, B., 2017).
Implementation of algorithm in code
def isPalindrome(s):
rev = ''.join(reversed(s))
if (s == rev):
return True
return False
file=open("english.txt",'r')
word_list=[]
for line in file :
word_list.append(line.rstrip())
print(list(filter(isPalindrome,word_list)))
Document Page
Demonstration that code works correctly using representative samples
Sr. No Objective Expected results Pass/ Fail
1. Complexity of code Normal Pass.
2. The code process
from starting or takes
random words.
The code starts to
process from staring.
Pass.
3. Execution time Fast Pass.
4. Can the code be more
optimize.
Yes the code can be
more optimized.
Fail
5. Are the comparison
made for both lower
case and upper case.
Yes, lower case and
upper case are
equally treated.
Pass.
Test cases screenshots
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
Correct Output and Discussion
The output is generated in the list which is printed. In this comparisons are made between
both upper case and lower case as the words are same. The list is printed is a manner
according to the processing of the code. In this the words which appear same when read from
starting or ending are called palindrome.
Document Page
Anagrams
Algorithm in pseudo-code
1) Counter is imported from collections module.
2) A function is created with 2 parameters passed in it.
3) Variable counter_word is created which stores the values of counter word
4) For loop is performed in words to search other words
5) If words are founded the they are yielded.
6) A file is to be stored in a variable name file
7) An empty list is created.
8) For loop is to be performed in file for line
9) Values are added in Word list.
10) For loop is performed in the word list
11) A variable stores the values of number of words and the anagram words.
12) If the length of anagram word is greater than 1
13) Then the results are printed (Stephenson, B., 2015).
Implementation of algorithm in code
from collections import Counter
def anagrams(word, words):
counter_word = Counter(word)
for other_word in words:
if Counter(other_word) == counter_word:
yield other_word
file=open("english.txt",'r')
word_list=[]
for line in file :
Document Page
word_list.append(line.rstrip())
for i in word_list:
cc=list(anagrams(i,word_list))
if (len(cc)>1):
print(len(cc),cc)
Demonstration that code works correctly using representative samples
Sr. No Objective Expected results Pass/ Fail
1. Complexity of code Complex Pass.
2. Show how many
words are matched.
Numbers are written
in the starting of the
results.
Pass
3. Is the code printing
the result from the
starting or taking
words from
anywhere.
The code starts to
compare from the
starting.
Pass
4. Execution time Slow as comparison
is to performed
between
11000x11000
Fail.
5. Are both cases
compared equally or
both are different
Both cases are
compared equally.
Pass
Test case screenshots
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
Document Page
Correct Output and Discussion
In this code anagram searching is performed, in this the words which are same or are made by
using same alphabets are called anagrams. In this code number of anagrams are shown in a
list and the number of words are shown outside the list.
Document Page
Conclusion
This report is made on the 2 programs which are written in python. The two programs are on
finding palindrome and anagram. The codes are pasted in the file and test cases are
mentioned on which the programs are tested. The screenshots of the program are pasted in the
result section.
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
References:
Overland, B., 2017. Python Without Fear. Addison-Wesley Professional.
Stephenson, B., 2015. The Python Workbook: A Brief Introduction with Exercises and Solutions.
Springer.
chevron_up_icon
1 out of 11
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]