You are given an integer array digits, where each element is a digit. The array may contain duplicates.
You need to find all the unique integers that follow the given requirements:
The integer consists of the concatenation of three elements from digits in any arbitrary order. The integer does not have leading zeros. The integer is even. For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.
Return a sorted array of the unique integers.
This is my attempt
def findEvenNumbers(self, digits):
list = set()
finallist=[]
for i in range(len(digits)):
for j in range(i+1, len(digits)):
for k in range(j+1, len(digits)):
num1 = digits[i]
num2 = digits[j]
num3 = digits[k]
conct = str(num1) + str(num2) + str(num3)
if conct[0] != '0':
list.add(conct)
for num in list:
if int(num) %2 == 0:
finallist.append(int(num))
finallist.sort()
return finallist `
**Input digits** = [2,1,3,0]
**Output** [130,210,230]
**Expected **[102,120,130,132,210,230,302,310,312,320]
**Input ** digits = [2,2,8,8,2]Output [222,228,282,288,882]Expected [222,228,282,288,822,828,882]
The issue is your code gets the combinations, not the permutations of the digits. This is because you start your inner loops with i+1 and j+1, so it doesn't go back to the start and get the other numbers. You can fix this by simply removing the i+1 and j+1 or use something like
itertools.permutationsfor clearer code