[Tutor] problem solving with lists

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Mar 8 10:17:25 EST 2022


		o/~		Talking to myself in public...	o/~

On Mon, 07 Mar 2022 13:28:49 -0500, Dennis Lee Bieber
<wlfraed at ix.netcom.com> declaimed the following:

>NOTE:	All solutions are using itertools.combinations and
>itertools.filterfalse, but differ on the filter predicate function.
>

	If the OP is still "listening"...

	Generating all (1820) combinations first, and then trying to exclude
those sharing adjacent letter pairs by testing the first against the rest
of the list is a complete failure -- accepting only the last generated
quad-letter (since, if you've eliminated all prior quads for sharing letter
pairs, the only remaining quad is now unique sharing with nothing).

	In contrast, the step-wise generate/filter-against-accepted-only
process, begins with one accepted quad (the first, since there is nothing
else against which to filter it out).

	One could still generate all 1820 combinations, but the filter logic
still has to be: reject a quad if it shares any letter pairs with
previously accepted quads (NOT with the rest of the 1820 combinations). If
running from the top-down (generation order), the result would be the same
as the step-wise method that generates new quads on demand, and only has to
maintain the shorter "accepted" list of quads.

	Of course, one could process the full list in reverse (accepting the
last quad automatically, the checking the next prior quad against it,
etc.). This WILL result in a different set of quads being accepted. Might
as well just run the step-wise process over a "reversed" generator (which
will likely generate all 1820 in order to reverse the list, but that is
behind the scenes).

Generator order:
quadFilter = filterfalse(theFilter, quads)

>>> 23
[['abcd', 'acef', 'adeg', 'aehi'],
 ['afgh', 'agij', 'ahjk', 'aikl'],
 ['ajlm', 'akmn', 'alno', 'amop'],
 ['bdfh', 'beil', 'bfim', 'bgjm'],
 ['bhkn', 'binp', 'cfjn', 'cgko'],
 ['chlo', 'dglp', 'dhmp']]

Reversed order:
quadFilter = filterfalse(theFilter, reversed(list(quads)))

>>> 20
[['mnop', 'klnp', 'jlmp', 'jkmo'],
 ['hilp', 'gikp', 'ghlo', 'fijp'],
 ['fhko', 'fgkn', 'ehjo', 'egjn'],
 ['efjm', 'deip', 'cdio', 'bdhp'],
 ['bcin', 'adgp', 'acho', 'abim']]

	Interesting -- reversed produced exactly 20 accepted quads, whereas
forward found 23... I wonder if the instructor may have coded something in
that nature -- by whatever means -- and based the assignment on this (hmmm,
pondering a recursive scheme to create the 1820 quads, and then unwinding
the stack would mean starting with "mnop" as the first quad to be
filtered/accepted... Though a recursion depth of 1820 sounds obscene to me)



>{Now, do I want to attempt to code a string combinations generator/iterator
>in REXX -- I'll need to study if it supports "static" data (previous value
>retained between invocations) in procedures. Otherwise there may be some
>ugly global data exposed; filterfalse generator/iterator isn't really
>required as the filter predicate can be manually called}

	Not that anyone cares -- using the Python documentation for
itertools.combinations I did manage to code something in REXX similar to
the generator (convoluted as no "yield" equivalent, nor StopIteration; and
only one "generator" could be in use at a time due to how state was carried
between calls... I'm not going to study an Object REXX text to see if I can
create a class for the generator!).

	It produces the same 23 quads as "forward" processing Python does, so
at least the combination generator works for that case.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list