Identify sort order Infosys springboard coding question with solution

Problem Statement :

Given a non-empty string instr containing only digits, print a string outstr based on the below logic:

Assumptions :

instr will contain at least 2 unique digits

Input Format :

Read the string instr from the standard input stream.

Output Format :

Print outstr to the standard output stream.

Question :

Code Answer :

				
					def solve(s):
    l=[]
    for i in range(len(s)):
        l.append(int(s[i]))
    w=""
    a=True
    for i in range(1,len(s)):
        if s[i-1]>s[i]:
            a=False
            break
    if a == True:
        w=w+(s[len(s)-1])+(s[0])
        return w
    a=True
    for i in range(1,len(s)):
        if s[i-1]<s[i]:
            a=False
            break
    if a == True:
        w=w+(s[0])+(s[len(s)-1])
        return w
    st=0
    start=0
    end=0
    startm=0
    endm=0
    pt=0
    #for check of longest int the string acending
    for i in range(1,len(s)):
        if s[i-1]<=s[i]:
            end=i
        else:
            p=end-start+1
            
            if p>st:
                st=p
                startm=start
                endm=end
            
            start=i
            end=i
    p=end-start+1
    if p>st:
        st=p
        startm=start
        endm=end
        
    start=i
    end=i
    #check fr the decending order
    start=0
    end=0
    pt=startm
    for i in range(1,len(s)):
        if s[i-1]>=s[i]:
            end=i
        else:
            p=end-start+1
            if p>st:
                st=p
                startm=start
                endm=end
            if p==st and start<pt:
                startm=start
                endm=end
                pt=start
            start=i
            end=i
    p=end-start+1
    if p>st:
        st=p
        startm=start
        endm=end
    if p==st and start<pt:
        startm=start
        endm=end
        pt=start    
    start=i
    end=i
    for x in range(startm,endm+1):
        w=w+s[x]
    return w
    
s=str(input())
print(solve(s))
				
			

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.