【每日一题20220324】序列分类

:man_mage: 一个数字序列经常会作为一个函数的返回结果,我们今天的任务是,根据数字序列的特征进行归类,判定它是属于哪种类型的序列,返回对应的分类编号。

具体分类如下:

编号 类型 示例
0 unordered(无序) [3,5,8,1,14,3]
1 strictly increasing(严格升序) [3,5,8,9,14,23]
2 not decreasing(不是降序) [3,5,8,8,14,14]
3 strictly decreasing(严格降序) [14,9,8,5,3,1]
4 not increasing(不是升序) [14,14,8,8,5,3]
5 constant(恒定) [8,8,8,8,8,8]

题目难度:中等
题目来源:CodeWars: Sequence classifier

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

assert solution([3,5,8,1,14,3]) == 0
assert solution([3,5,8,9,14,23]) == 1
assert solution([3,5,8,8,14,14]) == 2
assert solution([14,9,8,5,3,1]) == 3
assert solution([14,14,8,8,5,3]) == 4
assert solution([8,8,8,8,8,8]) == 5
assert solution([8,9]) == 1

def solution(nums: list) -> int:
    nums_shengxu=sorted(nums)
    nums_jiangxu=sorted(nums,reverse=True)
    if nums==nums_shengxu:
        for i in range(len(nums)):
            for j in range(1,len(nums)):
                if nums_shengxu[i]==nums_shengxu[j] and nums_shengxu[0]==nums[-1]:
                    return 5
                elif len(set(nums_shengxu))>1 and len(set(nums_shengxu))<len(nums):
                    return 2
                else:
                    return 1
    elif nums==nums_jiangxu:
        for i in range(len(nums)):
            for j in range(1,len(nums)):
                if len(set(nums_jiangxu))>1 and len(set(nums_jiangxu))<len(nums):
                    return 4
                else:
                    return 3
    elif nums!=nums_shengxu and nums!=nums_jiangxu:
        return 0
def solution(nums):
	if sorted(nums) == nums and len(set(nums)) == len(nums):
		return 1

	if len(set(nums)) != 1 and sorted(nums) == nums:
		return 2

	if all([nums[i] > nums[i + 1] for i in range(len(nums)-1)]):
		return 3

	if len(set(nums)) != 1 and sorted(nums,reverse=True) == nums:
		return 4

	if len(set(nums)) == 1:
		return 5

	else:
		return 0
def sequence_classifier(nums):
    if list(set(nums)).__len__() == 1:
        return 5
    new_arr = copy.deepcopy(nums)
    new_arr.sort()
    if new_arr == nums:
        if list(set(nums)).__len__() != new_arr.__len__():
            return 2
        else:
            return 1
    new_arr.sort(reverse=True)
    if new_arr == nums:
        if list(set(nums)).__len__() != new_arr.__len__():
            return 4
        else:
            return 3
    return 0
def solution(nums):
    if all(nums[i] == nums[i + 1] for i in range(len(nums) - 1)):
        return 5
    if all(nums[i] < nums[i + 1] for i in range(len(nums) - 1)):
        return 1
    if all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)):
        return 2
    if all(nums[i] > nums[i + 1] for i in range(len(nums) - 1)):
        return 3
    if all(nums[i] >= nums[i + 1] for i in range(len(nums) - 1)):
        return 4
    return 0
1 Like
def solution(nums: list) -> int:
    # your code here
    sortNum = sorted(nums)
    sortNum1 = sorted(nums, reverse=True)
    setNum = list(set(nums))
    if len(setNum) == 1:
        return 5
    else:
        if nums == sortNum and len(setNum) == len(nums):
            return 1
        elif nums == sortNum and len(setNum) < len(nums):
            return 2
        elif nums == sortNum1 and len(setNum) == len(nums):
            return 3
        elif nums == sortNum1 and len(setNum) < len(nums):
            return 4
        else:
            return 0


assert solution([3, 5, 8, 1, 14, 3]) == 0
assert solution([3, 5, 8, 9, 14, 23]) == 1
assert solution([3, 5, 8, 8, 14, 14]) == 2
assert solution([14, 9, 8, 5, 3, 1]) == 3
assert solution([14, 14, 8, 8, 5, 3]) == 4
assert solution([8, 8, 8, 8, 8, 8]) == 5
assert solution([8, 9]) == 1
def solution(nums: list) -> int:
    # your code here
    num0 = 0
    num1 = 0
    for i in range(len(nums) - 1):
        if nums[i] < nums[i + 1]:
            num0 += 1
        elif nums[i] == nums[i + 1]:
            num1 += 1

    if num0 == 0:
        if num1 == 0:
            return 3
        elif 0 < num1 < len(nums) - 1:
            return 4
        else:
            return 5
    else:
        if num0 == len(nums) - 1:
            return 1
        elif num0 + num1 < len(nums) - 1:
            return 0
        elif num0 + num1 == len(nums) - 1:
            return 2


assert solution([3, 5, 8, 1, 14, 3]) == 0
assert solution([3, 5, 8, 9, 14, 23]) == 1
assert solution([3, 5, 8, 8, 14, 14]) == 2
assert solution([14, 9, 8, 5, 3, 1]) == 3
assert solution([14, 14, 8, 8, 5, 3]) == 4
assert solution([8, 8, 8, 8, 8, 8]) == 5
assert solution([8, 9]) == 1
    def solution(nums: list) -> int:
        if len(nums) < 2:
            return 5 #constant
        else:
            n = 0
            l = len(nums)
            for i in range(l-2):
                if nums[i] == nums[i+1]:n += 1
                elif nums[i] < nums[i+1]:
                    if nums[i+2] < nums[i+1]:return 0
                    n += 2
                else :
                    if nums[i+2] > nums[i+1]:return 0
                    n += 3
            n = n+1 if nums[-1] == nums[-2] else  n+3  if nums[-1] < nums[-2] else n+2
            if n == (l-1):return 5
            elif n == 2*(l-1):return 1
            elif n == 3*(l-1):return 3
            elif l < n < 2*(l-1):return 2
            elif 2*(l-1) < n < 3*(l-1):return 4
def solution(nums: list) -> int:
    if list(dict(Counter(nums)).values())[0]==len(nums):
        return 5
    elif nums==sorted(nums) and list(Counter(list(Counter(nums).values())).values())[0]==len(nums):
        return  1
    elif nums==sorted(nums,reverse=True) and list(Counter(list(Counter(nums).values())).values())[0]==len(nums):
        return 3
    elif nums==sorted(nums) and list(Counter(list(Counter(nums).values())).values())[0]<len(nums):
        return 2
    elif nums==sorted(nums,reverse=True) and list(Counter(list(Counter(nums).values())).values())[0]<len(nums):
        return 4
    else:
        return 0

assert solution([3,5,8,1,14,3]) == 0
assert solution([3,5,8,9,14,23]) == 1
assert solution([3,5,8,8,14,14]) == 2
assert solution([14,9,8,5,3,1]) == 3
assert solution([14,14,8,8,5,3]) == 4
assert solution([8,8,8,8,8,8]) == 5
assert solution([8,9]) == 1

def solution(nums: list) -> int:
    if len(set(nums)) != len(nums):
        if len(set(nums)) == 1:
            return 5
        elif sorted(nums) == nums:
            return 2
        elif sorted(nums, reverse=True) == nums:
            return 4
    else:
        if sorted(nums) == nums:
            return 1
        elif sorted(nums, reverse=True) == nums:
            return 3
    return 0


assert solution([3, 5, 8, 1, 14, 3]) == 0
assert solution([3, 5, 8, 9, 14, 23]) == 1
assert solution([3, 5, 8, 8, 14, 14]) == 2
assert solution([14, 9, 8, 5, 3, 1]) == 3
assert solution([14, 14, 8, 8, 5, 3]) == 4
assert solution([8, 8, 8, 8, 8, 8]) == 5
assert solution([8, 9]) == 1

def solution(nums: list) -> int:
    if nums.count(nums[0]) == len(nums):
        return 5
    increasing = []
    decreasing = []
    for i in range(len(nums)):
        if i == 0:
            increasing.append([nums[i]])
            decreasing.append([nums[i]])
            continue
        if nums[i] > nums[i - 1]:
            increasing.append([nums[i]])
        elif nums[i] < nums[i - 1]:
            decreasing.append([nums[i]])
        else:
            increasing[-1].append(nums[i])
            decreasing[-1].append(nums[i])
    if len(increasing) == len(nums):
        return 1
    elif len(increasing) != len(nums) and len(increasing) + sum(
            [len(increasing[i]) - 1 for i in range(len(increasing))]) == len(nums):
        return 2
    elif len(decreasing) == len(nums):
        return 3
    elif len(decreasing) != len(nums) and len(decreasing) + sum(
            [len(decreasing[i]) - 1 for i in range(len(decreasing))]) == len(nums):
        return 4
    else:
        return 0


assert solution([3, 5, 8, 1, 14, 3]) == 0
assert solution([3, 5, 8, 9, 14, 23]) == 1
assert solution([3, 5, 8, 8, 14, 14]) == 2
assert solution([14, 9, 8, 5, 3, 1]) == 3
assert solution([14, 14, 8, 8, 5, 3]) == 4
assert solution([8, 8, 8, 8, 8, 8]) == 5
assert solution([8, 9]) == 1