[Tutor] problem solving with lists

dn PythonList at DancesWithMice.info
Fri Mar 18 23:02:47 EDT 2022


> a) I don't know how to do that, b) I am not shure wether this is a way do
> it,  nor c) if my problem can by solved by python at all.


a) as below - but this is an question of algorithm, NOT Python
b) as below
c) disingenuous question - if it can't be, then why were you assigned
such a problem by your tutor/teacher? That said, I dislike assignments
which so-obviously require attention to "indexing" (of strings or
lists). They allude that someone has taken a BASIC, C, Java, etc,
assignment, and re-used it in Python!

See also (frequent and official) notices about our not being a
homework/assignment mill/factory!


Has the OP's rather arcane approach 'blinkered' our responses?

Why build a number of 'combinations' and then attempt to prove/select
the ones that fit the criteria (or do not)? Wouldn't it be better to
build the sub-lists correctly and avoid any post-processing?

Results obtained:

/usr/bin/python3 /home/dn/Projects/experiments/marcus.py
0 [['a', 'b', 'c', 'd'], ['b', 'c', 'd', 'e'], ['c', 'd', 'e', 'f'],
['d', 'e', 'f', 'g']]
1 [['a', 'c', 'e', 'g'], ['c', 'e', 'g', 'i'], ['e', 'g', 'i', 'k'],
['g', 'i', 'k', 'm']]
2 [['a', 'd', 'g', 'j'], ['d', 'g', 'j', 'm'], ['g', 'j', 'm', 'p'],
['j', 'm', 'p', 'c']]
3 [['a', 'e', 'i', 'm'], ['e', 'i', 'm', 'a'], ['i', 'm', 'a', 'e'],
['m', 'a', 'e', 'i']]
4 [['a', 'f', 'k', 'p'], ['f', 'k', 'p', 'e'], ['k', 'p', 'e', 'j'],
['p', 'e', 'j', 'o']]

Are these correct? If not, please advise which part is incorrect?

If incomplete, are the following definitions correct, or in need of
augmentation/clarification?

all_letters = "abcdefghijklmnop"
number_of_lists = 5
number_of_sub_lists = 4
number_per_sub_list = 4


Solution in 32 nicely-formatted LoC, including both printing and
assembling the results into a single result-list, docstring, comment,
defining of 'magic constants' and helpers, PEP-8 spacings, and full
for-loops (cf comprehension one-liners). itertools has been imported,
but the solution could work just as well without.
-- 
Regards,
=dn


More information about the Tutor mailing list