In: Computer Science
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"
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 !!!