Question

In: Computer Science

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’, ‘!!’]

Solutions

Expert Solution

PYTHON CODE

#function str_slice(s) to slice sentence into single word
#by their spaces and punctuations
#returns a list
def str_slice(s):
    ind = 0#starting index
    str_array = []#string sliced and stored in str_array
    space = ' '#space character
    punctuations = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"#punctuations characters
    while ind < len(s):
        tempStr = ''#store slicing string
        if s[ind] not in space and s[ind] not in punctuations:#if the character is not space or punctuations, then sliced separately
            while ind < len(s) and s[ind] not in space and s[ind] not in punctuations:
                tempStr += s[ind]
                ind += 1
        elif s[ind] in space:#orelse if it is space, then sliced separately
            while ind <len(s) and s[ind] in space:
                tempStr += s[ind]
                ind += 1
        elif s[ind] in punctuations:#if it is any punctuation, then sliced separately
            while ind <len(s) and s[ind] in punctuations:
                tempStr += s[ind]
                ind += 1
        str_array.append(tempStr)#add sliced string to the array
    return str_array#return array

PYTHON CODE SCREENSHOT

OUTPUT SCREENSHOT


Related Solutions

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.
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...
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.
write a program in c language to count the number of spaces ina sentence .the sentence...
write a program in c language to count the number of spaces ina sentence .the sentence is saved in the string 'sentence' write it in a repl.it and along with input,output and algorithm
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...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word representing a word in English, and returns the word translated into Pig Latin. Pig Latin is a “language” in which English words are translated according to the following rules: For any word that begins with one or more consonants: move the consonants to the end of the word and append the string ‘ay’. For all other words, append the string ‘way’ to the end....
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT