- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check programmers convention arrangements are correct or not in Python
Suppose we have a number n, this is representing programmers looking to enter a convention, and we also have a list of number, convention 1 represents a programmer and 0 represents empty space. Now the condition is no two programmers can sit next to each other, we have to check whether all n programmers can enter the convention or not.
So, if the input is like n = 2, convention = [0, 0, 1, 0, 0, 0, 1], then the output will be True
To solve this, we will follow these steps −
- for i in range 0 to size of conv, do
- a:= 0 when i-1 < 0 otherwise i-1
- b:= size of conv -1 when i+1 >= size of conv otherwise i+1
- if conv[i] is same as 0 and conv[a] is same as 0 and conv[b] is same as 0, then
- conv[i]:= 1
- n := n - 1
- return true when n <= 0, otherwise 0.
Example
class Solution: def solve(self, n, conv): for i in range(len(conv)): a=0 if i-1<0 else i-1 b=len(conv)-1 if i+1>=len(conv) else i+1 if conv[i]==0 and conv[a]==0 and conv[b]==0: conv[i]=1 n-=1 return n<=0 ob = Solution() n = 2 convention = [0, 0, 1, 0, 0, 0, 1] print(ob.solve(n, convention))
Input
2, [0, 0, 1, 0, 0, 0, 1]
Output
True
- Related Articles
- Program to check old and new version numbering are correct or not in Python
- Program to check whether parentheses are balanced or not in Python
- Program to check three consecutive odds are present or not in Python
- Program to check whether elements frequencies are even or not in Python
- Program to check points are forming convex hull or not in Python
- Program to check points are forming concave polygon or not in Python
- Program to check whether two sentences are similar or not in Python
- Program to check linked list items are forming palindrome or not in Python
- Program to check given push pop sequences are proper or not in python
- Program to check whether two string arrays are equivalent or not in Python
- Program to check strings are rotation of each other or not in Python
- Program to check all listed delivery operations are valid or not in Python
- Program to check all values in the tree are same or not in Python
- Program to check two strings are 0 or 1 edit distance away or not in Python
- Program to check two rectangular overlaps or not in Python

Advertisements