jeudi 16 avril 2020

How if and else both are execute simultaneously in this code?

def permutation(head,tail=''):
    if(len(head)==0):
        print(tail)
    else:   
        for i in range(len(head)):
            permutation(head[0:i] + head[i+1:],tail + head[i])

permutation("ABC",tail='')

there is no need of else(we can directly use for loop) but as len(head) becomes zero it does print the tail and instead of returning...it does go in else block.

i still wonder how?

Aucun commentaire:

Enregistrer un commentaire