ITECH1400 Fundamentals of Programming: Palindrome and Anagram Code

Verified

Added on  2022/12/26

|7
|531
|1
Homework Assignment
AI Summary
This assignment solution for ITECH1400 Fundamentals of Programming focuses on implementing palindrome checks and anagram generation using Python. The solution includes pseudocode outlining the steps for both functionalities, followed by the corresponding Python code snippets. The palindrome check reads a text file, identifies palindromes, and prints the count and the identified palindromic lines. The anagram implementation involves reading words from a file, sorting the letters of each word, and identifying anagrams. The code includes functions for reading the file, sorting characters, and finding anagrams. The assignment also provides a screenshot of the program's output, demonstrating the execution of both the palindrome and anagram functionalities.
Document Page
Running head: ITECH1400 FUNDAMENTALS OF PROGRAMMING
ITECH1400 Fundamentals of Programming
Name of the Student
Name of the University
Authors note
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
1ITECH1400 FUNDAMENTALS OF PROGRAMMING
Implementation of Palindrome check from the words of the file
Pseudocode
Step 1: Read file
Step 2: Read all the words or strings in the file.
Step 3: Initialize len variable to zero and a flag= zero
Step 4: While word[len] is not NULL
len++
Initialize “k” to zero , l to len-1
While k < (len/2)+1
If word[i] =word[J]
flag=0
else
Flag=1
Increment i , l Decrement
If flag=0
Print Palindrome
else
Print Not Palindrome
Stop
Document Page
2ITECH1400 FUNDAMENTALS OF PROGRAMMING
Code
with open('English.txt') as text:
count=0
for line in text:
line = line.strip()
lines = line[::-1]
if line == lines:
count+=1
print ('Following line in the text file is a Palindrome!')
print(count)
print(line)
Document Page
3ITECH1400 FUNDAMENTALS OF PROGRAMMING
Implementation of Palindrome check from the words of the file
Pseudocode for the program
Get all the strings in a list
Remove the spaces and convert to lowercase
Read next line as String2.
Remove the spaces in between and convert all in lowercase
STEP 1:
Get one by one letter of the consecutive strings and compare those letters of the next
string
For the previous word and compare with the first character of next word]
STEP 2: count the number of similar words among the two words.
[if string.i[1] = string.j[i]
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
4ITECH1400 FUNDAMENTALS OF PROGRAMMING
add 1 to total string.i[i]
go to next character of the previous string]
STEP 3: check for all character/letters.
7[if total length of string= length(string1)
then return anagram=true
else
anagram check =false]
OUTPUT: returns true or false for check.
Code
def bd_dic(path):
adict = {}
f = open(path, "r")
for line in f.readlines():
line = line.strip()
key = sort_li(line)
if key in adict:
v = adict.get(key) + "," + line
Document Page
5ITECH1400 FUNDAMENTALS OF PROGRAMMING
adict[key] = v
else:
adict[key] = line
return adict
def sort_li(line):
chars = [c for c in line]
chars.sort()
return "".join(chars)
def anagr(adict, line):
key = sort_li(line)
values = adict.get(key, "NONE")
return values.split(",")
adict = bd_dic(r"English.txt")
results = anagr(adict, "noid")
print("Anagrams for [noid]")
Document Page
6ITECH1400 FUNDAMENTALS OF PROGRAMMING
print(results)
Screenshot
Following is the screen shot of the executed program.
chevron_up_icon
1 out of 7
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]