[Tutor] Creating lists with definite (n) items without repetitions
Marc Tompkins
marc.tompkins at gmail.com
Mon Sep 7 08:22:49 CEST 2015
On Fri, Sep 4, 2015 at 7:28 AM, marcus lütolf <marcus.luetolf at bluewin.ch>
wrote:
> I should probably tell you the real task are a series (maximum ~ 301)
> lists in which real names of people are assigned to the items/letters for
> 2 people(golfers) can be in the same list(flight) only once for an
> extended period of time.
> The next step would be to assign compatible and noncompatible attributes
> to the items/letters which will reduce the maximum of possible
> lists(flights)
>
>
I came up with this (obviously it could be made shorter, but I thought it
would be clearer this way):
import string, itertools
def main():
validCombos = []
usedPairs = []
allCombos = itertools.combinations(string.ascii_lowercase, 3)
for combo in allCombos:
pair1 = (combo[0],combo[1])
pair2 = (combo[0],combo[2])
pair3 = (combo[1],combo[2])
if pair1 in usedPairs or pair2 in usedPairs or pair3 in usedPairs:
next
else:
usedPairs.append(pair1)
usedPairs.append(pair2)
usedPairs.append(pair3)
validCombos.append(combo)
print(validCombos)
print(len(validCombos))
if __name__ == '__main__':
main()
The resulting triplets seem to meet your criteria - but there are only 90
of them. How confident are you about the number 301?
More information about the Tutor
mailing list