Python递归,非递归函数,列表生成式

img

img


第一题和第三题,希望尽快,半小时内,最好带代码,谢谢,正规用途,不涉及


x = int(input(''))
if x > 0:
  print("y=",1)
elif x == 0:
  print("y=",0)
else:
  print("y=",-1)

第一题的列表生成式没看懂,
和列表没关系哇

x = eval(input())
y = -1 if x<0 else int(bool(x))
print(y)

第二题的三个问题代码如下:

def search(a:list,key:str):
    for i in range(len(a)):
        if a[i]==key:
            return i
    return -1

def research(a:list,key:str,left:int,right:int):
    if left>right:return -1
    if a[left]==key:return left
    else: return research(a,key,left+1,right)

a = [34,56,78,87,88,90,101,112,520,888]
key = int(input())
res1 = search(a,key)
print("not found") if res1==-1 else print(res1)
res2 = research(a,key,0,len(a))
print("not found") if res2==-1 else print(res2)

百度最快

百度额