Create 3 digit numbers that are even and count them

Question:

I need to make a program in Python that do this:
Write a program that, for a given sequence of digits, prints the number of different three-digit even numbers that can be formed from the given digits. When forming each three-digit even number, each element of the sequence of digits can be used at most once.

The first line of the standard input contains the number of digits N, such that 3≤N≤50000. In the second row is N digits separated by one space.

Print only one number to the standard output, the requested number of three-digit numbers.

n=int(input())
num=[]
for i in range (n):
     num.append ()

Input

4

2 4 2 2

Output

4

Explanation
From the digits 2, 4, 2, 2, 4 different three-digit even numbers can be formed, namely 222, 224, 242, 422.

Asked By: Petar Tomic

||

Answers:

This is a general solution that checks all permutations of these digits and print even numbers out of them:

from itertools import permutations

k = 3
c = 0
n = int(input())
num = input().strip().split(" ")

perms = set(permutations(num, k))

for perm in perms:
    t = int("".join(perm))
    if t % 2 == 0 and len(str(t)) == k:
        print(t)
        c += 1
print(c)

This is another solution if you don’t want to use this generalized approach and just want to solve it for 3 digits:

c = 0
n = int(input())
num = [int(x) for x in input().strip().split(" ")]

r = set()
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i == j or i == k or j == k:
                continue
            t = num[i] * 100 + num[j] * 10 + num[k]
            if t % 2 == 0 and len(str(t)) == 3:
                r.add(t)
print(r)
print(len(r))
Answered By: Majid

First You should declare which user input u expect and how to handle false input.
Then the inputString is split into a list of Strings aka inputList. Now You can loop through each of those inputItem and check if they fit your expectations (is int and smaller then 10 ). Since You still deal with String Items You can try to cast ( convert them) into Int. That can fail and a unhandled failure could cripple Your script so it has to happen in an try- catch-block. If the casting works You wanna make sure to check your matchList if You allready added a simular number before You add the new Number to Your matchList.You do so again by looping through each item. If there is mo such number yet there is a last check for if the number is uneven or 0. In that case there is one possible combination less for each number that is 0 or uneven so they are counted in the variable minusOneCombi. That will be substrated from the maximum amount of combinations which is the amount of numbers in you matchList multiplied with itself (pow(2)). Be very careful with the Indentations. In Python they are determining the ends of if blocks and loops.

InputString=(input("Enter your number stream eg 1 0 0 5 ( non numeric signs and numbers greater 9 will be ignored): "))
    inputList= inputString.split(‘ ’)
    matchList=[]
    minusOneCombi= 0
    for inputItem in inputList:
       try:
          anInt= int(inputItem)
       except ValueError:
        # Handle the exception
        print(“item is not an integer and therefore ignored”)
       
        NotYetinMatchList= True
        for MatchItem in MatchList:
           If matchItem == inputItem:
               notYetinMatchList= False
                Break
           If notYetinMatchList:
             matchList.append(anInt)
             if ((anInt % 2) != 0) OR (anInt ==0):
                minusOneCombi++
        
    NumofMatches=matchList.count()
    NumOfMatchesPow= pow(NumofMatches,2)
    Result=NumOfMatchesPow -minusOneCombi
    Print(Result)
Answered By: Dom
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.