Question

(Same-number subsequence) Write an O(n) program in python that prompts the user to enter a sequence...

(Same-number subsequence) Write an O(n) program in python that prompts the user to enter a sequence of integers and finds longest subsequence with the same number. Here is a sample run of the program:

<Output>
Enter a series of numbers ending with 0: 2 4 4 8 8 8 8 2 4 4 0
The longest same number sequence starts at index 3 with 4 values of 8
<End Output>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

nums = []
n = -1
n = int(input("Enter a series of numbers ending with 0: "))
while n != 0:
nums.append(n)
n = int(input("Enter a series of numbers ending with 0: "))
count = 1
maxCount = 0
num = 0
index = 0
for i in range(0, len(nums)-1):
if nums[i] == nums[i+1]:
count = count + 1
else:
index = i
if maxCount < count:
maxCount = count
num = nums[i]
  
count = 1
  
print("The longest same number sequence starts at index "+str(index-maxCount)+" with "+str(maxCount)+" values of "+str(num));

Output:

sh-4.3$ python3 main.py Enter a series of numbers ending with : 2 Enter a series of numbers ending with 0: 4 Enter a series o

Add a comment
Know the answer?
Add Answer to:
(Same-number subsequence) Write an O(n) program in python that prompts the user to enter a sequence...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Same-number subsequence) Write an O(n) program that prompts the user to enter a sequence of integers...

    (Same-number subsequence) Write an O(n) program that prompts the user to enter a sequence of integers in one line and finds longest subsequence with the same number. Sample Run Enter a series of numbers: 2 4 4 8 8 8 8 2 4 4 0 The longest same number sequence starts at index 3 with 4 values of 8. In Python.

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • Program must be in Python 3. Write a program that prompts the user to enter two...

    Program must be in Python 3. Write a program that prompts the user to enter two strings and then displays a message indicating whether or not the first string starts with the second string. Your program must satisfy the following requirements. 1. Your program must include a function called myStartsWith that takes two string parameters. This function returns True if the rst string starts with the second string and returns False otherwise. 2. Your program must include a main function...

  • 1) Write a Python program that prompts the user to enter the current month name and...

    1) Write a Python program that prompts the user to enter the current month name and prints the season for that month. Hint: If the user enters March, the output should be "Spring"; if the user enters June, the output should be "Summer". 2 )Write a Python program using the recursive/loop structure to print out an equilateral triangle below (double spacing and one space between any two adjacent asterisks in the same row).          *      * * * *...

  • write a c program for the above Write a program which prompts the user to enter...

    write a c program for the above Write a program which prompts the user to enter three integers (data type int) and then calculates the arithmetic average as a decimal number and prints the value with two decimal digits. If you have time also calculate and print the harmonic mean of the same numbers according to the following formula: 3. PT harmonic _mean-1 1 22x (Test with integer values of 4, 7, and 8. You may submit one program per...

  • Python Code Write a program using functions and mainline logic which prompts the user to enter...

    Python Code Write a program using functions and mainline logic which prompts the user to enter a number. The number must be at least 5 and at most 20. (In other words, between 5 and 20, inclusive.) The program then generates that number of random integers and stores them in a list. The random integers should range from 0 to 100. (You can use a wider range if you want, but the lower end of the range must be at...

  • Name : StarryArrays Write a program called StarryArrays which prompts user for the number of items...

    Name : StarryArrays Write a program called StarryArrays which prompts user for the number of items in an array (a non-nega- tive integer), and saves it in an int variable called numltems. It then prompts user for the values of all the items (non-negative integers) and saves them in an int array called items. The program shall then print the contents of the array in a graphical form, with the array index and values represented by number of stars For...

  • Write a Python program that prompts the user to enter integer values for each of two...

    Write a Python program that prompts the user to enter integer values for each of two lists. It then should displays whether the lists are of the same length, whether the elements in each list sum to the same value, and whether there are any values that occur in both lists.

  • Design a Python program that prompts the user to enter "yes" or "no" and validates the...

    Design a Python program that prompts the user to enter "yes" or "no" and validates the input. (Use a case-insensitive comparison) and another Python program that prompts the user to enter a number in the range of 1 through 100 and validates the input

  • (PYTHON) Write a program that prompts the user to enter the number of students and each student's score

    USE PYTHON 3Write a program that prompts the user to enter the number of students and each student's score, and displays the highest and second- highest scores.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT