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

Sunday, 3 July 2016

              Nested loops using recursion in python

##coder @ shivamzaz#demo in c++ in simple loops
'''for(i=0;i<n;i++){
    for(j=0;j<nj++){
        cout<<n<<endl;
    }
}'''

def s_s(n):
    i=0
    s_f(i,n)
def s_f(i,n):
    if(i<n):   
        j=0
        s_g(i,j,n)
        s_f(i+1,n)
def s_g(i,j,n):
    if(j<n):
        print i,j
        s_g(i,j+1,n)
s_s(4)

Sunday, 17 April 2016

            Quick sort within 5 lines in python
def qs(ar):
    if len(ar) <= 1:
        return ar
    pvt = ar[len(ar) / 2]
    lft = [x1 for x1 in ar if x1 < pvt]
    md = [x1 for x1 in ar if x1 == pvt]
    right1 = [x1 for x1 in ar if x1 > pvt]
    return qs(lft) + md + qs(right1)

print qs([31,62,84,10,1,2,1])

Tuesday, 25 August 2015

.>>>>>>>>>>>Counting sort in ruby(PL)
#author "shivamzaz" @imsec 
a=[1,1,3,2]   #take any one arrray
d=a.max
c=Array.new((d+1)) { |i|  i=0 }
for i in 0...a.length
    c[a[i]]+=1
end
for i in 1...c.length
    c[i]=c[i]+c[i-1]
end
b=Array.new(c.max) {|v| v=0}
for i in (a.length-1).downto(0)
    b[c[a[i]]]=a[i]
    c[a[i]]-=1
end
i=a.min
while(b[i]!=nil)
    puts b[i]
    i+=1
end

>>>>>>>>>>>>>>happy coding<<<<<<<<<<<<<<<<<<

Monday, 10 August 2015

Number of unique elements using c++ in o(n):
    /*author => shivamzaz
                            level=> ad-hoc
          complexity=> o(n)  */
    #include "bits/stdc++.h"
      using namespace std;
    int main(){
    int a[]={2,2,3,2},cnt=0;
    bool b[4]={0};
    for(int i=0;i<4;i++){
        b[a[i]]=1;
    }
    for(int i=0;i<4;i++){
        if(b[i]==1){
            cnt++;
        }
    }
    printf("%d\n",cnt);
    return 0;
    }

Saturday, 8 August 2015

Quickly find fibonacci numbers(using matrix property).
 
 Divide_Conquer_Fib(n) {
       i = h = 1;
       j = k = 0;
      while (n > 0) {
               if (n%2 == 1) { // if n is odd
                   t = j*h;
                    j = i*h + j*k + t;
                    i = i*k + t;
               }
       t = h*h;
       h = 2*k*h + t;
       k = k*k + t;
       n = (int) n/2;
     }
return j;
}

Saturday, 25 July 2015

efficiently finding, number is multiple of 3 or not..(in ruby PL)
#author "shivamzaz"
#efficient for nu,ber is multipe of three or not.
#level "medium"
#specially use of bitwise operator
'''Example: 23 (00..10111)
    1) Get count of all set bits at odd positions (For 23 it’s 3).
    2) Get count of all set bits at even positions (For 23 it’s 1).
    3) If difference of above two counts is a multiple of 3 then number is also a multiple of 3.'''
#<---------------------Happy Coding-------------------------< implementation in #ruby>--------------------------Happy Coding---------------------->
g=gets().chomp.to_i
odd=0
even=0
for i in 1.upto(8)
    if(((g & (1<<(i-1)))>0) && (i%2!=0))    #(>0  becoz ruby have'nt if(1) type of stmts while takes if(1>0) means to say conditional stmts..
        odd+=1
        #puts("odd :#{i}")
    elsif(((g & (1<<(i-1)))>0) && (i%2==0))
        even+=1
        #puts("even :#{i}")

    else
        p+=1
    end
end
puts odd
puts even
if((odd-even)%3==0)
    puts("multiple of three ")
else
    puts("not multiples of three")
end