Answer to Question #329087 in Python for ratul

Question #329087

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


1
Expert's answer
2022-04-17T19:32:36-0400

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




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS