76 412
Assignments Done
Successfully Done
In May 2019

Answer to Question #52372 in Python for Oz

Question #52372
Assume that you are given a possibly empty list (call it mylist) of unique numbers in ascending order and then given a number (call it num). If the input is a number appearing in the list, print that number. Otherwise, print the smallest number that is greater than the given number. You may assume that the given number num is not greater than the largest number in the list.

Suppose mylist is

[0, 10, 15, 17, 23, 35, 46, 70, 75, 80, 90, 100]

If num is -10, print 0
If num is 18, print 23
If num is 80, print 80
If num is 99, print 100
num is not more than 100.

If mylist is empty, print “Empty list.”

Code your program to accept the list of numbers separated by commas. Then accept num.
Expert's answer
# read a line with list of numbers
line = input("Enter a list of numbers in ascending order, separated by commas: ")
# create a list of numbers
mylist = []
splitted = line.split(',')
if len(splitted) > 0 and len(splitted[0].strip()) > 0:
for num in splitted:
# entering a num and analyse a list
num = int(input("Enter a number: "))
if len(mylist) == 0:
print("Empty list.")
i = 0
while i < len(mylist) and mylist[i] < num:
i += 1
if i == len(mylist):
print("num is more than " + str(mylist[-1]))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions