def int_diff(nums: list, n: int):
count=0
for i in range(0,len(nums)):
for j in range(0,len(nums)):
if nums[i]-nums[j]==n:
count+=1
print(count)
return count
def int_diff(nums: list, n: int) → int:
list1=
a=len(nums)
for i in range(0,a):
for j in range(0,a):
if nums[j]-nums[i]==n:
list1.append((nums[i],nums[j]))
c=len(list1)
return c
老师,请问写成下面这样: 为什么符合要求的数据会被打印两遍呢?
def solution(nums: list,n : int)-> int :
m=0
#b=
for i in range(0, len(nums)-1):
for j in range(i,len(nums)):
if nums[j]-nums[i]==n: #b.append(nums[i]) #b.append(nums[j])
print(nums[i],nums[j]) 为什么被打印了两遍呢
m += 1
return m
if name == ‘main’:
a=[1, 1, 5, 6, 9, 16, 27]
n=4
solution(a,4)
print(solution(a, 4))
def int_diff(nums: list, n: int) -> int:
count = 0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if abs(nums[j] - nums[i]) == n:
count+=1
return count
import itertools,math
def int_diff(nums:list,n:int) -> int:
return len([list(itertools.combinations(nums,2))[i] for i in range(len(list(itertools.combinations(nums,2)))) if int(math.fabs(list(itertools.combinations(nums,2))[i][0]-list(itertools.combinations(nums,2))[i][1]))== n])
assert int_diff([1,1,5,6,9,16,27],4)==3
assert int_diff([1,1,3,3],2)==4