In: Computer Science
In Python3 please do the following:
Program MUST take USER INPUT. The examples below are just test cases for you to test the program.
You are given a rare document that we suspect is written in an alien language. The alien language has the following grammar.
All its words read the same backward as well as forward. E.g. aba. A sequence of these words are written as sentences.
It is composed of only English alphanumeric([a-z, A-Z, 0-9]) characters in it.
It has exactly one space between each word.
It is case insensitive. i.e. aba is the same word as abA.
Write a function isAlienWord that takes an input word and returns if it is alien or not. Then, write another function called isAlienLanguage that calls the isAlienWord function for each word of its sentence and returns if the entire document is alien or not. For the document to be alien each and every single word in the document has to alien. True of type bool indicates the document is alien False means otherwise.
Have a main function from which the answer to questions are retrieved and run the program as a whole.
Examples:
Test Case 1:
input: document = ”a ma” output: False
Explanation: Though the string ”ama” is a valid alien language ”a ma” is not.
Test Case 2:
input: document = ”aba mom roor tet jasttsaj pillip”
output: True
Explanation: All the words in the document are alien. Hence, the
language is alien. Return true.
Note: The function isAlienWord must be implemented recursively. Failure to do so will fetch 0 points for this question.
def isAlienWord(word):
for i in range(0,int(len(word)/2)):
if word[i].upper() !=
word[len(word)-i-1].upper():
return
False;
return True;
def isAlienWordRec(word,x,y):
if x > y:
return True
elif word[x] != word[y]:
return False
else :
x = x + 1
y = y - 1
return
isAlienWordRec(word,x,y)
def isAlienLanguage(language):
wordList = language.split(" ");
for word in wordList:
if
isAlienWordRec(word,0,len(word)-1) == False:
return
False;
return True;
lang = input("Enter the document : ");
if isAlienLanguage(lang) :
print("Yes the document is alien")
else :
print("No the document is not alien")
Both iterative and recursive methods are given. If you like the answer please give a thumbs up, it helps and motivates a lot. Feel free to ask in the comment section if any problem arises.