Discussion:
diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
(too old to reply)
HenHanna
2024-06-15 20:47:01 UTC
Permalink
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)))
Paul Rubin
2024-06-15 21:30:56 UTC
Permalink
Post by HenHanna
if len(x) <= 1: return True
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:]))
HenHanna
2024-06-16 00:13:49 UTC
Permalink
Post by Paul Rubin
Post by HenHanna
if len(x) <= 1: return True
if abs(x[i] - x[i+1]) != 1: return False
return True
return all(abs(a-b)==1) for a,b in zip(x,x[1:]))
Does this work when x is [] or [1] ?
Kaz Kylheku
2024-06-16 00:34:33 UTC
Permalink
Post by HenHanna
Post by Paul Rubin
Post by HenHanna
if len(x) <= 1: return True
if abs(x[i] - x[i+1]) != 1: return False
return True
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. :)
HenHanna
2024-06-16 00:52:18 UTC
Permalink
     if len(x) <= 1: return True
         if abs(x[i] - x[i+1]) != 1:    return False
     return True
   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.
Lawrence D'Oliveiro
2024-06-16 01:10:20 UTC
Permalink
Post by HenHanna
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]()
HenHanna
2024-06-16 02:29:30 UTC
Permalink
     if len(x) <= 1: return True
         if abs(x[i] - x[i+1]) != 1:    return False
     return True
   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.
Michael F. Stemper
2024-06-17 13:11:23 UTC
Permalink
     if len(x) <= 1: return True
         if abs(x[i] - x[i+1]) != 1:    return False
     return True
   return all(abs(a-b)==1) for a,b in zip(x,x[1:]))
Does this work when x is  []  or  [1]  ?
The easiest way to find out would be to type it in to REPL and
give it a whirl.
--
Michael F. Stemper
87.3% of all statistics are made up by the person giving them.
Loading...