Skip to content

Create seating_arrangements.py #11700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions backtracking/seating_arrangements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
def generate_seating_arrangement(n):
"""
Generates the nth binary sequence where no two '1's are adjacent.

Args:
n (int): The position of the sequence to retrieve.

Returns:
str: The nth valid binary sequence.

Examples:
>>> generate_seating_arrangement(4)
'101'
>>> generate_seating_arrangement(6)
'1001'
>>> generate_seating_arrangement(9)
'10001'
"""
k2 = 2
k1 = ["0"] * (n + 1)
k1[1] = "1"
a1 = 1

while k2 < (n + 1):
if k1[a1][-1] == "0":
k1[k2] = k1[a1] + "0"
k2 += 1
if k2 >= (n + 1):
break
k1[k2] = k1[a1] + "1"
k2 += 1
if k2 >= (n + 1):
break
a1 += 1
elif k1[a1][-1] == "1":
k1[k2] = k1[a1] + "0"
k2 += 1
if k2 >= (n + 1):
break
a1 += 1
return k1[n]


# Doctest
if __name__ == "__main__":
import doctest

doctest.testmod()