samedi 9 janvier 2021

Question about if statement if not thing is specified after it?

I wanted to ask three things. Firstly, for the 10th line, is this an 'else' statement? It doesn't seem to be the case- if it not, what purpose does it serve?

Secondly, for the 8th line, are we saying that if 'val' is in the list char_set, then return False? And generally speaking is this how we would write this?

Lastly, for line 5, what does [False for _ in range(128)] do? Thanks!

 def unique(string):
    if len(string) > 128:
        return False

    char_set = [False for _ in range(128)]
    for char in string:
        val = ord(char)                 
        if char_set[val]:          
            return False
        char_set[val] = True      

    return True

Aucun commentaire:

Enregistrer un commentaire