forked from arnavjindal/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpracticeProblem-4.py
96 lines (76 loc) · 2.29 KB
/
practiceProblem-4.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'''
'Learn from Mistakes'
Author: Arnav
Date: 8 Dec 2020
Purpose: Practice problem
'''
# “The Next Palindrome”
# Problem Statement:-
# A palindrome is a string that, when reversed, is equal to itself. Example of the palindrome includes:
#
# 676, 616, mom, 100001.
#
# You have to take a number as an input from the user. You have to find the next palindrome corresponding to that number. Your first input should be the number of test cases and then take all the cases as input from the user.
#
# Input:
# 3
#
# 451
#
# 10
#
# 2133
#
# Output:
# Next palindrome for 451 is 454
#
# Next palindrome for 10 is 11
#
# Next palindrome for 2311 is 2222
#----------------------------------------------------------------------------------------------------------------------------------------------------------------
counter =0
n = int(input("Enter number of test cases: \n"))
# nums = [i for i in input("Enter numbers separated by ',' ").split(",")]
num = []
for j in range(n):
num.append(int(input(f"Enter number {j+1} \n")))
for k in num:
breaker = 0
while breaker != 1:
lis = []
palin = k+counter
for o in str((palin)):
lis.append(o)
for i in range(int((len(lis)+1)/2)):
if lis[i] == lis[-(i+1)]:
if i == (int(((len(lis)+1)/2)-1)):
print(f"Next palindrome is {palin}\n")
breaker =1
else:
continue
else:
break
counter += 1
# really poor execution of program
# I forgot to use the three methods he taught in last problem (I picked the hardest Method)
# CWH's execution (Pretty neat):
'''
Author: Harry
Date: 15 April 2019
Purpose: Practice Problem For CodeWithHarry Channel
'''
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__":
n = int(input("Enter the number of test cases\n"))
numbers = []
for i in range(n):
number = int(input("Enter the number:\n"))
numbers.append(number)
for i in range(n):
print(f"Next palindrome for {numbers[i]} is {next_palindrome(numbers[i])}")