#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.
#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]
                                print "*", runlen, pix_list[index]
                            index = index + runlen

                    pix_list = input("pixel 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!

Recommended Answers

All 2 Replies

Yes 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 if there is more than 9 same pixels in row, say 11 1s.

def mock_runs(data):
    return 4, 5, 7, 2, 1, 4

t =[1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,0,0,0,0]
data = str(t[0])
runs = []
for r in mock_runs(t):
    if r <= 3:
        runs.append('*%i%s' % (r, data))
    if data == '0':
        data = '1'
        data = '0'

print ''.join(runs)

You have to add the last group, and note that the counter is initialized to one since we are comparing this pixel to the next pixel, so this pixel is the first in a series.

You should also test your program with the first 2 pixels different and with the last 2 pixels different

test_list = [1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,0,0,0,0]
list_out = []
num_found = 1
for ctr in range(0, len(test_list)-1):
    print "comparing", test_list[ctr], test_list[ctr+1], num_found
    if test_list[ctr] == test_list[ctr+1]:
        num_found += 1
        if num_found > 3:
            str_out='*%d%d' % (num_found, test_list[ctr])
            str_out = str(test_list[ctr])*num_found
        print "     adding", str_out
        num_found = 1

## add the last group ***** note the last pixel was not added *****
if num_found > 3:
    str_out='*%d%d' % (num_found, test_list[ctr-1])
    str_out = str(test_list[ctr-1])*num_found
print "     adding", str_out

print "".join(list_out)

If that is too much code to swallow at one time, then split the original list into a list of lists which breaks on each difference. You can then go through and send to a find_run function to count the number in each sub-list.

list_out = []
this_group = []
previous = ""
for pixel in test_list:
    if pixel != previous:
        if len(this_group):  ## don't append empty list on first break
            list_out.append(this_group) ## or send to find_run function
        this_group = []

    previous = pixel
## add the last group

print list_out
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.