# 扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A、2各4张,小王1张,大王1张。 # 牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王): # 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER # 输入两手牌,两手牌之间用"-"连接,每手牌的每张牌以空格分隔,"-"两边没有空格,如:4 4 4 4-joker JOKER。 # 请比较两手牌大小,输出较大的牌,如果不存在比较关系则输出ERROR。 # 基本规则: # (1)输入每手牌可能是个子、对子、顺子(连续5张)、三个、炸弹(四个)和对王中的一种,不存在其他情况,由输入保证两手牌都是合法的,顺子已经从小到大排列; # (2)除了炸弹和对王可以和所有牌比较之外,其他类型的牌只能跟相同类型的存在比较关系(如,对子跟对子比较,三个跟三个比较),不考虑拆牌情况(如:将对子拆分成个子); # (3)大小规则跟大家平时了解的常见规则相同,个子、对子、三个比较牌面大小;顺子比较最小牌大小;炸弹大于前面所有的牌,炸弹之间比较牌面大小;对王是最大的牌; # (4)输入的两手牌不会出现相等的情况。 # 数据范围:字符串长度:3≤s≤10 # 输入描述: # 输入两手牌,两手牌之间用"-"连接,每手牌的每张牌以空格分隔,"-"两边没有空格,如 4 4 4 4-joker JOKER。 # # 输出描述: # 输出两手牌中较大的那手,不含连接符,扑克牌顺序不变,仍以空格隔开;如果不存在比较关系则输出ERROR。 def poker_compare(string): # all_poker = ["3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"] all_poker = "3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER" continue_poker = "3 4 5 6 7 8 9 10 J Q K" if isinstance(string, str): string1 = string.split("-")[0] string2 = string.split("-")[1] temp1 = string1.split(" ") temp2 = string2.split(" ") if len(temp1) == 1: # 个子 if len(temp2) == 3: return "ERROR" elif len(temp2) == 2: if string2 == "joker JOKER": return string2 else: return "ERROR" elif len(temp2) == 4: if temp2[0] == temp2[1] == temp2[2] == temp2[3]: return string2 else: return "ERROR" else: if all_poker.find(string2) > all_poker.find(string1): return string2 else: return string1 elif len(temp1) == 2: # 对子 if string1 == "joker JOKER": # 对王 return string1 if len(temp2) == 3 or len(temp2) == 1: return "ERROR" elif len(temp2) == 4: if temp2[0] == temp2[1] == temp2[2] == temp2[3]: return string2 else: return "ERROR" else: if string2 == "joker JOKER": return string2 if temp2[0] == temp2[1]: if all_poker.find(temp2[0]) > all_poker.find(string1[0]): return string2 else: return string1 else: return "ERROR" elif len(temp1) == 3: # 三个 if len(temp2) == 1: return "ERROR" if len(temp2) == 2: if string2 == "joker JOKER": return string2 else: return "ERROR" if len(temp2) == 4: if temp2[0] == temp2[1] == temp2[2] == temp2[3]: return string2 else: return "ERROR" if len(temp2) == 3: if temp2[0] == temp2[1] == temp2[2]: if all_poker.find(temp2[0]) > all_poker.find(string1[0]): return string2 else: return string1 else: return "ERROR" elif len(temp1) == 4: # 炸弹(四个) if len(temp2) == 3 or len(temp2) == 1: return "ERROR" if len(temp2) == 2: if string2 == "joker JOKER": return string2 else: return "ERROR" elif len(temp2) == 4: if temp2[0] == temp2[1] == temp2[2] == temp2[3]: if all_poker.find(temp2[0]) > all_poker.find(string1[0]): return string2 else: return string1 else: return "ERROR" else: # 顺子(连续5张) if len(temp2) == 3 or len(temp2) == 1: return "ERROR" if len(temp2) == 2: if string2 == "joker JOKER": return string2 else: return "ERROR" if len(temp2) == 4: if temp2[0] == temp2[1] == temp2[2] == temp2[3]: return string2 else: return "ERROR" if len(temp2) == 5: index_list = [] for i in range(5): index_list.append(continue_poker.find(temp2[i])) for i in range(1, 5): if index_list[i] - index_list[i-1] > 2: return "ERROR" else: if index_list[0] > continue_poker.find(string1[0]): return string2 else: return string1 else: return "other error" if __name__ == "__main__": # 扑克牌大小比较 poker1 = "4-joker" poker2 = "4 4-2 2" poker3 = "4 4 4-A A A" poker4 = "4 4 4 4-K K K K" poker5 = "3 4 5 6 7-joker JOKER" print("poker1 compare result: ", poker_compare(poker1)) print("poker2 compare result: ", poker_compare(poker2)) print("poker3 compare result: ", poker_compare(poker3)) print("poker4 compare result: ", poker_compare(poker4)) print("poker5 compare result: ", poker_compare(poker5)) poker6 = input() print(poker_compare(poker6))
笔试题(六):扑克牌大小
最新推荐文章于 2024-08-29 19:30:55 发布