def is_nice(nums: list) -> bool:
nums.sort()
for i in range(len(nums)):
min_index = i - 1 if i > 0 else i
next_index = i + 1 if i < len(nums) - 1 else i
min_num, num, next_num = nums[min_index], nums[i], nums[next_index]
if not (min_num + 1 == num or num + 1 == next_num):
return False
return True
def is_nice(nums: list) -> bool:
for n in nums:
if (n + 1 in nums) or (n - 1 in nums):
...
else:
return False
return True
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums):
count = 0
for num in nums:
num1 = num - 1
num2 = num + 1
if num1 in nums or num2 in nums:
count += 1
continue
else:
break
if count == len(nums):
return True
return False
assert is_nice([2,10,9,3]) is True
assert is_nice([3,4,5,7]) is False
def is_nice(arg):
for i in arg:
if (i - 1 in arg) or (i + 1 in arg):
pass
else:
return False
return True
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
return all([i+1 in nums or i-1 in nums for i in nums])
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
for item in nums:
if (item -1) in nums or (item +1) in nums:
flag = 1
else:
flag = 0
return True if flag else False
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
for i in nums:
a=int(i)-1
b=int(i)+1
if a in nums or b in nums:
pass
else:
return False
return True
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
for n in nums:
nums2 = [x for x in nums]
nums2.remove(n)
for x in nums2:
if n == x + 1 or n == x - 1:
break
else:
return False
# 列表中所有数字都符合则为完美列表,返回 True
return True
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
for num in nums:
if num + 1 not in nums and num - 1 not in nums:
return False
return True
is_nice([3, 4, 5, 7])
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False
def is_nice(nums: list) -> bool:
x = True
for i in nums:
if i+1 in nums or i-1 in nums:
x=True
else:
x=False
return x
assert is_nice([2, 10, 9, 3]) is True
assert is_nice([3, 4, 5, 7]) is False