Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #367: Webmasters kidnapped by evil cult.


comp / comp.lang.python / Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.

SubjectAuthor
* diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.HenHanna
`* Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.Paul Rubin
 `* Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.HenHanna
  +- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.Kaz Kylheku
  +* Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.HenHanna
  |+- Re: diff1(x) in Python: True if all adjacent items (Posting On Python-List ProhiLawrence D'Oliveiro
  |+- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.HenHanna
  |`- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.HenHanna
  +- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.Michael F. Stemper
  `- Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.Michael F. Stemper

1
Subject: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
From: HenHanna
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sat, 15 Jun 2024 20:47 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: diff1(x) in Python: True if all adjacent items differ by 1, False
otherwise.
Date: Sat, 15 Jun 2024 13:47:01 -0700
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <v4kuk5$3k353$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Jun 2024 22:47:02 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5a9d7d343de3468ada1fe8423f653db7";
logging-data="3804323"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19gA7dpJoQSFBSd6g7Wx6W2ByLoV7spjI0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Laq+qFJ+2nbgRFyDoJayMn3ypjI=
Content-Language: en-US
View all headers

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

Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
From: Paul Rubin
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sat, 15 Jun 2024 21:30 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
Date: Sat, 15 Jun 2024 14:30:56 -0700
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <8734pd7v1r.fsf@nightsong.com>
References: <v4kuk5$3k353$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sat, 15 Jun 2024 23:31:01 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="10e676ec212f80670e94fe2bb9c0f8f5";
logging-data="3837891"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187HbrTMB1Tnk05Rmz+ivbe"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:/rI+4/Tglnr0dG9kzJgXuxmUD0Q=
sha1:Z0Pt4fCu76qjCSOK4wmDRNfRv00=
View all headers

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

Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
From: HenHanna
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 16 Jun 2024 00:13 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False
otherwise.
Date: Sat, 15 Jun 2024 17:13:49 -0700
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <v4lanu$3mt2j$1@dont-email.me>
References: <v4kuk5$3k353$1@dont-email.me> <8734pd7v1r.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 16 Jun 2024 02:13:52 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="35c3840a03f107b42fbf945be62926e9";
logging-data="3896403"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RzB8dIncdwORWTUrdtrroQbPVgvXI0G0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:GKBNF0lfYNWeEPoO5QPHPBzjckQ=
Content-Language: en-US
In-Reply-To: <8734pd7v1r.fsf@nightsong.com>
View all headers

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

Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
From: Kaz Kylheku
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 16 Jun 2024 00:34 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1,
False otherwise.
Date: Sun, 16 Jun 2024 00:34:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <20240615173407.24@kylheku.com>
References: <v4kuk5$3k353$1@dont-email.me> <8734pd7v1r.fsf@nightsong.com>
<v4lanu$3mt2j$1@dont-email.me>
Injection-Date: Sun, 16 Jun 2024 02:34:33 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f04802f881217b7539658feaa16a1466";
logging-data="3902551"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kRnrHGm1OlaU+f7SEV5jHdQ891zQVfDM="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:0zggLhn1+Wgme/FhMJUldr+6//0=
View all headers

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

Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
From: HenHanna
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 16 Jun 2024 00:52 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False
otherwise.
Date: Sat, 15 Jun 2024 17:52:18 -0700
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <v4ld02$3mt2j$6@dont-email.me>
References: <v4kuk5$3k353$1@dont-email.me> <8734pd7v1r.fsf@nightsong.com>
<v4lanu$3mt2j$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 16 Jun 2024 02:52:19 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="35c3840a03f107b42fbf945be62926e9";
logging-data="3896403"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ssq4n6FnShLaiRqBKbtwZ2hR7Ot6c5Gc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:WXZHzx8r4rLodypdSuZ+RlFw45M=
Content-Language: en-US
In-Reply-To: <v4lanu$3mt2j$1@dont-email.me>
View all headers

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.

Subject: Re: diff1(x) in Python: True if all adjacent items (Posting On Python-List Prohibited)
From: Lawrence D'Oliv
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 16 Jun 2024 01:10 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: Re: diff1(x) in Python: True if all adjacent items (Posting On
Python-List Prohibited)
Date: Sun, 16 Jun 2024 01:10:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <v4le1s$3n6db$4@dont-email.me>
References: <v4kuk5$3k353$1@dont-email.me> <8734pd7v1r.fsf@nightsong.com>
<v4lanu$3mt2j$1@dont-email.me> <v4ld02$3mt2j$6@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 16 Jun 2024 03:10:21 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="bd45dcd0b5a25b060647b4de82583354";
logging-data="3905963"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19T3BHx7m0L0jtQ2Dqs8DAg"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:nXzlqmEksZ/gPpHwbKjdGHc5ktY=
View all headers

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

Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
From: HenHanna
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 16 Jun 2024 02:29 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False
otherwise.
Date: Sat, 15 Jun 2024 19:29:30 -0700
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <v4limb$3s061$1@dont-email.me>
References: <v4kuk5$3k353$1@dont-email.me> <8734pd7v1r.fsf@nightsong.com>
<v4lanu$3mt2j$1@dont-email.me> <v4ld02$3mt2j$6@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 16 Jun 2024 04:29:32 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="35c3840a03f107b42fbf945be62926e9";
logging-data="4063425"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pB9kGJzchX1bXmmz7IVDrtUAfXAzsd5Y="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:pjFuPq0UfDHFCNuHRy1a3CSRAjw=
In-Reply-To: <v4ld02$3mt2j$6@dont-email.me>
Content-Language: en-US
View all headers

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.
>

Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False otherwise.
From: Michael F. Stemper
Newsgroups: comp.lang.python, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 17 Jun 2024 13:11 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: michael.stemper@gmail.com (Michael F. Stemper)
Newsgroups: comp.lang.python,comp.lang.lisp
Subject: Re: diff1(x) in Python: True if all adjacent items differ by 1, False
otherwise.
Date: Mon, 17 Jun 2024 08:11:23 -0500
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <v4pclr$luk9$1@dont-email.me>
References: <v4kuk5$3k353$1@dont-email.me> <8734pd7v1r.fsf@nightsong.com>
<v4lanu$3mt2j$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 17 Jun 2024 15:11:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="1a2a224911541de666da65aa8cf9c072";
logging-data="719497"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19w0eAS3vEj3jjw8rza7ZSARsb5wlDe1sA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:uzCWUhu1rtQ5ChSUNd0uAMYMvdE=
In-Reply-To: <v4lanu$3mt2j$1@dont-email.me>
Content-Language: en-US
View all headers

On 15/06/2024 19.13, 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]  ?

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.

1

rocksolid light 0.9.8
clearnet tor