Lexicographic order
peter wants to arrange the words in ascending order of their length and later arrange the ones with the same length in lexicographic order. Each word is given in a serial number according to its position. Find the word according to the serial number
Word serial
A 1
B 2
C 3
.. ..
X 24
Y 25
Z 26
AA 27
BB 28
... ..
BA 53
... ...
ZZ 702
AA 703
.. ...
INPUT
The first line of input is a positive integer which is the serial number
OUTPUT
The output should be a single line containing the word that represents the serial number.
EXPLANATION
Given serial number is 42
As the serial number is between 26 and 52, the first character of the word is A. The remaining counter of the serial number is 16 (42-26)
The second character of the word is the 16th alphabet, which is P
So, the output should be AP
input
42
output
AP
input
751
output
ABW
from string import ascii_uppercase
import itertools
def iter_all_strings():
for size in itertools.count(1):
for s in itertools.product(ascii_uppercase, repeat=size):
yield "".join(s)
num = int(input())
lst = []
for s in iter_all_strings():
lst.append(s)
if len(lst) == num:
print(lst[-1])
break
I have changed the answer, it is correct version
Comments
Leave a comment