In: Computer Science
Python
Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print:
Longest substring in numeric ascending order is: 14578
Average: 5
In the case of ties, print the first substring. For example, if s = '147279', then your program should print
Longest substring in numeric ascending order is: 147
Average: 4
def longest(string):
start=0;end=1;i=0;
while i<len(string):
j=i+1
while j<len(string) and string[j]>string[j-1]:
j+=1
if end-start<j-i:
#update if current string has greater length than
#max start and end
end=j
start=i
i=j;
avg=0
for i in string[start:end]:
avg+=int(i)
print('The longest string in ascending order
is',string[start:end])
print('The average is',avg/(end-start))
s=input('Enter a string')
longest(s)
The output is