【每日一题20220509】刚好大你

:mage:‍ 给定一个数字列表(无重复)和一个索引值,请编写一个函数,找出刚好比给定索引对应的数字值大一丢丢的数字。 如果存在则返回该数字,如果不存在则返回-1。

【示例】
输入:[4, 1, 3, 5, 6], 0
输出:3
解释:索引0对应数字是4,刚好比4大一丢丢的数字是5

题目难度:简单
题目来源:CodeWars-Least Larger

def solution(nums: list, idx: int)-> int:
    # your code here

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
def solution(nums:list,idx:int)-> int:
    big_nums = [nums[i] for i in range(len(nums)) if nums[i]>nums[idx]]
    if len(big_nums)>0:
        return nums.index(min(big_nums))
    else:
        return -1
if __name__ == '__main__':
    print(solution([4, 1, 3, 5, 6], 0))
    print(solution([4, 1, 3, 5, 6], 4))
    print(solution([1, 3, 5, 2, 4], 0))
    print(solution([1, 3, 5, 7], 0))

“找出刚好比给定索引对应的数字值大一点的数字"中“大一点”有歧义 :rofl:
应该是需要找出"列表中比给定值大的最小数字”

只找nums[idx]+1的数字会有点问题
举个列子:num=[1,3,5,7],idx=0
返回值应该是idx=1,但是按nums[idx]+1的话会找不到

有点没搞定为嘛Kata 随机测试跑不过,回头我再看看。

def solution(nums: list, idx: int)-> int:
    # your code here
    if (n := nums[idx] + 1) in nums:
        return nums.index(n)
    else:
        return -1

image

嗦嘎 感谢学委大大的提醒 感情这不是真的就大一点。

题目描述有歧义,原题是找出比目标值大的最小值

了解 再次感谢学委大大

def solution(nums: list, idx: int) -> int:
   li = []
   for i in nums:
      if i > nums[idx]:
         li.append(i)
   if len(li) == 0:
      return -1
   else:
      return nums.index(min(li))

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3

def solution(nums: list, idx: int) -> int:
    ls2 = [i for i in nums if i > nums[idx]]
    if len(ls2) > 0:
        return nums.index(min(ls2))
    else:
        return -1


if __name__ == '__main__':
    assert solution([4, 1, 3, 5, 6], 0) == 3
    assert solution([4, 1, 3, 5, 6], 4) == -1
    assert solution([1, 3, 5, 2, 4], 0) == 3
def solution(nums: list, idx: int)-> int:
    # your code here
    return -1 if sorted(nums[:]).index(nums[idx]) == len(sorted(nums[:])) - 1 \
        else nums.index(sorted(nums[:])[sorted(nums[:]).index(nums[idx]) + 1])

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
# 由于题目及示例表述问题,此处按照返回索引处理
def solution(nums: list, idx: int)-> int:
    # your code here
    return nums.index(sorted(nums)[sorted(nums).index(nums[idx])+1]) if sorted(nums).index(nums[idx])+1 < len(nums) else -1


assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3

修改后的代码。

def solution(nums: list, idx: int)-> int:
    # your code here
    if len(n := list(filter(lambda x: x > nums[idx], nums))) > 0:
        return nums.index(min(n))
    else:
        return -1

确实,题目有点歧义,以为就是找大1的

def solution(nums: list, idx: int) -> int:
    nums_bak = [i for i in nums]
    nums.sort()
    data = nums_bak[idx]
    if data == max(nums):
        return -1
    else:
        i = nums.index(data)
        result = nums[i+1]
        return nums_bak.index(result)


assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3

题目描述有问题,按照检验,返回的应该是返回值对应的索引值

def solution(nums: list, idx: int)-> int:
    data = nums[idx]
    if max(nums)==data:
        return -1
    return nums.index(min([num for num in nums if num > data]))


assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
def solution(nums: list, idx: int)-> int:
    target = nums[idx]
    litter_big = None
    litter_big_index = -1
    for index,value in enumerate(nums):
        if value>target and (litter_big is None or value < litter_big):
            litter_big = value
            litter_big_index = index
    return litter_big_index
def solution(nums: list, idx: int)-> int:
    num = nums[idx]
    num_diff = [(nums[i],nums[i]-num) for i in range(len(nums)) if nums[i]-num>0]
    num_diff.sort(key=lambda x: x[1])
    return [nums.index(num_diff[0][0]) if len(num_diff) != 0 else -1][0]

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3