Question

In: Computer Science

In Python3 please do the following: Program MUST take USER INPUT. The examples below are just...

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.

  1. All its words read the same backward as well as forward. E.g. aba. A sequence of these words are written as sentences.

  2. It is composed of only English alphanumeric([a-z, A-Z, 0-9]) characters in it.

  3. It has exactly one space between each word.

  4. 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.

Solutions

Expert Solution

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.


Related Solutions

JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Write a program that will read user input, and do the following: 1. The user can...
Write a program that will read user input, and do the following: 1. The user can input letters [A-Z] as much as he wants (ignore case). 2. If the user input other than letters or two characters, stop the input process and start to print unduplicated sorted pairs such as the below examples: User input: A a e b d d D E a B 1 Output: AB AD AE BD BE DE User Input: a q w e dd...
5. Take user input and give corresponding output. User will enter a sentence. The program will...
5. Take user input and give corresponding output. User will enter a sentence. The program will output the word that appears most frequently in this sentence. If there are multiple words with same frequency, output the first of these words. Please enter a sentence: I like batman because batman saved the city many times. The most frequent word is: batman The frequency is: 2 PYTHON
2. Write a program to do the following: • ask the user to input the size...
2. Write a program to do the following: • ask the user to input the size of an array of integers and allocate space for the array • ask the user to input the integer values for the array and store these values into the array • calculate and display the sum and the average of elements of the array
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
This needs to be a python3 code Write a program that prompts the user like this:...
This needs to be a python3 code Write a program that prompts the user like this: “Currency to convert to U.S. dollars: e = Euros, c= Chinese Yuan, r = Indian Rupees, b = Bitcoin: ”. Then depending on which letter the user enters, the program displays “Amount of Euros/Yuan/Rupees/Bitcoin to convert: ”. (Note: the second prompt should only name the one currency the user asked to convert, not all four currencies.) After the user enters the amount, the program...
Program using java Take user input and give corresponding output. A user is considering different options...
Program using java Take user input and give corresponding output. A user is considering different options of operating air conditioning. Depending on room temperature (here, room temperature is given by user), this program should give different instructions. There are three scenarios. - If temperature is above 90, the program should output “cooling”. -If the temperature is below 70, the program should output “heating”. -Otherwise, the program should output “stopped”. For example, if user enters “95”, this is how the program...
Write an error-free Java program to do the following things. Prompt the user to input a...
Write an error-free Java program to do the following things. Prompt the user to input a set of numbers. The numbers represent hourly wages so they will be between 7.25 (peon) and 50 (big boss). The user should be able to input up to 35 numbers but if the user enters 0 then the data input ceases. All of the data that the user enters should be stored in a single array. You do not need to check the input...
Need this program in python. The data must be taken from user as input. Write a...
Need this program in python. The data must be taken from user as input. Write a program that prompts the user to select either Miles-to-Kilometers or Kilometers-to-Miles, then asks the user to enter the distance they wish to convert. The conversion formula is: Miles = Kilometers X 0.6214 Kilometers = Miles / 0.6214 Write two functions that each accept a distance as an argument, one that converts from Miles-to-Kilometers and another that converts from Kilometers-to-Miles The conversion MUST be done...
This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT