Write a program that creates a class named Num
to receive a sorted array of numbers without duplicates as an attribute. Create a method sip
(search insert position) to receive a target value, the method return the index if the target is found in the array. If not, return the index where it would be if it were inserted in order. The following shows the execution of the function. (Do not use any build-in function.)
x=Num((1,2,5,8,13,19))
print(x.sip(5))
#output
2
----------------------------------------------------------------------------
x=Num((1,2,5,8,13,19))
print(x.sip(15))
#output
5
class Num:
def __init__(self, li):
self.list = li
def sip(self, n):
index = 0
for v in self.list:
if v>=n:
break
index += 1
return index
x=Num((1,2,5,8,13,19))
print(x.sip(5))
x=Num((1,2,5,8,13,19))
print(x.sip(15))
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y