Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6336015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:58:06+00:00 2026-05-24T18:58:06+00:00

I need to label N objects with unique tuples (A, B, C), where A

  • 0

I need to label N objects with unique tuples (A, B, C), where A < B < C and the maximal number of identical As is M. The same for Bs and Cs each. Among all solutions one with the lowest highest value of C is searched for. (This last sentence means: if one of two solutions has a highest C of 4 and the other of 5, then the first one is to right answer.)

Example:

M = 1
N = 4
#          As Bs Cs
objects = [(1, 2, 3), 
           (2, 3, 4), 
           (3, 4, 5), 
           (4, 5, 6)]
M = 2
N = 4
objects = [(1, 2, 3),
           (1, 2, 4),
           (2, 3, 4),
           (2, 3, 5)]
# or e.g
objects = [(1, 2, 3), 
           (2, 3, 4), 
           (2, 4, 5), 
           (3, 4, 5)]

M = 3
N = 8
objects = [(1, 2, 3), 
           (2, 3, 4), 
           (2, 3, 5), 
           (2, 4, 5), 
           (3, 4, 5), 
           (3, 4, 6), 
           (3, 5, 6), 
           (4, 5, 6)]

The program I came up with is a complicated if else monster:

import sys
# useage: labelme.py <N> <M>
class ObjectListTree(object):
    """Create many possible paths. 
    Store the parent in each node. 
    The last nodes are appended to the class wide endnodes.
    """
    endnodes = []
    def __init__(self, parent, label, counter, n, M, N):
        self.parent = parent
        self.M = M
        self.N = N
        self.label = label
        self.counter = counter
        self.n = n
        if n < N:    
            self.inc_a()
            self.inc_b() 
            self.inc_c()
        else:
            ObjectListTree.endnodes.append(self)

    def inc_a(self):
        if self.label[0]+1 < self.label[1]:
            if self.counter[1] < self.M:
                if self.counter[2] < self.M:
                    self.plus_1()
                else:
                    self.plus_1_3()
            else:
                if self.counter[2] < self.M:
                    self.plus_1_2()
                else:
                    self.plus_all()
        elif self.label[1]+1 < self.label[2]:
            if self.counter[2] < self.M:
                self.plus_1_2()
            else:
                self.plus_all()
        else:
            self.plus_all()

    def inc_b(self):
        if self.counter[0] == self.M:
            return
        if self.label[1]+1 < self.label[2] and self.counter[2] < self.M:
            self.plus_2()
        else:
            self.plus_2_3()

    def inc_c(self):
        if self.counter[0] == self.M or self.counter[1] == self.M:
            return
        else:
            self.plus_3()

    def plus_all(self):
        ObjectListTree(self, (self.label[0]+1, self.label[1]+1, self.label[2]+1),
                       counter = [1, 1, 1,],
                       n = self.n+1, N=self.N, M=self.M)
    def plus_1_2(self):
        ObjectListTree(self, (self.label[0]+1, self.label[1]+1, self.label[2]),
                       counter = [1, 1, self.counter[2]+1,],
                       n = self.n+1, N=self.N, M=self.M)
    def plus_1_3(self):
        ObjectListTree(self, (self.label[0]+1, self.label[1], self.label[2]+1),
                       counter = [1, self.counter[1]+1, 1,],
                       n = self.n+1, N=self.N, M=self.M)
    def plus_1(self):
        ObjectListTree(self, (self.label[0]+1, self.label[1], self.label[2]),
                       counter = [1, self.counter[1]+1, self.counter[2]+1,],
                       n = self.n+1, N=self.N, M=self.M)
    def plus_2(self):
        ObjectListTree(self, (self.label[0], self.label[1]+1, self.label[2]),
                       counter = [self.counter[0]+1, 1, self.counter[2]+1,],
                       n = self.n+1, N=self.N, M=self.M)
    def plus_2_3(self):
        ObjectListTree(self, (self.label[0], self.label[1]+1, self.label[2]+1),
                       counter = [self.counter[0]+1, 1, 1,],
                       n = self.n+1, N=self.N, M=self.M)
    def plus_3(self):
        ObjectListTree(self, (self.label[0], self.label[1], self.label[2]+1),
                       counter = [self.counter[0]+1, self.counter[1]+1, 1,],
                       n = self.n+1, N=self.N, M=self.M)

tree = ObjectListTree(parent=None, label=(1, 2, 3), counter = [1,1,1,], n=1, N=int(sys.argv[1]), M=int(sys.argv[2]))

best_path = tree.endnodes[0]
for n in tree.endnodes:
    if n.label[2] < best_path.label[2]:
        best_path = n
objects = []
while best_path:
    objects.append(best_path.label)
    best_path = best_path.parent
objects.reverse()
print objects 

But I have the feeling that this should actually be something simple like a combination of two or three functions from the itertools module wrapped in a set or something. Can anyone see a simple solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:58:06+00:00Added an answer on May 24, 2026 at 6:58 pm

    I think this code meets your requirements and always generates the solution with the lowest possible C. Not quite using itertools however.

    def generateTuples(N, M):
      done = 0
      counters = {}
      for C in range(3, N + 3):
        for B in range(2, C):
          for A in range(1, B):
            if (counters.get('A%i' % A, 0) < M and
                counters.get('B%i' % B, 0) < M and
                counters.get('C%i' % C, 0) < M):
              yield (A, B, C)
              counters['A%i' % A] = counters.get('A%i' % A, 0) + 1
              counters['B%i' % B] = counters.get('B%i' % B, 0) + 1
              counters['C%i' % C] = counters.get('C%i' % C, 0) + 1
              done += 1
              if done >= N:
                return
    
    for (A, B, C) in generateTuples(8, 3):
      print (A, B, C)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the admin change_form of one of my objects I need to have extra
I need to attach text labels to objects that are spread randomly across the
I had cause to need a label with a large font on a Delphi
In GWT javadoc, we are advised If you only need a simple label (text,
I need to perform a few checks (enable or disable a label elsewhere on
I need to create a row in XAML which has a label,two radio buttons..
I need help doing the following: a preprocessor macro label(x) shall output #x, e.g.,
I need to set a maximum widht to a label tag and avoid the
here i need a batch file which can apply and create label or base
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.