# Python：is_correct_sequence（不加倍）Python: is_correct_sequence (no doubles)

``````def is_correct_sequence(seq):
"""
Check whether the given sequence does not contain
the same value (except for None) more than once,
and whether it only contains (1) integer values between 1
and the length of the given sequence and/or (2) the special value None.
"""

for i in range(len(seq)):
if seq[i] < 1 or seq[i] > len(seq):
return False
elif type(seq[i]) != type(None) or type(seq[i]) != int:
return False
else:
for i in range(len(seq)-1):
for j in range(i+1, len(seq)):
if seq[i] == seq[j] != None:
return False
else:
return True

##TESTS

assert not is_correct_sequence((4, "abc", None, -17, None, "xyz"))
assert not is_correct_sequence((1, 2, 1))
assert not is_correct_sequence((1.0, None, 1))
assert is_correct_sequence((None, None, 4, 1))
assert is_correct_sequence((None, 1, 2, 3))
``````

## 3 个回复3

### ===============>>#1 票数：0

``````def is_correct_sequence(seq):
for i in range(len(seq)):
if seq[i] is not None and (seq[i] <= 1 and seq[i] >= len(seq)):
# This check previously failed if seq[i] was None, and did not
# check the bounds properly
return False
elif seq[i] is not None and type(seq[i]) != int:
# Use 'and', not 'or'
return False
else:
for i in range(len(seq)-1):
for j in range(i+1, len(seq)):
if seq[i] == seq[j] and seq[i] is not None and seq[j] is not None:
# Again, same thing. This failed if either seq[i] or seq[j]
# were not equal to None
return False
return True
``````

``````def is_correct_sequence(seq):
nums = [a for a in seq if a is not None]
allowed_types = (type(None), int)

contains_correct_types = all(isinstance(a, allowed_types) for a in seq)
numbers_within_bounds = all(1 <= a <= len(seq) for a in nums)
numbers_are_unique = len(nums) == len(set(nums))
# Set returns a version of a list with all duplicates removed

return contains_correct_types and numbers_within_bounds and numbers_are_unique
``````

### ===============>>#2 票数：0

``````def is_correct_sequence(seq):
return has_unique_values(seq) and has_correct_range(seq)

def has_unique_values(seq):
"""
Check whether sequence contains the same value (excluding None) more than once

Returns True if all values are unique or None, False otherwise.
"""

nones = seq.count(None)
seq_set = set(seq)

if nones:
if len(seq) != (len(seq_set) + nones - 1):
return False
else:
if len(seq) != len(seq_set):
return False

return True

def has_correct_range(seq):
"""
Check whether sequence contains integer values between 1 and the length
of the given sequence and/or the special value None.
"""

accepted_range = range(1, len(seq) + 1)
for e in seq:
if e not in accepted_range and e is not None:
return False

return True

assert not is_correct_sequence((4, "abc", None, -17, None, "xyz"))
assert not is_correct_sequence((1, 2, 1))
assert not is_correct_sequence((1.0, None, 1))
assert is_correct_sequence((None, None, 4, 1))
assert is_correct_sequence((None, 1, 2, 3))
``````

### ===============>>#3 票数：0

``````from collections import Counter

def is_correct_sequence(seq):

len_seq = len(seq)
counts = Counter(seq)

for value, count in counts.items():

# Never check None items
if (value is not None and (

# Does the value occur more than once
# or has it a bad type (no integer)?
(count > 1 or not isinstance(value, int)) or

# Or is the integer not in the valid range?
(value < 1 or value > len_seq))
):

# Sequence is in correct!
return False

return True

assert not is_correct_sequence((4, "abc", None, -17, None, "xyz"))
assert not is_correct_sequence((1, 2, 1))
assert not is_correct_sequence((1.0, None, 1))
assert is_correct_sequence((None, None, 4, 1))
assert is_correct_sequence((None, 1, 2, 3))
``````

• `isinstance()`用于类型检查
• `is None` / `is not None`而不是`== None` / `!= None`
• 对于这样的简单测试，没有嵌套循环（ `collections.Counter`应该使我的解决方案相当有效）

1回复

1回复

3回复

1回复

2回复

2回复

3回复

3回复

1回复

1回复