Old Wei wins the offer to take you to learn --- Brush title series (45 poker straight)

45. poker straight

problem:

LL particularly good mood today, because he bought a deck of cards and found that there actually has two king, two small king (the deck was originally 54 _ ) ... he randomly drawn from the five cards, I would like to test test their luck, see if you can be able to get straight, if able to get it, he decided to buy a sports lottery, hey! ! "Red A, 3 of spades, Wang, King, square piece 5", "Oh My God! " Is not a straight ... LL happy, he thought, decides \ Amy can be seen as any number, and A regarded as 1, J is 11, Q is 12, K 13. The above five cards can become "1,2,3,4,5" (king size and 2, respectively seen as 4), "So Lucky!" . LL decided to buy sports lottery friends. Now, I ask you to use this piece of card simulate the above procedure, and then tell us how LL luck, if the card can be composed of straight to output true, otherwise it will output false. For convenience, you can think of is king size 0

solve:

thought:

Satisfies the condition:
. 1 max - min <5
2 0 except no duplicate number (card)
3 5 array length

Let sorted array of residuals and can be less than 4

python code:

# -*- coding:utf-8 -*-
import collections
class Solution:
    def IsContinuous(self, numbers):
        # write code here
        if not numbers:
            return False
        new_list = [i for i in numbers if i > 0]
        new_list.sort()
        n = 0
        for j in range(len(new_list)-1):
            if (new_list[j+1] - new_list[j]) > 0:
                n += (new_list[j+1] - new_list[j])
            else:
                return False
        if n <= 4:
            return True
        else:
            return False
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/105058671