diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.

Liste des GroupesRevenir à cl python 
Sujet : 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.lisp
Date : 15. Jun 2024, 21:47:01
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4kuk5$3k353$1@dont-email.me>
User-Agent : Mozilla Thunderbird
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)))

Date Sujet#  Auteur
15 Jun 24 * diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.8HenHanna
15 Jun 24 `* Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.7Paul Rubin
16 Jun 24  `* Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.6HenHanna
16 Jun 24   +- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.1Kaz Kylheku
16 Jun 24   +* Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.3HenHanna
16 Jun 24   i+- Re: diff1(x) in Python: True if all adjacent items (Posting On Python-List Prohibited)1Lawrence D'Oliveiro
16 Jun 24   i`- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.1HenHanna
17 Jun 24   `- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.1Michael F. Stemper

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal