Question

Implement a function printEveryOtherWord () that takes in a string representing a paragraph with multiple words...

Implement a function printEveryOtherWord () that takes in a string representing a paragraph with multiple words separated by a space. The function should extract each word from the paragraph and print every other word found in the paragraph on its own line. print('Starting printEveryOtherWord tests:') printEveryOtherWord('Hello World') printEveryOtherWord('This is a test of the function') printEveryOtherWord('Print every other word')

for python.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.



def printEveryOtherWord(paragraph):
    # split all the lines into a list
    lines = paragraph.split('\n')

    # traverse through each line
    for line in lines:
        # take words in the line (sentence)
        words = line.split()

        # start with first word and repeat till last word, (STEP is 2=> every other word)
        for i in range(0, len(words), 2):
            print(words[i], end=' ')
        # print a new line
        print()


# test cases here
print('Starting printEveryOtherWord tests:')
printEveryOtherWord('Hello World')
printEveryOtherWord('This is a test of the function')
printEveryOtherWord('Print every other word')

# testing with multiple lines
paragraph = '''
Hai 
I am Line-1
I am Line-2
I am Line-3
'''
printEveryOtherWord(paragraph)

=========

Add a comment
Know the answer?
Add Answer to:
Implement a function printEveryOtherWord () that takes in a string representing a paragraph with multiple words...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can...

    IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can prints the number of words in the string. You can assume that words will only be separated with spaces, commas, and periods. "hello, world" -> 2 "this is cool" -> 3 4) Is Sorted Write a function isSorted() which takes in an list of integers and returns true if the numbers are sorted from smallest to largest.

  • camelCase(text) This function accepts a string assumed to consist of multiple words separated by spaces, and...

    camelCase(text) This function accepts a string assumed to consist of multiple words separated by spaces, and returns a single "camel cased" word. In other words, all spaces are removed, and all words other than the first word are capitalized. Code Python

  • IN PYTHON Implement a function easyCrypto() that takes as input a string and prints its encryption...

    IN PYTHON Implement a function easyCrypto() that takes as input a string and prints its encryption defined as follows: Every character at an odd position i in the alphabet will be encrypted with the character at position i + 1, and every character at an even position i will be encrypted with the character at position i - 1. In other words, ‘a’ is encrypted with ‘b’, ‘b’ with ‘a’, ‘c’ with ‘d’, ‘d’, with ‘c’, and so on. Lowercase...

  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • CODE: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as...

    CODE: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the word if it is part of a larger word. For example,...

  • In Python: LoadFile is a function that takes in a string (a filename) and then returns...

    In Python: LoadFile is a function that takes in a string (a filename) and then returns a list. The list is the contents of the file, where each element is a list of data from the file. Here's an example of using this function. The input file had four lines of text. >>> lines = LoadFile("test.txt") >>> print("OUTPUT", lines) OUTPUT ["Hello there", "I am a test file", "please load me in and print me out", "Thanks"]

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • bool process_words(map_type&, string); Takes in an empty map and a string representing a file name. The...

    bool process_words(map_type&, string); Takes in an empty map and a string representing a file name. The function opens the file, reads the file one line at a time, splits the line, cleans each word and then records it in the map where the key of the map is the string and the value of the map is how many times that string occurred. Error: if the file represented by the string cannot be opened, the function returns false and makes...

  • write a program in C Write a program to implement the following requirement: The program will...

    write a program in C Write a program to implement the following requirement: The program will read from standard input any text up to 10, 900, characters and store each unique word (any string that does not contain any whitespace) into a node of a linked list, following the following Node struct: struct NODE { char *word; struct NODE * prev; Note that each node must store a unique word, i.e., no word is stored in more than one node....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT