You are here : InfyTQ Archive >> InfyTQ Coding Problems
Given a string and it contains the digits as well as non-digits. We have to find the largest even number from available digits after removing the duplicates. If not possible, print -1.
Input :
First Input : String
Output :
Largest number
Sample Testcases :
I/P 1:
%#32%#%2
O/P 1:
32
I/P 2:
%#2373#@
O/P 2:
732
Solution :
def largeeven(lst):
if(res[-1]%2==0 or res[-1]==0):
return res
else:
for j in range(1,len(lst)+1):
if(res[-j]%2==0 or res[-j]==0):
ev=res.pop(-j)
res.append(ev)
return res
else:
return -1
s1=set()
s=str(input())
for i in s:
if i.isdigit():
s1.add(int(i))
res=sorted(s1,reverse=True)
print(largeeven(s1))
import string
s=input()
l=[]
for i in s:
if i in string.digits:
l.append(i)
l=list(set(l))
print(”.join(map(str,sorted(l,reverse=True))))
sorry for indentation ‘if’ inside ‘for’ and ‘l.append’ inside ‘if ‘
Hope this helps you all 🙂
import string
s=input()
l=[]
for i in s:
if i in string.digits:
l.append(i)
l=list(set(l))
print(”.join(map(str,sorted(l,reverse=True))))
import re
n=input()
digits=list(set(re.findall(‘\d’,n)))
digits.sort()
digits.reverse()
num=int(“”.join(digits))
if num%2==0:
print(num)
else:
l=len(digits)
for i in range(len(digits)-1,0,-1):
if int(digits[i])%2==0:
a=digits[i]
digits.remove(a)
digits.insert(l-1,a)
even=int(“”.join(digits))
print(even)
break;
else:
print(-1)
last else loop is execute with for loop: