Question

In: Computer Science

Python Practice Sample:   Write code to replace every occurrence of THE or the with ### and...

Python Practice Sample:  

Write code to replace every occurrence of THE or the with ### and every word ending with the letter s to end with a $. Print the resulting text four words per line (and any remaining words from each paragraph on the last one of each paragraph)

"The modern business world goes way beyond the balance sheet. Whether your passion is finance or fashion, economics or the environment, you need an education built for business. At Bentley, we understand this. Sure, we started as an accounting school. But over the past 100 years, the business world has changed and we have too. Today, your classmates are dreaming of Silicon Valley or Washington D.C., just as often as Wall Street"

Solutions

Expert Solution

Python Program:

import re
para = "The modern business world goes way beyond the balance sheet. Whether your passion is finance or fashion, economics or the environment, you need an education built for business. At Bentley, we understand this. Sure, we started as an accounting school. But over the past 100 years, the business world has changed and we have too. Today, your classmates are dreaming of Silicon Valley or Washington D.C., just as often as Wall Street"

words = re.findall(r'[\w]+', para)
final_words = []
for i in words:
    if i.endswith("s"):
        j = i[:-1] + i[-1].replace("s", "$")  # replace ends with 's' by ends with '$'
        final_words.append(j)
    else:
        final_words.append(i)
        
for i in range(0, len(final_words), 4): # printing 4 words per line
    print(" ".join(final_words[i:i+4]).replace("The", "###").replace("the", "###")) # replace "The", "the" by "###"

Output:

Thumbs Up Please !!!


Related Solutions

4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 #...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 # Ask user for a number using an input statement (no prompt) & assign the input to a variable # Convert the user entry to an integer and assign it to a variable # Condition: user entry is greater than zero # Action: display the following word on screen: positive # Exercise 2 # Ask user for a number using an input statement (no prompt)...
Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m (sample mean) and s (sample standard deviation) from the sample.
Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m and s from the sample. Do the same using the Bayes’ estimator assuming a prior distribution for μ.
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask the user to input a name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the user input on screen #Exercise 2 #Ask the user to input his/her first name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the...
PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every...
PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every 6th number down to 300 # (i.e., prints 500, 494, 488, . . . etc., but does not print any number lower than 300). # Write one while-loop that starts at 80 and prints every 12h number thereafter, # but does not print any number greater than 210 # Write one while-loop that prints all the numbers from 30 through 70, # except for...
Python 3.7: Give the following sample code, write the following programs: • An iterator class called...
Python 3.7: Give the following sample code, write the following programs: • An iterator class called Iteratefirstn that implements __iter__() and __next__() methods where next method should raise an exception for the cur index if it is passed the last element otherwise set cur index to the next index and advance the index by one, i.e. cur, num = num, num+1. Instantiate the class to calculate the sum of 1000000 elements similar to the example. • Instead of implementing the...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This assignment will give you experience on the use of the while loop and the for loop. You will use both selection (if) and repetition (while, for) in this assignment. Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number...
Write a code to ask for a vector with 10 elements from the user and replace...
Write a code to ask for a vector with 10 elements from the user and replace the 1st, 3rd, 8th  to 9th  elements with the second element of the given vector. (you can insert a vector when you use input function)
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT