aboutsummaryrefslogtreecommitdiff
path: root/challenge-115/paulo-custodio/python/ch-2.py
blob: 1e080c88f2c16be55415772dcd207e2f87a76dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

# Challenge 115
#
# TASK #2 - Largest Multiple
# Submitted by: Mohammad S Anwar
# You are given a list of positive integers (0-9), single digit.
#
# Write a script to find the largest multiple of 2 that can be formed from the
# list.
#
# Examples
# Input: @N = (1, 0, 2, 6)
# Output: 6210
#
# Input: @N = (1, 4, 2, 8)
# Output: 8412
#
# Input: @N = (4, 1, 7, 6)
# Output: 7614

import sys

def largest_mult2(nums):
    # select smallest even number for last element
    even = list(filter(lambda x: x%2 == 0, nums))
    if len(even)==0:
        return 0        # no even numbers
    even.sort()
    last = even[0]

    # sort the other elements in descending order
    nums = list(filter(lambda x: x!=last, nums))
    nums.sort()
    nums = nums[::-1]

    return int("".join([str(x) for x in [*nums, last]]))

print(largest_mult2([int(x) for x in sys.argv[1:]]))