D
Dennis Lee Bieber
Simplistically? By looping to remove one element from the list, thenHow would you write a function that will populate a list with a list
of numbers with all the possibilities? For example a list of 3 numbers
taken among 4 [0,1,2,3] without duplicates. The result should be:
[0,1,2]
[0,1,3]
[0,2,3]
[1,2,3]
recursing to obtain the possibilities of n-1 from the remaining list.
Finally putting together the removed element with all the subpart
candidates... When the recurse is down to wanting 1-element responses,
convert each item in the input list into a 1-element sublist and return
it as each item is a valid last term in the result.
-=-=-=-=-=-=-=-
"""
Brute force m of n
"""
M = 4
N = 8
LST = range(N)
def process(data, elements):
if elements == 1:
return [[d] for d in data]
else:
results = []
for pos, itm in enumerate(data):
nest = data[
tmp = process(nest, elements - 1)
for part in tmp:
candidate = [itm]
candidate.extend(part)
candidate.sort()
if candidate not in results:
results.append(candidate)
return results
import pprint
pprint.pprint(process(LST, M))
-=-=-=-=-=-=-
Note I set it for four digits out of a range of 8, it is not tied to 3
out of 4
[[0, 1, 2, 3],pythonw -u "mofn.py"
[0, 1, 2, 4],
[0, 1, 2, 5],
[0, 1, 2, 6],
[0, 1, 2, 7],
[0, 1, 3, 4],
[0, 1, 3, 5],
[0, 1, 3, 6],
[0, 1, 3, 7],
[0, 1, 4, 5],
[0, 1, 4, 6],
[0, 1, 4, 7],
[0, 1, 5, 6],
[0, 1, 5, 7],
[0, 1, 6, 7],
[0, 2, 3, 4],
[0, 2, 3, 5],
[0, 2, 3, 6],
[0, 2, 3, 7],
[0, 2, 4, 5],
[0, 2, 4, 6],
[0, 2, 4, 7],
[0, 2, 5, 6],
[0, 2, 5, 7],
[0, 2, 6, 7],
[0, 3, 4, 5],
[0, 3, 4, 6],
[0, 3, 4, 7],
[0, 3, 5, 6],
[0, 3, 5, 7],
[0, 3, 6, 7],
[0, 4, 5, 6],
[0, 4, 5, 7],
[0, 4, 6, 7],
[0, 5, 6, 7],
[1, 2, 3, 4],
[1, 2, 3, 5],
[1, 2, 3, 6],
[1, 2, 3, 7],
[1, 2, 4, 5],
[1, 2, 4, 6],
[1, 2, 4, 7],
[1, 2, 5, 6],
[1, 2, 5, 7],
[1, 2, 6, 7],
[1, 3, 4, 5],
[1, 3, 4, 6],
[1, 3, 4, 7],
[1, 3, 5, 6],
[1, 3, 5, 7],
[1, 3, 6, 7],
[1, 4, 5, 6],
[1, 4, 5, 7],
[1, 4, 6, 7],
[1, 5, 6, 7],
[2, 3, 4, 5],
[2, 3, 4, 6],
[2, 3, 4, 7],
[2, 3, 5, 6],
[2, 3, 5, 7],
[2, 3, 6, 7],
[2, 4, 5, 6],
[2, 4, 5, 7],
[2, 4, 6, 7],
[2, 5, 6, 7],
[3, 4, 5, 6],
[3, 4, 5, 7],
[3, 4, 6, 7],
[3, 5, 6, 7],
[4, 5, 6, 7]]
--Exit code: 0
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/