Friday, March 1, 2013

Decreasing String-codechef

Question:

You need to find a string which has exactly K positions in it such that the character at that position comes alphabetically later than the character immediately after it. If there are many such strings, print the one which has the shortest length. If there is still a tie, print the string which comes the lexicographically earliest (would occur earlier in a dictionary). Input The first line contains the number of test cases T. Each test case contains an integer K (≤ 100). Output Output T lines, one for each test case, containing the required string. Use only lower-case letters a-z.
Sample Input
2
1
2
Sample Output
ba
cba
http://www.codechef.com/problems/DECSTR
Solution:http://www.codechef.com/viewsolution/701336 I tried but it took time to understand,here's a solution.n very first line we can learn many things..
STRING = ''.join(map(lambda x: chr(x+97), reversed(range(26))) * 4)
 
def shortest_k_string(k):
 if k < 26: return STRING[103-(k+0):]
 if k < 51: return STRING[103-(k+1):]
 if k < 76: return STRING[103-(k+2):]
 if k < 101: return STRING[103-(k+3):]
 
t = int(raw_input())
for i in range(t):
 k = int(raw_input())
 print shortest_k_string(k)
Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

No comments:

Post a Comment