idalsin
idalsin

Reputation: 546

Loop Issue with Local Variable

I'm using Python (3.x) to create a simple program for an assignment. It takes a multiline input, and if there is more than one consecutive whitespace it strips them out and replaces it with one whitespace. [That's the easy part.] It must also print the value of the most consecutive whitespaces in the entire input.

Example:

input = ("This is   the input.")

Should print:

This is the input.
3

My code is below:

def blanks():
    #this function works wonderfully!
    all_line_max= []
    while True:
        try:
            strline= input()
            if len(strline)>0:
                z= (maxspaces(strline))
                all_line_max.append(z)
                y= ' '.join(strline.split())
                print(y)
                print(z)
            if strline =='END':
                break
        except:
            break
        print(all_line_max)

def maxspaces(x):
    y= list(x)
    count = 0
    #this is the number of consecutive spaces we've found so far
    counts=[]
    for character in y:
        count_max= 0
        if character == ' ':
            count= count + 1
            if count > count_max:
                count_max = count
            counts.append(count_max)
        else:
            count = 0
    return(max(counts))


blanks()

I understand that this is probably horribly inefficient, but it seems to almost work. My issue is this: I would like to, once the loop is finished appending to all_lines_max, print the largest value of that list. However, there doesn't seem to be a way to print the max of that list without doing it on every line, if that makes sense. Any ideas on my convoluted code?

Upvotes: 0

Views: 53

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121972

Just print the max of all_line_max, right where you currently print the whole list:

print(max(all_line_max))

but leave it at the top level (so dedent once):

def blanks():
    all_line_max = []
    while True:
        try:
            strline = input()
            if strline:
                z = maxspaces(strline)
                all_line_max.append(z)
                y = ' '.join(strline.split())
                print(y)
            if strline == 'END':
                break
        except Exception:
            break
    print(max(all_line_max))

and remove the print(z) call, which prints the maximum whitespace count per line.

Your maxspaces() function adds count_max to your counts list each time a space is found; not the most efficient method. You don't even need to keep a list there; count_max needs to be moved out of the loop and will then correctly reflect the maximum space count. You also don't have to turn the sentence into a list, you can directly loop over a string:

def maxspaces(x):
    max_count = count = 0

    for character in x:
        if character == ' ':
            count += 1
            if count > max_count:
                max_count = count
        else:
            count = 0

    return max_count

Upvotes: 1

Related Questions