ITECH1400 Fundamentals of Programming: Palindromes and Anagrams

Verified

Added on  2025/05/01

|9
|667
|205
AI Summary
Desklib provides past papers and solved assignments for students. This assignment focuses on palindrome and anagram detection algorithms.
Document Page
ITECH1400
Fundamentals of Programming
Assignment 1 – Palindromes and Anagrams
Student Name:
Student ID:
1
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
References..................................................................................................................................9
2
Document Page
Part 1: Palindromes
Algorithm in Pseudo-code
The text file which was provided will get opened by the function named open()
Now, all the values of the file will be treated as one string variable
The words will be separated by the use of the split() function.
starting a loop with numbering the text file strings
In the loop there is a condition for the present word being checked for palindrome
A function word_palindrome is created with 1 parameter as the word which will be
checked.
Starting from the beginning till the end, from first to the last letter will be checked with a
loop
If the formation of letter is not same in the backward as well as forward direction then the
result will be false.
If the formation of letters is same both the ways then the result will be true.
After checking the words, the program prints the palindrome words.
Finished.
3
Document Page
Implementation of algorithm in code
def word_palindrome(wrd):
last=len(wrd)-1
first=0
while last>=first:
if not wrd[first]==wrd[last]:
return False
last-=1
first+=1
return True
print("\nThe list of all the palindromes in the file are listed.\n")
tfile=open('English.txt')
for x,y in enumerate(tfile, 1):
if word_palindrome(y.strip()):
print(y.strip())
4
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 level Yes True Pass
2 divid Yes True Pass
3 madam Yes True Pass
4 18-point No False Pass
5 aarrgh No False Pass
6 complexional No False Pass
7 halalah Yes True Pass
8 sexes Yes True Pass
9 SMS Yes True Pass
10 hemidactylous No False Pass
5
Document Page
Part 2: Anagrams
Algorithm in pseudo-code
Anagramss(s,k)
o Parameters string s & list k
o Opening the file given with the open() function
o Checking the words and getting them compared with the parameter m.
o If the words are equal, result will be true else false
o Now inserting the word which is checked by the function in the k list and then
if the result comes out to be true then the word found will be appended to the k
list.
o Closing the file and then returning list of anagrams.
find_anagram(w1, w2)
o Parameters w1 & w2
o Firstly sorting the two given words with the use of sorted() function
o Comparison of the two sorted strings,
Both strings equal so, result will be true
Both strings not equal, result will be false
The text file is opened and then loop runs for each word
o Every word in the text file is separated with the use of the split() function
o Checking if the word is in the k list and if its not present in the k list then the
word will be considered as an argument for the anagramss(m.k) function
o Then anagrams are printed on the console
o For every word anagram is printed.
Finished.
6
Document Page
Implementation of algorithm in code
def find_anagram(w1, w2):
z1 = sorted(w1)
z2 = sorted(w2)
if z1==z2:
return True
else:
return False
def Anagramss(m,k):
tfile = open('English.txt')
anagram_list = []
for i in tfile:
i = i.strip()
if m!=i:
result = find_anagram(m, i)
if result:
anagram_list.append(i)
k.append(m)
k.append(i)
tfile.close()
return anagram_list
print("\nAnagrams will take time to print! Please wait...")
t_file = open('English.txt')
k = []
for u in t_file:
u = u.strip()
if not u in k:
r = Anagramss(u,k)
if r!=[]:
print(r);
7
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 AMA Yes True Pass
2 arona Yes True Pass
3 badan Yes False Pass
4 maraschinos No False Pass
5 hemiparanesthesia No False Pass
6 AUA Yes True Pass
7 ahead Yes True Pass
8 hemiparanesthesia No False Pass
9 Maorilander No False Pass
10 cabasa Yes True Pass
8
Document Page
References
Guzdial, M. and Ericson, B., 2016. Introduction to computing and programming in python.
Pearson.
Ngo, A., 2017. Introduction to Python Programming: Beginner to Advanced, Practical Guide,
Tips and Tricks, Easy and Comprehensive.
9
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]