forked from arnavjindal/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpracticeProblem-5.py
71 lines (52 loc) · 1.65 KB
/
practiceProblem-5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# “Palindromify the List”
#
# The task is very similar to the previous one i.e. Tutorial #109 ( Pyhton Problem 4)
#
# Problem Statement:-
# You are given a list that contains some numbers. You have to print a list of next palindromes only if the number is greater than 10; otherwise, you will print that number.
#
# Input:
# [1, 6, 87, 43]
#
# Output:
# [1, 6, 88, 44]
def next_palindrome(n):
if n<10:
return n
n = n+1
while not is_palindrome(n):
n += 1
return n
def is_palindrome(n):
return str(n) == str(n)[::-1]
if __name__ == "__main__":
numbers = [i for i in input("Enter the number(s) separated by ',' :\n").split(',')]
numbers = [int(i) for i in numbers]
finallis = []
for i in range(len(numbers)):
finallis.append(next_palindrome(numbers[i]))
print(finallis)
#CWH's SOLUTION --
# WAAH!!! maja aa gaya padh ke. kya concept use kiya hai.
def next_palindrome(n):
n = n+1
while not is_palindrome(n):
n += 1
return n
def is_palindrome(n):
return str(n) == str(n)[::-1]
if __name__ == "__main__":
size = int(input("Enter the size of your list\n"))
num_list = []
for i in range(size):
num_list.append(int(input("Enter the number of the list\n")))
print(f"You have entered {num_list}")
print(f"Output List: {[num_list[i] if num_list[i] < 10 else next_palindrome(num_list[i] ) for i in range(size)]}")
# new_list = []
# for element in num_list:
# if element >10:
# n = next_palindrome(element)
# new_list.append(n)
# else:
# new_list.append(element)
# print(f"Output List: {new_list}")