[Tutor] problem solving with lists

Dennis Lee Bieber wlfraed at ix.netcom.com
Fri Mar 18 13:56:00 EDT 2022

```On Fri, 18 Mar 2022 18:21:19 +0100, <marcus.luetolf at bluewin.ch> declaimed
the following:

Since the OP appears to be conducting a private email conversation with
Mr. Gauld -- having never even acknowledged any other posts provided by
others -- I'm going to do something I normally wouldn't on the basis that
the OP probably won't notice this post either

Provide my working code! Both versions... (though I see my comments are
a bit out of date -- current code is parameterized rather than hard-coded
to particular lengths)

*****	Assumption: common pairs are ADJACENT letters
"""
letters.py              based upon a homework question on tutor
list
dennis l bieber         march 18 2022

a blend of the original sixteen_letters.py and ooREXX letters.rex

This version interprets "common pairs" to mean "adjacent" letters
"""

import sys
import string
from itertools import combinations, filterfalse
import pprint

LETTERS = string.ascii_lowercase

accepted = []   #accumlate good item strings here
def theFilter(item):
"""
Returns False (accepted) if no adjacent letter pair
in item is IN any previously accepted item
"""
rc = False   #assume item will  be accepted
item = "".join(sorted(item))
for aitem in accepted:
for pairx in range(len(item) - 1):
if item[pairx:pairx+2] in aitem:
#reject this item, don't test other items
rc = True
break
if rc: break
return rc

def main(pool_len, item_len):
#generator of four-letter substrings, then generator of filtered
items = combinations(LETTERS[:pool_len], item_len)
itemFilter = filterfalse(theFilter, items)

#generator unfortunately returns tuples, not strings
#also, accepted is "global", theFilter reads it
for item in itemFilter:
accepted.append("".join(sorted(item)))

return accepted #even though a global item

if __name__ == "__main__":
if len(sys.argv) < 3:
print("\n***\tUsage: letters.py pool_length item_length\n")
else:
accepted = main(int(sys.argv[1]), int(sys.argv[2]))
print("\nAccepted Items: %s" % len(accepted))
answer = [accepted[i*4:i*4+4] for i in
range(round(len(accepted) / 5 + 0.5) + 1)]
-=-=-

*****	Assumption: common pairs are set intersection
"""
letters_set.py              based upon a homework question on tutor
list
dennis l bieber         march 18 2022

a blend of the original sixteen_letters.py and ooREXX letters.rex

This version interprets "common pairs" to mean "set intersection"
"""

import sys
import string
from itertools import combinations, filterfalse
import pprint

LETTERS = string.ascii_lowercase

accepted = []   #accumlate good item strings here
def theFilter(item):
"""
Returns False (accepted) if no adjacent letter pair
in item is IN any previously accepted item
"""
rc = False   #assume item will  be accepted
#not optimized -- should save set() in accepted
#rather than continually converting
item = set(item)
for aitem in accepted:
if len(item & set(aitem)) >= 2:
#reject this item, don't test other items
rc = True
break
return rc

def main(pool_len, item_len):
#generator of four-letter substrings, then generator of filtered
items = combinations(LETTERS[:pool_len], item_len)
itemFilter = filterfalse(theFilter, items)

#generator unfortunately returns tuples, not strings
#also, accepted is "global", theFilter reads it
for item in itemFilter:
accepted.append("".join(sorted(item)))

return accepted #even though a global item

if __name__ == "__main__":
if len(sys.argv) < 3:
print("\n***\tUsage: letters_set.py pool_length item_length\n")
else:
accepted = main(int(sys.argv[1]), int(sys.argv[2]))
print("\nAccepted Items: %s" % len(accepted))
answer = [accepted[i*4:i*4+4] for i in
range(round(len(accepted) / 5 + 0.5))]