fun-with-string

Xavier challenges one of his friend to perform some crazy string functions by giving him some words and each word basically denotes a type of string function which his friend has to perform.

WORDS WITH THEIR MEANING:-

Wordrev n->To arrange each words of the string in reverse order.The last   letter n
denotes upto which letter the words are to be reversed in the   string. For example:-the given string is my name is xavier,the given task is Wordrev 3.Then the output must be ym eman si xavier.The 3 words are reversed because the last letter is 3 that is the task is performed for 3 words,and the remaining are simply dispalyed same as they were.

subpalin n->To find the nth palindrome substring present in the given string.Here n denotes the nth palindrome substring. For eg the string is xyzbabmadamh,the task is subpalin 2.The output must be madam which is the substring of given input string."ababa" is also a palin substring but it   is not the second,it was the first substring.

sortword n->To sort the given string words alphabetically.The letter n denotes the step upto which the sorting is to be done.For example:-The string is he loves to play cricket and the given task is sortword 2.Then the output would be "cricket he to play loves" only the first two words are sorted rest remainsunsorted 

Xaviers friend plans to write a program to solve these questions easily.

Input FORMAT

The first line inputs the String on which the task is to be performed.
Next line denotes number of test cases T.
The next line inputs the T NUMBER tasks to be performed one by one.

Output Format

The output string after the application of each function one by one.

SAMPLE INPUT
my name is xavier
2
Wordrev 3
sortword 2

SAMPLE OUTPUT
ym eman si xavier
is my name xavier

Difficulty: UNKNOWN_DIFFICULTY

Starter code:

Solutions:
def sort_words(seq, n):
	res = []
	for x in range(n):
		cur = min(seq)
		seq.remove(cur)
		res.append(cur)
	for x in seq:
		res.append(x)
	return res 


	
		

s = input().strip().split()
noc = eval(input())
while noc:
	noc-=1
	
	ins = input().split()
	com = ins[0]
	ndx = int(ins[1])
	res = []
	if com == "Wordrev":
		for i in range(ndx):
			res.append(s[i][::-1])
		#res =  " ".join(res)
		for i in range(ndx, len(s)):
			res.append(s[i])
		print(" ".join(res))     
   
	if com == "subpalin":
		count = 0
		q = ""
		for c in s:           
			q=q+str(c)+" "            
			l=len(q)
			for i in range(l):
				for j in range(i+2,l):
					#print q[i:j]
					if(q[i:j]==(q[i:j])[::-1]):
						count+=1
					if count==ndx:
						print(q[i:j])
						break
				if count==ndx:
					break       
		
	   
	if com == "sortword":           
		res = sort_words(s, ndx)
		print(" ".join(res))


def rev(s,n):
	s=list(s.split())
	t=''
	for i in range(len(s)):
		if i<n:
			t=t+s[i][::-1]+' '
		else:
			t=t+s[i]+' '
	t=t[:len(t)-1]
	print(t)
def sort(s,n):
	s=list(s.split())
	ans=''
	for i in range(n):
		for j in range(i+1,len(s)):
			if s[i]>s[j]:
				t=s[i]
				s[i]=s[j]
				s[j]=t
	for i in range(0,len(s)):
		ans=ans+s[i]+' '
	ans=ans[:len(ans)-1]
	print(ans)
def subpalin(s,n):
	f=0
	c=0
	s=s.replace(" ","")
	for i in range(len(s)):
		if f==1:
			break
		for j in range(i+1,len(s)):
			t=s[i:(j+1)]
			if t==t[::-1]:
				c=c+1
			if c==n:
				print(t)
				f=1
				break
			
s=input()
tc=int(eval(input()))
while tc>0:
	l=list(map(str,input().split()))
	if l[0]=='Wordrev':
		rev(s,int(l[1]))
	elif l[0]=='sortword':
		sort(s,int(l[1]))
	else:
		subpalin(s,int(l[1]))
	tc=tc-1


st = input()
task = int(input())
for op in range(task):
	tas, n = input().split()
	n = int(n)
	if tas == "Wordrev":
		k = [''.join(p[::-1]) for p in st.split()]
		l = st.split()
		new = k[:n] + l[n:]
		print(' '.join(new))
	elif tas == "sortword":
		z = st.split()
		z.sort()
		res = z[:n]
		for w in st.split():
			if w not in res:
				res.append(w)
		print(' '.join(res))
	elif tas == "subpalin":
		#print st
		i = 0
		j = 1
		palin = []
		while (i!= len(st)):
			wor = []
			for s in range(i, j+1):
				wor.append(st[s])
			wor = ''.join(wor)	
				
			#print wor	
			if len(wor) >= 2 and wor == wor[::-1]:
				
				#print wor
				palin.append(wor)
				#print palin
			if j < len(st)-1:
				j = j+1
			else:
				i += 1
				j = 1
				#print "hua"
		print(palin[n-1])		
		
			


def palindrome(x):
	y = x[::-1]
	if x == y:
		return True
	else:
		return False

s = input()
a = s.split()
t = int(input())
for i in range(t):
    task = input().split()
    if task[0] == 'Wordrev':
        n = int(task[-1])
        z = ''
        for i in range(n):
            if i == len(a) - 1:
                z += a[i][::-1]
            else:
                z += a[i][::-1] + ' '
        for i in range(n, len(a)-1):
            if i == len(a) - 1:
                z += a[i]
            else:
                z += a[i] + ' '
        try:
            z += a[-1]
        except:
            pass
        print(z)

    if task[0] == 'sortword':
        n = int(task[-1])
        a3 = a[:]
        a3 = sorted(a3, key = lambda x:x[:n])
        a2 = a[:]
        a1 = a3[:n]
        z = ''
        for i in range(n):
            a2.remove(a1[i])
            if i == len(a)-1:
                z += a1[i]
            else:
                z += a1[i] + ' '
        for i in range(len(a2)-1):
            if i == len(a2) - 1:
                z += a2[i]
            else:
                z += a2[i] + ' '
        try:
            z += a2[-1]
        except:
            pass
        print(z)

    if task[0] == 'subpalin':
        n = int(task[-1])
        z = ''
        for i in a:
            z += i
        count = 0
        flag = 0
        for i in range(len(z)):
            for j in range(i+1, len(z)):
                if palindrome(z[i:j+1]):
                    count += 1
                    if count == n:
                        print(z[i:j+1])


s = input()
T = int(input())

def nth_palindrome(s, n):
	palindrome_count = 0
	for i in range(1, len(s)):
		for offset in 0, 1:
			a = i
			b = i + offset
			while a >= 0 and b < len(s):
				if s[a] != s[b]:
					break
				pal = s[a:b+1]
				a -= 1
				b += 1
		if len(pal) > 1:
			palindrome_count += 1
			if palindrome_count == n:
				return pal

for i in range(T):
	cmd, n = input().split()
	n = int(n)

	if cmd == 'Wordrev':
		ss = s.split()
		l = [w[::-1] for w in ss[:n]] + ss[n:]
		print(' '.join(l))
	elif cmd == 'sortword':
		ss = s.split()
		nw = sorted(ss)[:n]
		nws = set(nw)
		l = nw + [w for w in ss if w not in nws]
		print(' '.join(l))
	elif cmd == 'subpalin':
		print(nth_palindrome(s, n))


def sort_words(seq, n):
	res = []
	for x in range(n):
		cur = min(seq)
		seq.remove(cur)
		res.append(cur)
	for x in seq:
		res.append(x)
	return res 


	
		

s = input().strip().split()
noc = eval(input())
while noc:
	noc-=1
	
	ins = input().split()
	com = ins[0]
	ndx = int(ins[1])
	res = []
	if com == "Wordrev":
		for i in range(ndx):
			res.append(s[i][::-1])
		#res =  " ".join(res)
		for i in range(ndx, len(s)):
			res.append(s[i])
		print(" ".join(res))     
   
	if com == "subpalin":
		count = 0
		q = ""
		for c in s:           
			q=q+str(c)+" "            
			l=len(q)
			for i in range(l):
				for j in range(i+2,l):
					#print q[i:j]
					if(q[i:j]==(q[i:j])[::-1]):
						count+=1
					if count==ndx:
						print(q[i:j])
						break
				if count==ndx:
					break       
		
	   
	if com == "sortword":           
		res = sort_words(s, ndx)
		print(" ".join(res))