Home > Enterprise >  Why is my output going in an infinity loop?
Why is my output going in an infinity loop?

Time:11-01

I tried taking each string elements out of the list which contains 10 elements and pass it to the isPalindrome method to check whether it is palindrome or not. But the output gives me "It is palindrome." to an infinite times that it crashes the kernel.

`

class Palindrome():
    def isPalindrome(self, x):
        stack = []

        #for strings with even length
        if len(x)%2==0:
            for i in range(0,len(x)):
                if i<int(len(x)/2):
                    stack.append(x[i])
                elif stack.pop()!=x[i]:
                    return False
            if len(stack)>0:
                return false
            return True

        #for strings with odd length    
        else:
            for i in range(0,len(x)):
                if i==int(len(x)/2):
                    continue
                elif i<int(len(x)/2):
                    stack.append(x[i])
                elif stack.pop()!=x[i]:
                    return False
            if len(stack)>0:
                return false
            return True
    
    def __init__(self):
        while True:   
            string=["mom","dad","madam","redivider","civic","radar","refer","racecar","level","rotor"]
            for i in range(len(string)):
                if self.isPalindrome(string[i]):
                    print(string[i]," is a palindrome")
                else:
                    print(string[i]," is not a palindrome")
            
                

if __name__ == '__main__':

    WS = Palindrome()

`

CodePudding user response:

in your __init__() method, there is a while True wrapping everything - there is nothing "breaking" this loop, so it carries on forever and "It is palindrome." keeps printing

CodePudding user response:

I tried out your program and added a couple of tweaks to it to make it produce the type of output I believe you were looking for. Following is the snippet of code.

class Palindrome():
    def isPalindrome(self, x):
        stack = []

        #for strings with even length
        if len(x)%2==0:
            for i in range(0,len(x)):
                if i<int(len(x)/2):
                    stack.append(x[i])
                elif stack.pop()!=x[i]:
                    return False
            if len(stack)>0:
                return false
            return True

        #for strings with odd length    
        else:
            for i in range(0,len(x)):
                if i==int(len(x)/2):
                    continue
                elif i<int(len(x)/2):
                    stack.append(x[i])
                elif stack.pop()!=x[i]:
                    return False
            if len(stack)>0:
                return false
            return True
    
    def __init__(self):
        while True:   
            stringx=["mom","dad","madam","redivider","civic","radar","refer","racecar","level","rotor"]
            for i in stringx:                       # Made this into a loop to read the items in the list
                if self.isPalindrome(i):
                    print(i," is a palindrome")
                else:
                    print(i," is not a palindrome")
            break                                   # Added this to break out of the while loop
    
if __name__ == '__main__':

    WS = Palindrome()

A couple of things to point out. Since you really have a list of words/strings I revised the "for" loop to read each item as a list item. The other tweak was to break out of the "while" loop. Alternatively, the "while" loop could have been omitted, but if one wants to utilize a "while" loop to act in an infinite manner, usually there is some test condition that is done to then issue a "break" from the loop. In this case, the "break" out of the loop happens unconditionally.

As a result, the following output was printed out at the terminal.

@Dev:~/Python_Programs/Palindrome$ python3 Pal.py 
mom  is a palindrome
dad  is a palindrome
madam  is a palindrome
redivider  is a palindrome
civic  is a palindrome
radar  is a palindrome
refer  is a palindrome
racecar  is a palindrome
level  is a palindrome
rotor  is a palindrome

Give that a try and see if it meets the spirit of your project.

  • Related