• #### diff1(x) in Python: True if all adjacent items differ by 1, False other

From HenHanna@21:1/5 to All on Sat Jun 15 13:47:01 2024
XPost: comp.lang.python

in Python, is this something you'd write using all() ?

https://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work

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 pd(x): print (x, diff1(x))
pd( [0,3]) # ===> #f
pd( [2,3,4,3]) # ===> #t
pd( [1,2,3,4,3,2,3]) # ===> #t
pd( [1,2,3,4,5,6,7,8,9,0]) # ===> #f

(define (diff1p? x)
(or (null? x)
(null? (cdr x))
(and (= 1 (abs (- (car x) (cadr x)) ))
(diff1p? (cdr x)) )))

(diff1p? '(0 3)) ===> #f
(diff1p? '(2 3 4 3)) ===> #t
(diff1p? '(1 2 3 4 5 6 7 8 9 8)) ===> #t
(diff1p? '(1 2 3 4 5 6 7 8 9 8 0)) ===> #f

__________________________
find
find-tail
any
every
count

every pred clist1 clist2 . . . [Function]

[R7RS list] Applies pred across each element of clists, and returns
#f as soon as pred returns #f. If all application of pred
return a non-false value, [every] returns the last result of the
applications.

it sounds like it expands to a big (OR ........ )

______________________________ Not

(define (every? predicate lst) (and (map predicate lst)))

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Paul Rubin@21:1/5 to HenHanna on Sat Jun 15 14:30:56 2024
XPost: comp.lang.python

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:]))

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From HenHanna@21:1/5 to Paul Rubin on Sat Jun 15 17:13:49 2024
XPost: comp.lang.python

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] ?

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Kaz Kylheku@21:1/5 to HenHanna on Sun Jun 16 00:34:33 2024
XPost: comp.lang.python

On 2024-06-16, HenHanna <HenHanna@devnull.tb> 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] ?

Can we keep this abject filth out of the Lisp newsgroup?

Thanks. :)

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From HenHanna@21:1/5 to HenHanna on Sat Jun 15 17:52:18 2024
XPost: comp.lang.python

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.

Lispers can learn from Pythonicity and vice versa.

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Lawrence D'Oliveiro@21:1/5 to HenHanna on Sun Jun 16 01:10:20 2024
XPost: comp.lang.python

On Sat, 15 Jun 2024 17:52:18 -0700, HenHanna wrote:

Lispers can learn from Pythonicity and vice versa.

diff2 = \
lambda x : \
(
lambda : True,
lambda : all(abs(a - b) == 1) for a, b in zip(x, x[1:]),
)[len(x) > 1]()

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From HenHanna@21:1/5 to HenHanna on Sat Jun 15 19:29:30 2024
XPost: comp.lang.python

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.

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)