Question

In: Computer Science

In PYTHON: Write a function that receives a sentence and returns the last word of that...

In PYTHON:

Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence.

To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.

Solutions

Expert Solution

Answer : please up vote or comment for any query i will update the same . Thanks

Note : check attached image for output and program .

Program Plan :

  1. declare a function name is getLastWord
  2. split the sentence by space
  3. sentence.split() return list of string
  4. last element of list is end word of sentence
  5. return last word

Program Python 3,7 compiler :

def getLastWord(sentence):
List= sentence.split() #split sentence by space
LastWord = str(List[len(List)-1]) #convert last word in string
return LastWord

lastWord = getLastWord("hello tinku jangid how are you.") #store return word


print ( "Last Word is :",lastWord ) #print last word

Program python 2 compiler


def getLastWord(sentence):
List= sentence.split() #split sentence by space
LastWord = str(List[len(List)-1]) #convert last word in string
return LastWord

lastWord = getLastWord("hello tinku jangid how are you.") #store return word


print "Last Word is :",lastWord #print last word

Output :

Python 2 compiler output & program


Related Solutions

Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a function in python that accepts a sentence as the argument and converts each word...
Write a function in python that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY. Please, I want the coding indented and then the...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Python Write a function str_slice(s) to slice sentence into single word by their spaces and punctuations...
Python Write a function str_slice(s) to slice sentence into single word by their spaces and punctuations and returns a list. You cannot use split(), join() method for this define function Example: >>>str_slice(‘Hi, I like it!!’) [‘Hi’, ‘, ’, 'I', ‘ ‘, ‘like’, ‘ ‘, ‘it’, ‘!!’]
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is a palindrome, and boolean False if it is not. The palindromes for this problem are contained in the list below. You must use this list in your solution. Use a for loop to retrieve each palindrome and test it. Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list....
Python. Create a function that receives the name of an auto maker and returns a slice...
Python. Create a function that receives the name of an auto maker and returns a slice of the dataframe with the cars from that auto maker. For instance, it may receive 'ford' and return a slice with all the cars produced by 'ford' meaning they have 'ford' in their name. Call the function for 'ford' to see if it works properly. Hint: You may create a list comprehension producing a list of all the index labels that include the auto-maker's...
Write a function bracket_by_len that takes a word as an input argument and returns the word...
Write a function bracket_by_len that takes a word as an input argument and returns the word bracketed to indicate its length. Words less than five characters long are bracketed with << >>, words five to ten letters long are bracketed with (* *), and words over ten characters long are bracketed with /+ +/. Your function should require the calling function to provide as the first argument, space for the result, and as the third argument, the amount of space...
write a python program that reads a sentence and identifies a word from an existing glossary...
write a python program that reads a sentence and identifies a word from an existing glossary and states the meaning of the word
python: Write a program that turns a sentence into camel case. The first word is lowercase,...
python: Write a program that turns a sentence into camel case. The first word is lowercase, the rest of the words have their initial letter capitalized, and all of the words are joined together. For example, with the input "fOnt proCESSOR and ParsER", your program will output "fontProcessorAndParser". Optional: can you do this with a list comprehension? Optional extra question: print a warning message if the input will not produce a valid variable name. You don't need to be exhaustive...
7. Write a function that accepts a sentence as the argument and converts each word to...
7. Write a function that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT