Write a function called show_palindrome that takes a number as an argument and then returns
a palindrome string. Finally, prints the returned value in the function call.
Example1:
Function Call:
show_palindrome(5)
Output:
123454321
===================================================================
Example2:
Function Call:
show_palindrome(3)
Output:
12321
Version 1:
def show_palindrome(number):
answer = ''
for i in range(1, number * 2):
if i <= number:
answer += str(i)
else:
answer += str(number * 2 - i)
print(answer)
show_palindrome(5)
show_palindrome(3)
Output:
123454321
12321
Version 2:
def show_palindrome(number):
answer = [str(i) if i <= number else str(number * 2 - i) for i in range(1, number * 2)]
print(*answer, sep='')
show_palindrome(5)
show_palindrome(3)
Output:
123454321
12321
Comments
Leave a comment