Ad
Fundamentals
Mathematics
Algorithms
Logic
Numbers
Strings
Data Types

Ques 7: Input type String. Output type number. Print out the maximum even number which can be generated by making combinations of numbers present in the given string. Each number can be used only once ie. No Redundancy.

Example1:
Input String = Infytq@218412
Intermediate step (List of numbers //redundancy removed) = [2,1,8,4]
Output Number = 8412

Example2
Input String = someString&337
Intermediate step (List of numbers //redundancy removed) = [3,7]
Output Number = -1

def maximum_even_no(string):
    list1=[]
    list2=[]
   
    for i in string:
        if(i.isdigit()):
            list1.append(i)
    list1=list(dict.fromkeys(list1))
    list2=sorted(list1,reverse=True)
    if(int(list2[-1])%2==0 or int(list2[-1])==0):
        return int("".join(list2))
    list3=range(len(list2))
    list3=sorted(list3,reverse=True)
    for j in list3:
        if(int(list2[j])%2==0):
            temp=list2.pop(j)
            list2.append(temp)
            break
    result="".join(list2)
    if(int(result)%2==0):
        return int(result)
    else:
        return -1