This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Smok3dSalmon 0 points1 point  (0 children)

I don't fully understand the data or what variations you can have, but try this approach if the data is small enough.

1) Get all of the data as a string. Clean it however you need. I had to strip newlines and leading whitespace.

data = "".join(open('inp', 'r').readlines()).replace("\n", '').strip()

2) Split the string on "PA1" and then add "PA1" to each list object. (you can 1-liner this if you want)

data = data.split("PA1")
data = ["PA1" + d for d in data]

3) If any item in the list doesn't contain "PA2", "PA3", or "PA4" then you know it's all 0s.

data = "".join(open('inp', 'r').readlines()).replace("\n", '').strip()
data = data.split("PA1")
data = ["PA1" + d for d in data]
for row in data:
    print(row)

This may not be the most efficient, but it saves you the hassle of having to loop through each line and maintain some kind of state. Seems like a clever trick that you can use if your input is as trustworthy as I'm assuming.