Reputation: 33
I'm looking at a very large set of binary data which is in a separate file. The challenge is to find the largest consecutive number of 1's and 0's. I have already accessed the file from within Python (I'm using Python btw) and have been able to code to find out the total number of 0's and 1's. any help would be much appreciated as I am a total beginner to coding when using Python. Cheers. This what I've done thus far:
filename = "C:/01.txt"
file = open(filename, "r")
count_1 = 0
count_0 = 0
for line in file:
count_0 = count_0 + line.count("0")
count_1 = count_1 + line.count("1")
pass
print("Number of 1s = " + str(count_1))
print("Number of 0s = " + str(count_0))
I have not actually started the coding to find the consecutive numbers.
Upvotes: 0
Views: 917
Reputation: 2540
Here is a simple solution: Loop through the data, count consecutive 1s read and when reading a 0 (meaning you reached the end of one segment) compare it's length to the longest segment of consecutive 1s found so far.
def getMaxSegmentLength(readable):
current_length= 0
max_length= 0
for x in readable:
if x == '1':
current_length+= 1
else:
max_length= max(max_length, current_length)
current_length= 0
return max(max_length, current_length)
def main():
# open a file located in G:/input.txt in read mode and name the file object: inputf
with open('G:/input.txt', 'r') as inputf:
# put all the text in filef in s
s= inputf.read()
# get the longest streak of 1s in string s
n= getMaxSegmentLength(s)
print(n)
if __name__ == '__main__':
main()
Upvotes: 1
Reputation: 4130
To find the longest occurrence of a certain substring, you could use a function like this one:
def longest_segment(sub, string):
return max(m.group() for m in re.finditer(r'(%s)\1*' % sub, string))
This works by finding all occurrences of the provided substring, sub
, in the string
, and returning the longest one.
Upvotes: 3
Reputation: 637
s=raw_input() #read s from file in this case
zero=0
one=0
zz=0
oo=0
for i in list(s):
if i=='1':
if zz>=1:
zero=max(zero,zz)
zz=0
oo+=1
else:
if oo>=1:
one=max(one,oo)
oo=0
zz+=1
if oo>=1:
one=max(oo,one)
if zz>=1:
zero=max(zero,zz)
print zero,one
#O(n)
Upvotes: -1