Sujet : Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
De : HenHanna (at) *nospam* devnull.tb (HenHanna)
Groupes : comp.lang.python comp.lang.lispDate : 16. Jun 2024, 03:29:30
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4limb$3s061$1@dont-email.me>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
On 6/15/2024 5:52 PM, HenHanna wrote:
On 6/15/2024 5:13 PM, HenHanna wrote:
On 6/15/2024 2:30 PM, Paul Rubin wrote:
HenHanna <HenHanna@devnull.tb> writes:
def diff1(x):
if len(x) <= 1: return True
for i in range(len(x) - 1):
if abs(x[i] - x[i+1]) != 1: return False
return True
>
def diff2(x):
return all(abs(a-b)==1) for a,b in zip(x,x[1:]))
>
>
Does this work when x is [] or [1] ?
i'm pretty sure that x[1:] would raise an error.
x[1] would raise an error,
but x[1:] is ok. (works like Lisp's CDR)... Nice!
Lispers can learn from Pythonicity and vice versa.