#Hello, I am working on a homework assignment that after many hours, I still cannot get to function correctly.
#The point is to take a list input of ones and zeros and print a compressed version.
#Example)
#input: [1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,0,0,0,0]
#output: *41*50*71001*40
#Since the compression takes up 3 symbols(asterisk, amount of pixels in a row, pixel value), there is no point #to compress a length of the same pixel values of 3 or less.
#My code so far is:
def findRun(pix_list, index):
length = len(pix_list)
runlen = 0
c = index
while pix_list[c] == pix_list[c+1]:
if c != length - 2:
c = c + 1
runlen = runlen + 1
return runlen
def makeRLE(pix_list):
index = 0
length = len(pix_list)
while index < length:
runlen = findRun(pix_list, index)+1
if runlen == 1:
print pix_list[index]
elif runlen == 2:
print pix_list[index], pix_list[index]
elif runlen == 3:
print pix_list[index], pix_list[index], pix_list[index]
else:
print "*", runlen, pix_list[index]
index = index + runlen
pix_list = input("pixel list: ")
makeRLE(pix_list)
# I know there is a problem with findRun because in makeRLE, under the while loop, I have to add one to runlen #for the program to work 'correctly'. By 'correctly' I mean that it does what I want except for the last #iteration.#
#So say)
#input: [1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,0,0,0,0]
#output: * 4 1
# * 5 0
# * 7 1
# 0 0
# 1
#What would be the best way to get the output on one line without spaces? .join()?
#Also, I put a print string after the last line and it would not print so I'm assuming my program is stuck on #the last pixel counting or exits out before it counts the last pixels
#Thanks for the help!
#
anonymous0318
0
Newbie Poster
Recommended Answers
Jump to PostYes join does fine, here I have mocked the run finding to isolate this function from your implementations of runs. It does not make sense to save actually any other info than the first value in data it is alternating between 1 and 0. Also I am wondering wjat happends …
All 2 Replies
Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.