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 7506523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:18:12+00:00 2026-05-29T22:18:12+00:00

I am trying to implement an algorithm that takes in two ints n and

  • 0

I am trying to implement an algorithm that takes in two ints n and k where n is the number of seats in a row, and k is the number of students trying to sit in that row. The thing is that each student must be at least two seats from each other on both side. What I have is a function that generates all subsets (an array of either 0 or 1 s, 1 meaning someone is sitting there) and I send this to a function to check to see if it is a valid subset. This is the code I have for that function

def process(a,num,n):
    c = a.count('1')
    #If the number of students sitting down (1s) is equal to the number k, check the subset
    if(c == num):
        printa = True
        for i in range(0,n):
            if(a[i] == '1'):
                if(i == 0):
                    if( (a[i+1] == '0') and (a[i+2] == '0') ):
                        break
                    else:
                        printa = False
                elif(i == 1):
                    if( (a[i-1] == '0') and (a[i+1] == '0') and (a[i+2] == '0') ):
                        break
                    else:
                        printa = False
                elif(i == (n-1)):
                    if( (a[i-2] == '0') and (a[i-1] == '0') and (a[i+1] == '0') ):
                        break
                    else:
                        printa = False
                elif(i == n):
                    if( (a[i-2] == '0') and (a[i-1] == '0') ):
                        break
                else:
                    printa = False                    
            else:
                if( (a[i-2] == '0') and (a[i-1] == '0') and (a[i+1] == '0') and (a[i+2] == '0') ):
                    break
                else:
                    printa = False
        if(printa):
            print a
    else:
        return

The code works for small inputs of k and n but if I get higher values I get an index out of list error for some reason I can’t figure out.
Any help out be great thanks.

O the input a is the list that looks something like this

['1','0','0','1','0'] # a valid subset for n=5 and k=2
['0','0','0','1','1'] # an invalid subset

EDIT:

Code that calls process:

'''
This function will recursivly call itself until it gets down to the leaves then sends that
subset to process function.  It appends
either a 0 or 1 then calls itself
'''
def seatrec(arr,i,n,k):
    if(i==n):
        process(arr,k,n)
        return
    else:
        arr.append("0")
        seatrec(arr,i+1,n,k)
        arr.pop()
        arr.append("1")
        seatrec(arr,i+1,n,k)
        arr.pop()
    return
'''
This is the starter function that sets up the recursive calls
'''
def seat(n,k):
    q=[]
    seat(q,0,n,k)

def main():
    n=7
    k=3
    seat(n,k)

if __name__ == "__main__":
    main()

The error I get if I use these numbers are

if( (a[i-2] == '0') and (a[i-1] == '0') and (a[i+1] == '0') ):
IndexError: list index out of range
  • 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-29T22:18:14+00:00Added an answer on May 29, 2026 at 10:18 pm

    It is sufficient to exclude invalid seating arrangements, namely, when the students seat next to each other ['1', '1'] or when there is only one seat between them ['1', '0', '1'] all other arrangements that have correct numbers of '1', and '0' are valid, example:

    def isvalid(a, n, k):
        if not isinstance(a, basestring):
           a = ''.join(a) # `a` is a list of '1', '0'
        return (len(a) == n and a.count('1') == k and a.count('0') == (n-k) and
                all(p not in a for p in ['11', '101']))
    

    There are more efficient algorithms to generate valid subsets without checking all subsets e.g.,

    def subsets(n, k):
        assert k >= 0 and n >= 0
        if k == 0: # no students, all seats are empty
            yield '0'*n
        elif k == 1 and (n == 1 or n == 2): # the last student at the end of the row
            yield '1' + '0'*(n-1) # either '1' or '10'
            if n == 2: yield '01'
        elif n > 3*(k-1): # there are enough empty seats left for k students
            for s in subsets(n-3, k-1):
                yield '100' + s # place a student
            for s in subsets(n-1, k):
                yield '0' + s   # add empty seat
    

    Example

    n, k = 5, 2
    for s in subsets(n, k):
        assert isvalid(s, n, k)
        print(s)
    

    Output

    10010
    10001
    01001
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm trying to do is find an algorithm that can I can implement
I am trying to implement a simple decimation algorithm in c++. I have two
I'm trying to implement a 6 Degrees-style algorithm, in that I'm trying to find
Overview: I am trying to implement the Johnson Trotter Algorithm in Java so that
I'm trying to implement an algorithm that repeatedly applies operations on a Collection (currently
I have been trying to implement an efficient string comparing algorithm that will given
The Problem : I am trying to implement a search algorithm that shows the
I am trying to implement a unify function with an algorithm that is specified
i'm trying to implement an algorithm which is flood-fill alike. the problem is that
I am currently trying to implement an algorithm to select a unique (16-bit) identifier.

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.