RE: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

2024-06-10 Thread AVI GROSS via Python-list
> i was just curiuos about simple, clever way to write it in Python

It depends on what you mean by "clever".

For some, it was like a suggestion on using something already available such
as itertools.groupby, perhaps even better if it is actually compiled in from
a language like C and perhaps more efficient.

For some who like to Scheme, only a recursive solution may be  considered
clever!

For some who have absorbed ideas in languages like python,  perhaps they
would implement it the way I described as an iterator that is efficient in
the sense that it delivers things just in time and quits if not iterated
further. An example might be if you asked to get groups of consecutive
digits in the calculated indefinite value of a number like pi or e or looked
at how many digits in a row were the same for prime numbers but wanted to
quit after the first billion.

Some may like some kind of functional programming method. Some want short
code and some want efficiency and some even like something written in a
subtle way so others have trouble understanding it, as in the obfuscated C
contests.

What you do need to consider is what exactly are your requirements. Your
examples are trivial so maybe we need to think about what it means to gather
together what is supplied in one container, into a container containing
other grouped containers.

First, what python containers should be handled? Yes, you probably mean a
list but if handed a tuple, should it return the same, or always return a
list for the top level and perhaps even deeper levels? There are many
containers in python including (ordered) dictionaries where the keys may be
unique, but you may want to see if the contents have some order, numpy
arrays, and so on.

And, are all the contents required to be atomic? What does it mean for items
in a row to be equal? If I have a sublist, should I unlist to make it flat
first, or should it be an error, or should each such sublist be compared for
full equality or even relative equality so that (a (b c) (c b) d) actually
accepts (b c) and (c b) as the same for the purpose? For that matter, is 1.0
matched to 1 or even "1" or perhaps an object that has that value in some
way such as when comparing arbitrary objects?

There can be many such questions and some elegant methods get less elegant
if they need to handle too much. You need to explain what to do when a bad
unintended case is found, such as perhaps an empty list or ...

To just do what your examples ask, again, seems easy enough. It is the usual
code if not done too elegantly. Write a function that accepts a list (or
tries to coerce what it gets into a copy as a list. It returns an empty list
if it has nothing, otherwise pops off the first item and then loops on the
rest. When items match the current first item, extend a sublist and when
they don't, yield what you have and start a new sublist. After the loop,
anything remaining is returned.

Simple enough?

Note to get your result properly, since it is an iterator, you need to
either be calling it iteratively, or do something like:

Result = list(chunk(something))

To force it to run to completion.



-Original Message-
From: Python-list  On
Behalf Of HenHanna via Python-list
Sent: Sunday, June 9, 2024 10:37 PM
To: python-list@python.org
Subject: Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

On 6/9/2024 7:05 PM, avi.e.gr...@gmail.com wrote:
> I remembered that HenHanna had been hard to deal with in the past and when
> my reply to him/her/them bounced as a bad/fake address it came back to me
> that I am better off not participating in this latest attempt to get us to
> perform then probably shoot whatever we say down.
> 
> A considerate person would ask questions more clearly and perhaps explain
> what language they are showing us code from and so on.
> 
> Life is too short to waste.
> 
> -Original Message-
> From: Python-list 
On
> Behalf Of HenHanna via Python-list
> Sent: Sunday, June 9, 2024 5:20 PM
> To: python-list@python.org
> Subject: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))
> 
> Chunk, ChunkC -- nice simple way(s) to write these in Python?
> 
> 
> (Chunk  '(a a   ba a a   b b))
>   ==> ((a a) (b)  (a a a) (b b))
> 
> 
> (Chunk  '(a a a a   b   c c   a a   d   e e e e))
>   ==> ((a a a a) (b) (c c) (a a) (d) (e e e e))
> 
> 
> (Chunk  '(2 2   foo   bar bar   j j j   k   baz baz))
>   ==> ((2 2) (foo) (bar bar) (j j j) (k) (baz baz))
> 
> _
> 
> (ChunkC  '(a a   b b b))
>==> ((a 2)  (b 3))
> 
> (ChunkC  '(a a   b  a a a   b b))
>==> ((a 2)  (b 1)  (a 3)   (b 2))



i was just curiuos about simple, clever way to write it in Python


in Scheme (Gauche)

(use srfi-1)  ;; span

(define (gp x)
   (if (null? x) '()
 (let-values (((F L) (span (cut equal? (car x) <>) x)))
   (cons F (gp L)

(print (gp   '(ab ba a a   b b b b)))
(print (gp   '(c c c   a   d d d d   a   e e e e e)))

(de

Re: IDLE: clearing the screen

2024-06-10 Thread Michael F. Stemper via Python-list

On 08/06/2024 14.18, Rob Cliffe wrote:

OK, here is the advanced version:
import os
class _cls(object):
     def __repr__(self):
         os.system('cls')
         return ''
cls = _cls()

Now when you type
cls
it clears the screen.  The only flaw is that there is a blank line at the very top of the screen, 
and the ">>>" prompt appears on the SECOND line.
(This blank line is because the IDLE prints the blank value returned by "return 
''" and adds a newline to it, as it does when printing the value of any expression.)


Why have it return anything at all?

--
Michael F. Stemper
Indians scattered on dawn's highway bleeding;
Ghosts crowd the young child's fragile eggshell mind.

--
https://mail.python.org/mailman/listinfo/python-list


Re: IDLE: clearing the screen

2024-06-10 Thread Michael F. Stemper via Python-list

On 10/06/2024 09.32, Stefan Ram wrote:

"Michael F. Stemper"  wrote or quoted:

On 08/06/2024 14.18, Rob Cliffe wrote:

OK, here is the advanced version:
import os
class _cls(object):
      def __repr__(self):
          os.system('cls')
          return ''
cls = _cls()

...

Why have it return anything at all?


   Because __repr__ needs to return a str.


Got it. Thanks for clarifying.

--
Michael F. Stemper
87.3% of all statistics are made up by the person giving them.

--
https://mail.python.org/mailman/listinfo/python-list


Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

2024-06-10 Thread HenHanna via Python-list

On 6/9/2024 7:05 PM, avi.e.gr...@gmail.com wrote:

I remembered that HenHanna had been hard to deal with in the past and when
my reply to him/her/them bounced as a bad/fake address it came back to me
that I am better off not participating in this latest attempt to get us to
perform then probably shoot whatever we say down.

A considerate person would ask questions more clearly and perhaps explain
what language they are showing us code from and so on.

Life is too short to waste.

-Original Message-
From: Python-list  On
Behalf Of HenHanna via Python-list
Sent: Sunday, June 9, 2024 5:20 PM
To: python-list@python.org
Subject: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

Chunk, ChunkC -- nice simple way(s) to write these in Python?


(Chunk  '(a a   ba a a   b b))
  ==> ((a a) (b)  (a a a) (b b))


(Chunk  '(a a a a   b   c c   a a   d   e e e e))
  ==> ((a a a a) (b) (c c) (a a) (d) (e e e e))


(Chunk  '(2 2   foo   bar bar   j j j   k   baz baz))
  ==> ((2 2) (foo) (bar bar) (j j j) (k) (baz baz))

_

(ChunkC  '(a a   b b b))
   ==> ((a 2)  (b 3))

(ChunkC  '(a a   b  a a a   b b))
   ==> ((a 2)  (b 1)  (a 3)   (b 2))




i was just curiuos about simple, clever way to write it in Python


in Scheme (Gauche)

(use srfi-1)  ;; span

(define (gp x)
  (if (null? x) '()
(let-values (((F L) (span (cut equal? (car x) <>) x)))
  (cons F (gp L)

(print (gp   '(ab ba a a   b b b b)))
(print (gp   '(c c c   a   d d d d   a   e e e e e)))

(define (gpC x)  (map (lambda (x) (list (car x) (length x))) (gp x)))

(print (gpC '(ab ba a a   b b b b)))
(print (gpC '(c c c   a   d d d d   a   e e e e e)))
--
https://mail.python.org/mailman/listinfo/python-list


Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

2024-06-10 Thread HenHanna via Python-list

On 6/9/2024 3:50 PM, MRAB wrote:

On 2024-06-09 22:20, HenHanna via Python-list wrote:


Chunk, ChunkC -- nice simple way(s) to write these in Python?


(Chunk  '(a a   b    a a a   b b))
  ==> ((a a) (b)  (a a a) (b b))


(Chunk  '(a a a a   b   c c   a a   d   e e e e))
  ==> ((a a a a) (b) (c c) (a a) (d) (e e e e))


(Chunk  '(2 2   foo   bar bar   j j j   k   baz baz))
  ==> ((2 2) (foo) (bar bar) (j j j) (k) (baz baz))

_

(ChunkC  '(a a   b b b))
   ==> ((a 2)  (b 3))

(ChunkC  '(a a   b  a a a   b b))
   ==> ((a 2)  (b 1)  (a 3)   (b 2))


You can make use of itertools.groupby.




Thanks!   i'll try it.


Scheme (Gauche)

(use srfi-1)   ; span

(define (gp x)
  (if (null? x) '()
(let-values (((F L) (span (cut equal? (car x) <>) x)))
  (cons F (gp L)

(print (gp   '(ab ba a a   b b b b)))
(print (gp   '(c c c   a   d d d d   a   e e e e e)))

--
https://mail.python.org/mailman/listinfo/python-list