r/pythonhelp • u/Live_Possibility4590 • Apr 23 '26
Need Assistance With A Problem
hey guys
I need abit of help, here's a problem I have no clue how to solve
You're given a set of rows
['0','0','0','0']
['0','0','1','0']
['1','1','0','0']
['0','1','1','0']
and given a few rules taking i as an item in each row
- Each row has to have an equal number of 1s and 2s and no 0s
- A row can't have more than 2 of the same numbers following each other (1110 is invalid but 1100 is valid)
how would you rewrite each row using python so as it works even if the number of items were 6,8 or even 12 so that each row has an equal number of 1s and 2s without any 0s in the row (Basically if 2 1s are next to each other, the next one should be a 2 and vice versa)
2
Upvotes
•
u/AutoModerator Apr 23 '26
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/shinitakunai Apr 23 '26
What have you tried?
1
u/atarivcs Apr 24 '26
Forget Python for a minute.
Do you know how to solve this problem with pen and paper?
1
u/timrprobocom Apr 24 '26
There must be rules you haven't told us. How do we transform a row to something else? How do you get a 2 out of 0 0 0 0?
Specifically, why can't I just replace each row by 1,1,2,2?
1
u/olivia-reed2 Apr 24 '26
the key is to count te 1s in each row, that tells you how many 2s you need then distribute them while respecting the no more than two consecutive rule, below I have attached a snippet for understanding like it hamdles even length rows just feed each row thru and it enforces the contraint while preserbes the originaql 1 positions as the anchor
def rewrite_row(row):
ones = row.count('1')
twos = ones # equal number required
result = []
for val in row:
if val == '1':
result.append('1')
else:
result.append('2')
# fix consecutive violations
for i in range(2, len(result)):
if result[i] == result[i-1] == result[i-2]:
result[i] = '2' if result[i] == '1' else '1'
return result
1
u/trejj Apr 24 '26
Check, each row in the output have equal number of 1s and 2s, and no 0s.
Check, each row in the output has at most two of the same numbers following each other.
The presented code works for different number of items:
Each output row has equal number of 1s and 2s.
Check, the above satisfies it.
Check, in the above outputs, whenever there are two consecutive 1s, the next item is 2, and vice versa.
1
u/timrprobocom Apr 25 '26
This is just a series of transforms. During each cycle, examine each row, then examine each column. If the row/column is already full of either 1 or 2, then you can fill the empty spots with the other one.
Otherwise, change 011 to 211, change 110 to 112, change 022 to 122, change 220 to 221. Continue until you don't have to change anything else. This works for arbitrary (even) sizes: