Iterate for with two variables

Asked

Viewed 3,297 times

1

I have the following code:

def hasPalin(string):
    begin = 0
    end = len(string)-1
    result = ""
    while(begin < len(string)/2):
        if (string[begin] == string[end]):
            result += string[begin]
        else:
            break
        begin += 1
        end -= 1
    return result

It would be possible to transform this while in a for? That is, iterate over 2 variables, one being an increment, and the other a decrement.

I haven’t tested it, but I’m pretty sure it’s possible to do this in java, where I increment one variable while decreasing another.

  • Could you say what the purpose of this function is?

  • @Sidon It checks whether the beginning of a string is equal to the end, like a palindrome sub, being input: abcqwertbca, the output would be: abc. But my biggest doubt is whether it is possible to make one such feature.

2 answers

2


Yes you can iterate with two variables in python with for. For this you can use the function zip that returns a dropdown like this:

for var1, var2 in zip(colecao1, colecao2):

Which in your case would look something like:

tam = len(string)

for begin, end in zip(range(0,tam), range(tam-1,-1,-1)):

In which the first range builds the growing values and the second range builds the decreasing values.

If the idea of the algorithm was to check a sub-palindrome, and taking advantage of its logic can do:

def hasPalin(string):
    result = ""
    tam = int(len(string)/2)

    #Primeiro range de 0 até metade e o segundo do fim até metade
    for begin, end in zip(range(0,tam), range(len(string)-1,tam-1,-1)): 
        if string[begin] == string[end]:
            result += string[begin]
        else:
            return result #devolver o subpalíndromo. É diferente logo pode parar aqui

    return string #se chegou aqui a frase é totalmente um palíndromo e devolve o original

print(hasPalin("casaca")) #""
print(hasPalin("osctso")) #"os"
print(hasPalin("abcddcba")) #"abcddcba"
  • Try with a large string that is totally palindromic, for example: ataxxxxxxxxxata, the result would have to be the original string, correct? By logic this would happen in the code of your answer?

  • @Sidon That’s actually an interesting question. Which I don’t know what the author’s intention was. But in its optics it should return only the part that is palindromic when it is partial and the whole original text when it is totally a palindrome ?

  • If you test your code with aaddaa he’ll get confused. :-)

  • But you’re right, you need to know what the author’s real intention is, but it’s very strange a sub-palindromo on a palindrome. For example, what would be a sub in a string like this: aaaaaaaaaaaaaaaaaaaaaa? and, even more intriguing, in ada

  • @Sidon Yes I even think that what you are saying makes sense, although it is not clear the intention of the author, or even if he thought about it! But I think I’m going to change the code to accommodate that detail as well.

  • Thank you so much for your contribution, I didn’t think about those cases anyway. I bumped into this problem on some site to train programming and this doubt of the for was in my head, but you already clarified it, which was the main reason. Now, the rest I leave to you, hehe.

Show 1 more comment

2

When I arrived with the zip, Isac had already responded, but I’ll still try to make a contribution with a little more pythonic shape to the function you want:

def haspalin(_str):
    p=''
    for i in range(len(_str)-1):
        if _str[i] != _str[::-1][i]:
            return p 
        else:
            p+=_str[i]
    return _str        

print(haspalin('atabcdefata'))
ata
print(haspalin('ataxxxxxxxxxata'))
ataxxxxxxxxxata

Edited, Explanation:

_str[::-1][i]: Invert the string, then iterate the original string by comparing through the media i loop is each character of the original string with the corresponding Indice character in the inverted string, if they are equal, it is added to the variable p, when the first character is found not corresponding, the function is 'aborted', returning the variable p.

See running on repl.it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.