【每日一题20220225】两段求和

:man_mage: 已知一个包含唯一数字的列表,和一个指定的左闭右开区间[from, to),我们的任务是:首先凑对,找出存在几对元素之和在区间内,然后将这些符合区间的和加总得到最终的和值。

备注:

  1. 所有凑对的元素必须是唯一的
  2. 凑对的和值只在区间内统计一次

示例:
已知列表是 [2, 4, 6, 10],已知区间是[6, 10)

6:可以等于 2 + 4
7:无法凑对
8:可以等于 2 + 6
9:无法凑对

最终结果:6 + 8 = 14

题目难度:中等
题目来源:CodeWars: 2-Sum Sums

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

assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    # your code here
    def solution1(nums: list, num: int) -> bool:
        for i in range(0, len(nums) - 1):
            for j in range(i, len(nums)):
                if nums[i] + nums[j] == num:
                    return True
        return False

    num = 0
    for i in target:
        if solution1(nums, i):
            num += i
    return num


assert solution([2, 4, 6, 10], range(6, 10)) == 14



# 优化
def solution(nums: list, target: range) -> int:
    num = []
    for i in range(0, len(nums) - 1):
        for j in range(i, len(nums)):
            num.append(nums[i] + nums[j])
    num1 = 0
    num = list(set(num))
    for j in target:
        if j in num:
            num1 += j
    return num1

assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    add_list = []
    for i in range(len(nums) - 1):
        for j in range(i+1, len(nums)):
            add_list.append(nums[i]+nums[j])

    add_list = list(set(add_list))
    new_list = []
    for i in add_list:
        if i in target:
            new_list.append(i)

    new_list = list(set(new_list))
    num = 0
    for i in new_list:
        num = num + i

    return num

assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    s = 0
    for i in target:
        for n, v in enumerate(nuns):
            for k in nums[n + 1:]:
                if i == k + v:
                    s += i
                    break
    return s


assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    res_list = []
    tmp_list = []
    for i in range(len(nums)-1):
        for j in range(1,len(nums)):
            new = nums[i] + nums[j]
            if new not in tmp_list and new in target:
                tmp_list.append(new)
                res_list.append(new)
    print(sum(res_list))
    return sum(res_list)

assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    nums_list = []
    for i in target:
        for j in nums:
            if (i - j) in nums:
                nums_list.append(i)
                break
    return sum(nums_list)
def solution(nums: list, target: range) -> int:
    # your code here
    res = 0
    for target_i in target:
        for cur_j in nums:
            dest_j = target_i - cur_j
            if dest_j in nums:
                res += target_i
                break
    return res

assert solution([4, 2, 6, 8], range(6, 11)) == 24
def solution(nums: list, target: range) -> int:
    s = []
    for i in range(len(nums)):
        for j in range(i+1,len(nums)):
            if sum((nums[i],nums[j])) in target:
                s.append(sum((nums[i],nums[j])))
    return sum(list(set(s)))

assert solution([2, 4, 6, 10], range(6, 10)) == 14
assert solution([2, 6, 3, 5], range(6, 11)) == 24
def solution(nums: list, target: range) -> int:
    result = []
    for i in range(len(nums) - 1):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] in target:
                result.append(nums[i] + nums[j])
    return sum(set(result))


assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    # your code here
    list_t = [i for i in target]
    list_num = []
    for j in range(len(nums)-1):
        for o in nums[j+1:]:
            num_int = nums[j] + o
            print(num_int)
            if num_int in list_t:
                list_num.append(num_int)
    return sum(list_num)


assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
   sm = 0
   for i in range(0, len(nums)-1):
      for j in range(i, len(nums)):
         if (nums[i]+nums[j]) in target and nums[i] != nums[j]:
            sm = sm + (nums[i]+nums[j])
   return sm

assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    n = 0
    target_result = [i for i in target]
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] in target_result:
                n += nums[i] + nums[j]
    return n
def solution(nums: list, target: range) -> int:
    if isinstance(target,int):
        targets = []
        targets.append(target)
    else:
        targets = [i for i in target]
    result = [(nums[i]+nums[j]) for i in range(len(nums)) for j in range(i+1,len(nums)) if (nums[i]+nums[j]) in targets]
    return sum(result)


assert solution([2, 4, 6, 10], range(6, 10)) == 14
assert solution([1,2,3,4], 7) == 7
def solution(nums: list, target: range) -> int:
    target= [i for i in target]
    dicts ={}
    for i in range(len(nums)):
        for j in nums[i+1:]:
            if  target[0]<=nums[i] +j<=target[-1] and not dicts.get(nums[i] +j):
                dicts[nums[i] + j]=True
            elif target[-1]<nums[i] +j:
                break
    return sum(dicts.keys())

关闭