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
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
test.assert_equals(maximum_even_no("Infytq@218412"),8412)
test.assert_equals(maximum_even_no("Abhishek@4668557"),87654)
test.assert_equals(maximum_even_no("Ashish@913478"),987314)
test.assert_equals(maximum_even_no("someString&337"),-1)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings