How to make this for loop in Python work?

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

I want to spit out a list of palindromes until a certain letter I give.

It's about this part:

def pyramid_palindrome(last_letter):     for letter in range(97,last_letter):         last_letter = last_letter - (last_letter-1)         print call_first_part_palindrome(last_letter)         print call_second_part_palindrome(last_letter) 

What I'd want is, to do this:

a aba abcba abcdcba abcdedcba abcdefedbca abcdefgfedcba abcdefghgfedcba .... 

I am thinking like this:

"I will give the last letter as an input, and it will keep iterating the palindrome until the last letter is being checked."

The problem is that I can't manage to spit out the palindromes one by one, ascending it just like a pyramid. Can someone point me in the right direction?

Am I right when I say I need to use a for loop in the first function?

This is my full code:

def pyramid_palindrome(last_letter):     for letter in range(97,last_letter):         last_letter = last_letter - (last_letter-1)         print call_first_part_palindrome(last_letter)         print call_second_part_palindrome(last_letter)  def call_first_part_palindrome(last_letter):      letters_a_to_y = ""                   for letter in range(97,last_letter):         letters_a_to_y += chr(letter)       return(letters_a_to_y)  def call_second_part_palindrome(last_letter):       letters_y_to_a = ""      for letter in range(last_letter,96,-1):          letters_y_to_a += chr(letter)      return(letters_y_to_a)  pyramid_palindrome(112) 

I think I am close, but can't make the finishing touch.

回答1:

Your problem was when calling your two palindrome functions and on how you were changing the value for last_letter-

I tried to modify your code as little as possible:

def pyramid_palindrome(last_letter):     for letter in range(97,last_letter):         print(call_first_part_palindrome(letter) + call_second_part_palindrome(letter-2)) 

And this remains the same:

def call_first_part_palindrome(last_letter):      letters_a_to_y = ""                   for letter in range(97,last_letter):         letters_a_to_y += chr(letter)       return(letters_a_to_y)  def call_second_part_palindrome(last_letter):       letters_y_to_a = ""      for letter in range(last_letter,96,-1):          letters_y_to_a += chr(letter)      return(letters_y_to_a)  pyramid_palindrome(112) 

Which outputs:

a aba abcba abcdcba abcdedcba abcdefedcba abcdefgfedcba abcdefghgfedcba abcdefghihgfedcba abcdefghijihgfedcba abcdefghijkjihgfedcba abcdefghijklkjihgfedcba abcdefghijklmlkjihgfedcba abcdefghijklmnmlkjihgfedcba 


回答2:

Your this entire logic could be simplified using string slicing along with string.ascii_lower as:

import string alphs = string.ascii_lowercase   # returns string of lower case characters last_letter = 'f'  for i in range(len(alphs)):     print alphs[:i]+alphs[i::-1]     if alphs[i] == last_letter:  # break the loop when `last_letter` is found         break 

which will generate output as:

a aba abcba abcdcba abcdedcba abcdefedcba     

Edit: If you do not want to import string, you may get the string of lowercase characters via using:

alphs = ''.join(chr(i) for i in range(97,123)) 


回答3:

Your code is more complicated than it needs to be, and does unnecessary work generating each string of letters from scratch.

I'm not sure why you don't want to import the letter string from the string module, but you can easily generate the string of letters once and then slice it to get the substrings required to build each palindrome. The code below works on Python 2 or 3.

def pyramid_palindrome(last_letter):     letters = ''.join([chr(i) for i in range(97, last_letter)])     for i in range(last_letter - 97):         print(letters[:i] + letters[i::-1])  pyramid_palindrome(102) 

output

a aba abcba abcdcba abcdedcba 

Alternatively, keep letters as a list and use .join on the sliced lists:

def pyramid_palindrome(last_letter):     letters = [chr(i) for i in range(97, last_letter)]     for i in range(last_letter - 97):         print(''.join(letters[:i] + letters[i::-1])) 

It's theoretically faster to add two lists than two strings, although there are optimizations in CPython for small strings so you may not notice the difference unless the strings are longer than 1000 or so. OTOH, calling .join once on letters is probably better than calling it for each palindrome.


Here's a minor variation of the first version. We save all the palindromes into a list of strings. Then the caller can join that list of strings into one string and print it with one print call.

def pyramid_palindrome(last_letter):     letters = [chr(i) for i in range(97, last_letter)]     return [''.join(letters[:i] + letters[i::-1])         for i in range(last_letter - 97)]  print('\n'.join(pyramid_palindrome(102))) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!