+ 1

Pls explain this piece of code

words = 'abracadabra', 'tomato', 'python' def isGarland(w): g = 0 for i in range(len(w)): if w[:i] == w[-i:]: g = i return w[:g], g for word in words: gar = isGarland(word) print("{} is {}a Garland word {}".format(word, "not "*(gar[0]==''), ("of a degree of "+str(gar[1])+": "+gar[0])*(gar[0]!='')))

7th Dec 2019, 4:21 PM
Thakur Lion😎😎
Thakur Lion😎😎 - avatar
2 Respostas
0
a garland word is word that has the same letters in the beginning and at the end in the same order. the number of similar letters determine the degree. tomato for example is 2 degrees because the same letters are "to" and are two letters. code explanation bellow
7th Dec 2019, 6:36 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
the function compares the letters first vs last, second vs second last and so on. by using. w[:i] == [-i :] g = i stores the number of similar letters return w[:g], g w[:g] first letter to the last similar letter. g is the number of similar letters (degree) in the loop, gar stores the return of the function, similar letters and degree. {} are place holders for the variables in the format method. gar[0] = similar letters gar[1] = degree "not" *(gar[0]=='') means unpack "not" only when gar[0] is empty, when the expression is true. gar[0])*(gar[0]! ='') unpack gar[0] when it's not empty.
7th Dec 2019, 6:50 PM
Bahhaⵣ
Bahhaⵣ - avatar