Home > Net >  I want to find common prefix out of this List. it work. but all the time final element of the list n
I want to find common prefix out of this List. it work. but all the time final element of the list n

Time:08-26

if you run code with first three items with list, final item not evaluated. but if you run code with four elements it work.

more explain : when we run code with three items in list, up to second items loop. but when it get third items to process, all program terminated by 'break' keyword. but without 'break' it shows another error call 'out of index'

but if you run code with four item in the list, it work properly.. ( you can see what I say run code and see terminal. I get all the steps, who code work line by line )

could you help me please...

names = ["flower", "flow", "fluihr"]
common = ''
start = 0

for items in names:
    print('loop item -->', items)
    # insert first world to common variablea
    if(names.index(items) ==  0):
        common = items
        print("this is first index ->", items, common)
    else:
        # get minimum len of the both word
        if(len(common) > len(items)):
            minNum = len(items)
            print('this is min number as len(items) ->', minNum, items)
        else:
            minNum = len(common)
            print('this is min number as len(common) ->', minNum, items)

        # get common prefix and insert to common variable
        while start < minNum:
            print("this is while loop start point -->",start, minNum, "this is item variable -->", items)
            if(common[start] == items[start]):
                start  = 1
                print("this is evaluation point -->", common[start])
            else:
                common = common[:start]
                print('this is final common -->', common)
                start = 0
                print('this is new stating point -->', start)
                breakenter code here

if(len(common) > 1):
    print(common)
else:
    print("")




# print(common)

CodePudding user response:

The PROBLEM here is that you aren't resetting start for each loop. Move the start = 0 line to just before the while loop and it all works.

Here are some other improvements:

    if(names.index(items) ==  0):
        common = items
        print("this is first index ->", items, common)

No need to do a search here. Just:

    if not common:
        common = items
        print("this is first index ->", items, common)

And here:

        # get minimum len of the both word
        if(len(common) > len(items)):
            minNum = len(items)
            print('this is min number as len(items) ->', minNum, items)
        else:
            minNum = len(common)
            print('this is min number as len(common) ->', minNum, items)

You can replace all of that with:

        minNum = min(len(common), len(items))
        print('this is min number as len(common) ->', minNum, items)

And Python if statements do not use outer parentheses. That's a bad habit left over from C.

CodePudding user response:

As I understood your code right (I needed to edit something to make it compilable), the problem is in a part where you are getting common prefix and inserting to common variable.
In case where:

common[start] == items[start]         # == False

In a statement else you are assigning a value:

common[:start]

to a variable

common

But the value start is assigned as zero, so when you use:

common[:start]

It becomes and empty string, so after coming back to If statement in your loop it compares:

common[start]

But as it`s emptry string, the value common[start] does not exist

  • Related