Question

In: Computer Science

Python Assume s is a string of numbers. Write a program that prints the longest substring...

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

Solutions

Expert Solution

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


Related Solutions

In python write a program which prints the longest substring of numbers which occur in ascending...
In python write a program which prints the longest substring of numbers which occur in ascending order s=342153476757561235
Write a Python loop that goes through the list and prints each string where the string...
Write a Python loop that goes through the list and prints each string where the string length is three or more and the first and last characters of the strings are the same. Test your code on the following three versions of the list examples: examples = ['abab', 'xyz', 'aa', 'x', 'bcb'] examples = ['', 'x', 'xy', 'xyx', 'xx'] examples = ['aaa', 'be', 'abc', 'hello'].
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT