”’
Dr. A. Euler
Cass Business School
—————————–
Tested with Python 3.7 and 3.8
”’
import math as m
import random as r
##Q4(a-i)
num_lines = 0
num_tabs = 0
num_spaces = 0
text = “This is\tquestion 4, \npart a(i)\n”
for c in text:
if c == ‘\n’:
num_lines += 1
if c == ‘\t’:
num_tabs += 1
if c == ‘ ‘:
num_spaces += 1
print(“\nBlanks: %d\nTabs: %d\nNewlines: %d\n” %
(num_spaces, num_tabs, num_lines))
print(“”)
##Q4(a-ii)
text = “This is question 4, part a(ii).”
lastc = text[0]
print(“\nInitial text: \n{0:s}\n\t”.format(text))
newtext = []
print(“Final: \t “)
for c in text:
if c == ‘ ‘:
if lastc != ‘ ‘:
newtext.append(c)
print(c, end=”)
else:
newtext.append(c)
print(c, end=”)
lastc=c
print(“\n\nCharacter by character\n{0}\n\t..note the space reduction…”.format(newtext))
##——————————————————————-
##Q4(a-iii)
text = “This is question 4, part a(ii).”
lastc = text[0]
print(“\nInitial text: \n{0:s}\t”.format(text))
print(“\nFinal: \n\t “)
for c in text:
if c == ‘ ‘:
if lastc != ‘ ‘:
print(‘\n’)
else:
print(c, end=”)
lastc=c