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.
1
Expert's answer
2015-05-01T12:56:51-0400
# 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(',')
print(splitted)
if len(splitted) > 0 and len(splitted[0].strip()) > 0:
for num in splitted:
mylist.append(int(num))
# entering a num and analyse a list
num = int(input("Enter a number: "))
if len(mylist) == 0:
print("Empty list.")
else:
i = 0
while i < len(mylist) and mylist[i] < num:
i += 1
if i == len(mylist):
print("num is more than " + str(mylist[-1]))
else:
print(mylist[i])

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS