Monday 7 November 2016

Smartprix Working solution QUESTION-1 in Python

The replacement works as follows:
Example:
Replacement Array: Smart site India comparison Best
Format String: %sprix is the %s[4] online %s[3]:6 shopping %s:9 in %s
Output: Smartprix is the Best online compar shopping site in India.
If no index specifier is present with conversion specifier %s, Index 0, 1, 2, 3... are assigned to all in sequence from left to right.
In above example %sprix is %s[0]prix; %s:9 is %s[1]:9 and so on.
The specifier string is replaced by element of Replacement Array denoted by index specifier. If no element is found the specifier is not replaced.
In above example %s[4] is replaced by element at 4th index of the Replacement Array "Best". %sprix is replaced by Smartprix and so on.
%s[3]:6 is replaced by 'first 6 characters' of the element at 3rd index of the Replacement Array, i.e., "compar".
If the 'length specifier' is not present or is greater than length of the element then use whole string.
%s:9 is replaced by site, %s[4] is replaced by Best.
For any error case, specifier is not replaced.



Input:
There will be 2 lines in the input.
1st line contains space separated elements of Replacement Array
2nd line is Format String



Output:
Formatted String



INPUT 1
smart site india comaprision best
%sprix is the %s[4] online %s[3]:6 shopping %s:9 in %s
OUTPUT
smartprix is the best online comapr shopping site in india


INPUT 2
india boom startup up hub
%s %s[is] a %sing %s:5%s:5 %s[4]. and %s[6] are :4 of %s[-1].
OUTPUT
india %s[is] a booming startup hub. and %s[6] are :4 of %s[-1].




Here We Go In Python ....


import re
pat=re.compile('%s(?!\[)(?::\d+)?')
pat2=re.compile('%s[\[](\d+|)\](:&?\d+|)')
#'%s[\[](\d+|)\](:|)(\d+|)')
#l3="Smart site India comparison Best"
#l3="hello lavish kothari"
l3=raw_input()#"india boom startup up hub"
#%s??[a-z](:\d+|)
l3=l3.split(" ")
#st="%s[2]:xyz"
st=raw_input()
#st="%s %s[is] a %sing %s:5%s:5 %s[4]. and %s[6] are :4 of %s[-1]"
#st="%sprix is the %s[4] online %s[3]:6 pshopping %s:9 in %s"
s1=re.finditer(pat,st)
op=[]
o2p=[]
for i in s1:
    #print i.span()
    op.append(i.span())
#print op
s1=re.finditer(pat,st)
s2=re.finditer(pat2,st)
l1=map(lambda x:x.group(),s1)
#print l1
p1=l1[:]
l2=map(lambda x:x.group(),s2)
p2=l2[:]
#print p2
k=0
for i in range(len(l1)):
    e=len(l1[i])
    f=0
    if(len(l1[i])>=4):
        e=re.search(r'\d+',l1[i])
        e=int(e.group())
        #print e
        f=1
    if(f!=0):
        l1[i]=l1[i].replace(l1[i],l3[k][:e])
    else:
        l1[i]=l1[i].replace(l1[i],l3[k])  
    k+=1
hj=0
for i in range(len(l1)):
    #print p1[i],l1[i]
    #print hj
    st=st[:op[i][0]+hj]+l1[i]+st[op[i][1]+hj:]
    hj+=len(l1[i])-op[i][1]+op[i][0]
#print st
s2=re.finditer(pat2,st)
for i in s2:
    o2p.append(i.span())
#print o2p
s2=re.finditer(pat2,st)
vbb=0
#print l2
for i in range(len(l2)):
    e2=re.compile(r'\d+')
    sd=re.finditer(e2,l2[i])
    p=[]
    l31=map(lambda x:int(x.group()),sd)
    #print l31
    for ij in l31:
        if(ij<len(l3)):
            p.append(ij)
    #print p
    if(len(p)>=2):
        l2[i]=l3[0][:p[1]]
    else:
        if(len(p)!=0):
            #print i,p[0]
            l2[i]=l3[p[0]]
#print l2
hj=0
#print st,o2p
for i in range(len(l2)):
    #print o2p[i][0],o2p[i][1],l2[i]
    st= st[:o2p[i][0]+hj]+ l2[i]+ st[o2p[i][1]+hj:]
    #print st
    hj+=len(l2[i])-o2p[i][1]+o2p[i][0]
print st

  



Smartprix Working Approach QUESTION-2 in Python
# simple question and approach is quite straight as follows , not gonne give full code
#print shivam => shivam
import re
pat=re.compile('(?<=^print\s).*')
str="print shiva"
che=re.finditer(pat,str)
l1=map(lambda x: x.group(),che)
print l1