Question

In: Computer Science

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

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

I need python code for this one?

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

def satisfies(num):
for i in range(1,len(num)):
if num[i-1] > num[i]:
return False
return True
  
def find_longest_substr(s):
#Initialise the longest substring as ''
longest_substr=''
  
#Loop through each substring
for i in range(len(s)):
for j in range(i,len(s)):
#Get the substring
sub_str = s[i:j]
  
#If satisfies and larger, then it is longest_substr
if satisfies(sub_str) and len(longest_substr)<len(sub_str):
longest_substr = sub_str
  
sum_of_numbers =sum(int(x) for x in longest_substr)
  
return longest_substr, sum_of_numbers/len(longest_substr)
  
#TEST 1   
s= '561984235272145785310'
longest_substr, average = find_longest_substr(s)
print('Longest Increasing Substring: ',longest_substr)
print('Average of digits: ',average)
  
#TEST 2   
s= '147279'
longest_substr, average = find_longest_substr(s)
print('Longest Increasing Substring: ',longest_substr)
print('Average of digits: ',average)

================================

SCREENSHOT:


Related Solutions

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...
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 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 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...
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
Question 1 A substring of String s is a sequence of k >= 0 characters in...
Question 1 A substring of String s is a sequence of k >= 0 characters in s, in the order in which they occur in s. The letters in a substring may be either contiguous (next to each other) or non-contiguous in the original String s. For instance, these are the substrings of String s="abc". String s All substrings of s ======== ================================== "abc" "" "a" "b" "ab" "c" "ac" "bc" "abc" Write a function allSubstrings that returns an ArrayList...
(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).
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT