We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importmathimportosimportrandomimportreimportsys## Complete the 'biggerIsGreater' function below.## The function is expected to return a STRING.# The function accepts STRING w as parameter.#defbiggerIsGreater(w):if"".join(sorted(w,reverse=True))==w:return"no answer"length=len(w)iflength==2:returnw[::-1]base_index=length-1whilebase_index>0:ifw[base_index]<=w[base_index-1]:base_index-=1else:base_index-=1breakifbase_index+1==length-1:returnw[:-2]+w[-1]+w[-2]result=w[:base_index]remainder=sorted(w[base_index:])base_num=w[base_index]highest=list(filter(lambdax:x>base_num,remainder))[0]remainder.remove(highest)result+=highest+"".join(remainder)returnresultif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')T=int(input().strip())forT_itrinrange(T):w=input()result=biggerIsGreater(w)fptr.write(result+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →