【每日一题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())

from functools import reduce


def solution(nums: list, target: range) -> int:
	result=[]
	tmp_s=[]
	##得出nums里任意两个数组成嵌套列表
	for i in range(0,len(nums)):
		for j in range(1,len(nums)):
			if nums[i]!=nums[j] and i<j:
				result.append([nums[i],nums[j]])

	#如果target里的数与嵌套数组相等,则得出新的数组
	for i in range(len(result)):
		for j in range(target.start,target.stop):
			if result[i][0]+result[i][1]==j:
				tmp_s.append(j)

	sum= reduce(lambda x, y: x + y, tmp_s)
	return sum
assert solution([2, 4, 6, 10], range(6, 10)) == 14
def isadd(nums: list, num: int) -> bool:
    for i in range(len(nums)):
        if num - nums[i] in nums[i:]:
            return True
        else:
            return False


def solution(nums: list, target: range) -> int:
    lresult = []
    for i in target:
        if isadd(nums, i) == True:
            lresult.append(i)
    return sum(lresult)
def solution(nums: list, target: range) -> int:
    res_list = []
    for i in nums:
        for j in nums:
            if nums.index(i) >= nums.index(j):
                continue
            if i + j in target:
                res_list.append(i + j)
    return sum(res_list)


assert solution([2, 4, 6, 10], range(6, 10)) == 14
def solution(nums: list, target: range) -> int:
    res_list=[]
    nums.sort()
    for i in target:
        left = 0
        while left<len(nums) and nums[left]<i:
            right=left+1
            while right<len(nums) and nums[right]<i:
                if nums[left]+nums[right]==i:
                    res_list.append(i)
                    break
                else:
                    right+=1
            left+=1
    return sum(res_list)

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