Assignment 1: Palindrome and Anagram Identification in Python

Verified

Added on  2025/05/01

|8
|718
|178
AI Summary
Desklib provides past papers and solved assignments for students. This assignment focuses on palindrome and anagram detection.
Document Page
ITECH 1400
FUNDAMENTALS OF PROGRAMMING
ASSIGNMENT 1
Student name:
Student ID:
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
Table of Contents
Palindromes................................................................................................................................3
Algorithm in pseudo-code......................................................................................................3
Implementation in code..........................................................................................................3
Demonstration........................................................................................................................4
Output and discussion............................................................................................................5
Anagrams...................................................................................................................................6
Algorithm in pseudo-code......................................................................................................6
Implementation in code..........................................................................................................6
Demonstration........................................................................................................................8
Output and discussion............................................................................................................8
Document Page
Palindromes
Algorithm in pseudo-code
1. Creating list to store correct results.
2. Read data from text file “with open(filepath.extension, ‘r’)”
3. Iterate through the complete file to pick words one by one.
for word in k:
word= word. strip() //such that words separated with a space or if in next line
are picked one by one
reverse the picked word and compare it with the original one
if (match)
print (output)
Implementation in code
output=[] #list to store the
palindromes
with open('English.txt', 'r') as k:
for word in k:
word=word.strip() #delete whitespace
if word==word[::-1]: #str[::-1] is used to reverse a
string
output.append(word) #store string if it is a
palindrome
print("Palindromes found in the text file are:")
print(output) #show found palindromes in
the file
Document Page
Demonstration
Figure 1: Palindrome program showing output
Samples:
Word (Input) Is it a palindrome Actual output Comment
ELLE Yes True Passed
sagas Yes True Passed
conducibly No False Passed
murdrum Yes True Passed
hallah Yes True Passed
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
Output and discussion
The created program reads strings from the file one by one and then reverses them and
compares them with the original string. The words are separated by each line in the text file
and are picked up by the program using the strip() function as it returns the string with
preceding and succeeding characters deleted. With this approach, the correct output is
produced and all the test cases are passed. The strings are reversed using slice method [::-1]
which actually starts picking characters from the string by last index value and hence the
string gets reversed.
Document Page
Anagrams
Algorithm in pseudo-code
1. A list is created to store words whose anagrams have been found and must not be checked
again to avoid redundancy.
2. Strings picked from the file one by one in a loop and checked if they exist in the above
created list.
If (string not found in anagram list):
Search for its anagrams
Read file and pick-up string again
Compare the word with all others except the word itself to find its anagrams
Sort the characters in the strings and compare them
3. If the comparison returns true, add the anagrams to the output and print.
4. Add the word itself and the anagrams to the list with which redundancy is controlled.
Implementation in code
def check(string1, string2):
#Function to compare strings
#and check if they are equal
cmp1 = sorted(string1)
cmp2 = sorted(string2)
if cmp1 == cmp2:
return True
else:
return False
dlt = []
#list to store words whose anagrams have been found already
Document Page
def search_anag(wrd,dlt):
#function to find anagrams
anagram_list = []
#list to store anagrams
strings = open('English.txt')
#reading strings from text file
for k in strings:
k = k.strip()
if wrd!=k:
temp = check(wrd, k)
if temp:
anagram_list.append(k)
dlt.append(wrd)
dlt.append(k)
strings.close()
return anagram_list
file = open('English.txt')
print ("Kindly wait for the anagrams to load...")
for words in file:
words = words.strip()
if not words in dlt:
found = search_anag(words,dlt)
if found !=[]:
print("Anagrams of "+words+" found in the file
are: ") #final output
print(found)
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
Demonstration
Figure 2: Anagram program showing output
Samples:
Word (Input) Has anagrams in
the file
Actual output Comment
abelmosk Yes smokables Passed
abend Yes bande, baned Passed
rebar Yes barer, aberr Passed
A.D. Yes D.A. Passed
diabases Yes abaissed Passed
Output and discussion
The program produces all the anagrams of the picked strings and avoids redundancy by not
comparing them with one another once again. The program reads the strings from the text file
and picks them one by one. It then compares the string with all other strings in the text file
excluding itself by sorting the strings alphabetically. Sorting them alphabetically basically
helps to check if both the strings have the same characters. If the case is true, the palindromes
are passed into a list and produced as output. The results and the words are also stored in
another list which helps to avoid checking them again each time the loop is run for the next
cycle and thus eradicates redundancy.
chevron_up_icon
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]