python判断列表是顺子还是同花

def judge(alist):
	if any(alist[i+1] <= alist[i] for i in range(0,len(alist)-1)):
		return 'No'
	else:
		return 'Yes'

alist = [1,2,3,4,5,6,7,8,9,10]
print(judge(alist))         # 'Yes'

alist = [1,2,2,4,5,6,8,9,10]
print(judge(alist))         # 'No'

如果是手动实现算法

from functools import reduce

#file = 'not-number-vip.tom.com.txt'
#shunzi = 'shun-vip-due.txt'

file = 'not-number-vip.tom.com.txt'
tonghua = 'tonghua-vip-due.txt'

value = []

def shunzilist(*value):
    if value[1:]==value[:-1] and reduce(lambda x,y:x*y,value)==1:
        return True
    else:
        return False

def tonghualist(*value):
    if value[1:]==value[:-1]:
        return True
    else:
        return False

with open(file,'r') as file_object:
    lines = file_object.readlines()
    for line in lines:
        line = line.strip()
        char_list = list(line)
        length = len(char_list)
        if length != 1:
            res = tonghualist(*char_list)
            print(line)
            print(res)
            if res == True:
                with open(tonghua, 'a') as file_object:
                    file_object.write(line + '\n')

# with open(file,'r') as file_object:
#     lines = file_object.readlines()
#     for line in lines:
#         line = line.strip()
#         char_list = list(line)
#         length = len(char_list)
#         index = 0
#         if length != 1:
#             while index < length -1:
#                 number = ord(char_list[index])
#                 next_number = ord(char_list[index + 1])
#                 index = index + 1
#                 sub = next_number - number
#                 value.append(sub)
#             res = shunzilist(*value)
#             print(line)
#             print(value)
#             print(res)
#             if res == True:
#                 with open(shunzi, 'a') as file_object:
#                     file_object.write(line + '\n')
#             value.clear()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值