Idiomatic Python


  • 0
    G
    class Solution(object):
        def validUtf8(self, data):
            """
            :type data: List[int]
            :rtype: bool
            """
            suffix, bits = 0, [128, 192, 224, 240, 248]
            
            for datum in data:
                
                if datum >= bits[4]:
                    return False
                    
                if suffix: 
                    if bits[0] <= datum < bits[1]:
                        suffix -= 1
                    else:
                        return False
                else:
                    if bits[0] <= datum < bits[1]:
                        return False
                    else:
                        for i in range(1, 4):
                            if bits[i] <= datum < bits[i+1]:
                                suffix = i
                                break
    
            return suffix == 0

  • 0

    If you want idiomatic, how about chained comparisons?


  • 0
    G

    good point, with that correction and another minor change it should be more pythonic now...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.