ITECH1400: Assignment 1 - Palindrome and Anagram Algorithms

Verified

Added on  2025/05/01

|8
|669
|314
AI Summary
Desklib provides past papers and solved assignments for students. This solved assignment covers palindrome and anagram detection.
Document Page
ITECH1400
Fundamentals of Programming
Assignment 1 – Palindromes and Anagrams
Student Name:
Student ID:
Page 1 of 8
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
Part 1: Palindromes....................................................................................................................3
Algorithm in Pseudo-code......................................................................................................3
Implementation of algorithm in code.....................................................................................4
Demonstration........................................................................................................................5
Correct Output and Discussion..............................................................................................5
Part 2: Anagrams........................................................................................................................6
Algorithm in pseudo-code......................................................................................................6
Implementation of algorithm in code.....................................................................................7
Demonstration........................................................................................................................8
Correct Output and Discussion..............................................................................................8
Page 2 of 8
Document Page
Part 1: Palindromes
Algorithm in Pseudo-code
Opening the given text file using open() function
Passing the values in the “textfile” as a string variable
enumerating & lopping the text file words with numbers
Inside the loop checking if the current word is a palindrome or not.
Creating a function named palindrome_check and passing parameter of the word to be
checked.
Using a loop from the right hand side to the left hand side which will check each
place
If the letter is not same as the reverse positioned letter then the function returns false
If the letter is same then it returns true
Checked palindrome with a Boolean true is printed.
End of program
Page 3 of 8
Document Page
Implementation of algorithm in code
def Palindrome_Check(tf_word):
lhs = 0
rhs = len(tf_word)-1
while rhs>=lhs:
if not tf_word[lhs]==tf_word[rhs]:
return False
lhs+=1
rhs-=1
return True
print("\nThe Palindromes are listed below.\n")
text_file=open('English.txt')
for n,l in enumerate(text_file, 1):
if Palindrome_Check(l.strip()):
print(l.strip())
Page 4 of 8
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
Correct Output and Discussion
Sr. No. Word Is a Palindrome Actual Output Comment
1 alula Yes True Pass
2 B/B Yes True Pass
3 Abiathar No False Pass
4 Able No False Pass
5 Cat No False Pass
6 Catacomb No False Pass
7 mum Yes True Pass
8 POP Yes True Pass
9 SIS Yes True Pass
10 System No False Pass
Page 5 of 8
Document Page
Part 2: Anagrams
Algorithm in pseudo-code
Creating a function for checking if two words are anagrams of each other.
o Parameters word1 and word2
o For this function, first using sorted function on both the words
o Then, storing the sorted words in other strings.
o Comparing the two strings,
If each and every letter is equal in both strings then return true
If not equal return false
Creating a function for finding the anagrams of the word
o Parameters string s and list anagrams_used
o The list anagrams_used will store the anagrams used and then prevent
repeating of the words in the output
o In this function opening the file and then storing file data into the list variable.
o Then reading file words and comparing it with the parameter s if it’s equal or
not. This will prevent repetition of the same anagram check
o Now, storing value returned by the anagram check function and if the result is
true, found_anagrams get appended with the word from the text file and
anagrams_used also gets appended with both word and the string
o Textfile closed.
o Returned anagrams found
Opening the text file and then running a loop.
o Each word gets stripped of spaces.
o If the word is not in the anagrams_used list, it will be passed as an argument in
the anagrams_found function.
o Then the anagrams found function value is returned and printed on the console
o Anagrams are displayed for each word
End of program
Page 6 of 8
Document Page
Implementation of algorithm in code
def Anagrams_Found(s,anagrams_used):
found_anagrams = []
textfile = open('English.txt')
for w in textfile:
w = w.strip()
if s!=w: result = Anagram_Check(s, w)
if result:
found_anagrams.append(w)
anagrams_used.append(s)
anagrams_used.append(w)
textfile.close()
return found_anagrams
def Anagram_Check(word1, word2):
w1 = sorted(word1)
w2 = sorted(word2)
if w1==w2: return True
else: return False
print("\nPlease wait, the anagrams will be listed shortly!")
anagrams_used = []
text_file = open('English.txt')
for one_word in text_file:
one_word = one_word.strip()
if not one_word in anagrams_used:
r = Anagrams_Found(one_word,anagrams_used)
if r!=[]:
ag = ""
for i in r:
ag = ag + i + " "
print("Anagram of "+one_word+"--> "+ag)
Page 7 of 8
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
Correct Output and Discussion
Sr. No. Word Having anagram Actual Output Comment
1 ahead Yes True Pass
2 abaca Yes True Pass
3 10-Point No False Pass
4 10th No False Pass
5 1st No False Pass
6 acrobat Yes True Pass
7 Botanica Yes True Pass
8 30-30 No False Pass
9 2D No False Pass
10 A.B.A. Yes True Pass
Page 8 of 8
chevron_up_icon
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]